velocity mybatis spring 在maven的整合开发(三)

原创
2014/07/04 10:54
阅读数 218
Spring自带的MVC框架, 相当于struts,单springMVC兼容性好,且性能更高。 通过modelAndView 控制view。这里介绍SpringMVC整合Velocity作为前台架构

SpringMVC配置文件

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
	http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
	http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.1.xsd">

	<description>Spring MVC Configuration</description>

	<!-- 对静态资源文件的访问,交给default servlet handler处理 -->
	<mvc:default-servlet-handler />

	<!-- 启用spring mvc 注解 -->
	<context:annotation-config />

	<!-- 默认的注解映射的支持 -->
	<mvc:annotation-driven />

	<!-- 设置使用注解的类所在的jar包 -->
	<context:component-scan base-package="ct.bean,ct.controllers" />

	<!-- Velocity视图文件解析配置 -->
	<bean id="velocityConfig"
		class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
		<property name="configLocation" value="/WEB-INF/velocity/velocity.properties"/>
	</bean>
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
		<property name="prefix" value="/template/" />
		<property name="suffix" value=".vm" />
		<property name="contentType" value="text/html;charset=utf-8"/>
		<property name="requestContextAttribute" value="true"/>
	</bean>
        
	<!-- 国际化配置文件 -->
	<bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basename" value="classpath:i18n/messages" />
		<property name="defaultEncoding" value="UTF-8" />
	</bean>

	<bean id="localeResolver"
		class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
		<property name="defaultLocale" value="zh" />
	</bean>

	<bean id="localeChangeInterceptor"
		class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
		<property name="paramName" value="lang" />
	</bean>


	<bean id="handlerMapping"
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="interceptors">
			<ref bean="localeChangeInterceptor" />
		</property>
	</bean>

	<!-- 上传文件拦截,设置最大上传文件大小 10M=10*1024*1024(B)=10485760 bytes -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- one of the properties available; the maximum file size in bytes -->
		<property name="maxUploadSize">
			<value>5242880</value>
		</property>
	</bean>
</beans>

Velocity配置文件

velocity.properties

velocimacro.permissions.allow.inline=true
velocimacro.permissions.allow.inline.to.replace.global=true
velocimacro.permissions.allow.inline.local.scope=true
input.encoding=UTF-8
output.encoding=UTF-8
contentType=text/html;charset=utf-8
resource.loader=webapp, class
class.resource.loader.description=Velocity Classpath Resource Loader
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader
webapp.resource.loader.path=/WEB-INF/velocity/
webapp.resource.loader.cache=false

配置文件解释

1、Velocity模板位置:

velocityConfig 里配置 Velocity全局配置文件。包括字符编码,Velocity模板位置等属性。

velocity.properties中,我们定义了webapp.resource.loader.path=/WEB-INF/velocity/  意思是把Velocity模板都放在WEB-INF的Velocity目录下。

spring-mvc.xml中,我们在ViewResolver定义了<property name="prefix" value="/template/" />  意思是SpringMVC模板的目录为 template。同时指定<property name="suffix" value=".vm" /> 意思是以.vm为结尾的文件。

最终Velocity模板位置是/WEB-INF/velocity/template/在 该目录下以.vm为扩展名的文件,都会被SpringMVC拦截,作为Velocity的视图模板。

2、上传文件的拦截器。

见multipartResolver

界面展示

/WEB-INF/velocity/template/index.vm 文件里随便写一些东西。

注:Velocity文件扩展名是.vm的但实际上就是一个html文件。

html界面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>信息管理系统界面</title>
</head>

<body>
		Hello
</body>
</html>



Controller.java文件

@Controller
public class WelcomeController{
    @RequestMapping("index")
    public String welcome() {
    	System.out.println("in index................");
        return "index";
    }
}



在tomcat上部署查看是否界面被正常显示

ps spring4.1 和tomcat8有些不兼容的地方。如果报错无法处理,就降级用tomcat7或6.tomcat尽量不要用当前最高版本的。

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
1 收藏
0
分享
返回顶部
顶部