springmvc和jsp,velocity,freemarker等的整合

springmvc和jsp,velocity,freemarker等的整合

可以任意切换,使用jsp,velocity,freemarker的任意一个,也可以选择三个同时使用

使用springmvccontroller在spring-servlet.xml里面这样配置:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">

<!---Applicationcontextdefinitionfor"springserver"DispatcherServlet.-->

<beansdefault-autowire="byName">

<!--resourceBundleviewbegin三个一起使用-->

<!--

<beanid="velocityConfig"

class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">

<propertyname="resourceLoaderPath"value="/template/velocity/"/>

</bean>

<beanid="freemarkerConfig"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

<propertyname="templateLoaderPath"

value="/template/freemarker/"/>

</bean>

<beanid="viewResolver"

class="org.springframework.web.servlet.view.ResourceBundleViewResolver">

<propertyname="basename">

<value>views</value>

</property>

</bean>

-->

<!--resourceBundleviewend-->

<!--jspviewbegin使用JSP-->

<beanid="jspViewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<propertyname="viewClass">

<value>org.springframework.web.servlet.view.JstlView</value>

</property>

<propertyname="prefix"value="/template/jsp/"/>

<propertyname="suffix"value=".jsp"/>

</bean>

<!--jspviewend-->

<!--velocityviewbegin使用VELOCITY-->

<!--

<beanid="velocityConfig"

class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">

<propertyname="resourceLoaderPath"value="/template/velocity/"/>

</bean>

<beanid="velocityViewResolver"

class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">

<propertyname="cache"value="false"/>

<propertyname="suffix"value=".vm"/>

<propertyname="exposeSpringMacroHelpers"value="true"/>

</bean>

-->

<!--velocityviewend-->

<!--freemarkerviewbegin使用FREEMARKER-->

<!--

<beanid="freemarkerConfig"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

<propertyname="templateLoaderPath"

value="/template/freemarker/"/>

</bean>

<beanid="freemarkerViewResolver"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">

<propertyname="cache"value="false"/>

<propertyname="suffix"value=".ftl"/>

<propertyname="exposeSpringMacroHelpers"value="true"/>

</bean>

-->

<!--freemarkerviewend-->

<beanid="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<propertyname="maxUploadSize"value="2000000"/>

</bean>

<beanid="methodNameResolver"

class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">

<propertyname="paramName"value="method"/>

<propertyname="defaultMethodName"value="index"/>

</bean>

</beans>

要使用哪个东东的时候就把注释放开就可以了。另外,三个同时使用时,要增加一个配置文件views.properties

#jsparea############################################################

#前面的这个user/userList要和controller里面的保持一致

user/userList.class=org.springframework.web.servlet.view.JstlView

user/userList.url=/template/jsp/user/userList.jsp

########################################################################

#velocityarea##########################################################

user/userEdit.class=org.springframework.web.servlet.view.velocity.VelocityView

user/userEdit.url=/user/userEdit.vm

##########################################################################

#freemarkerarea##############################################################

user/userShow.class=org.springframework.web.servlet.view.freemarker.FreeMarkerView

user/userShow.url=/user/userShow.ftl

#############################################################################

目录结构我是这样设计的,

template是总目录,下面分别是template/jsp

template/freemarker

template/velocity

里面的template/jsp/user中的USER是模块名字

UserController.java如下,这个是ACTION层:

packagecom.sillycat.core.web;

importjava.util.List;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.springframework.web.servlet.ModelAndView;

importcom.sillycat.core.model.User;

importcom.sillycat.core.service.UserManager;

importcom.sillycat.plugin.commons.base.BaseManageController;

publicclassUserControllerextendsBaseManageController{

privateUserManageruserManager;

{

listView="user/userList";

editView="user/userEdit";

showView="user/userShow";

successView="redirect:/user.do";

}

protectedvoidonList(HttpServletRequestrequest,

HttpServletResponseresponse,ModelAndViewmav)throwsException{

Listlist=userManager.getAllUser();

mav.addObject("users",list);

}

protectedvoidonEdit(HttpServletRequestrequest,

HttpServletResponseresponse,ModelAndViewmav)throwsException{

Integerid=newInteger(request.getParameter("id"));

Useruser=userManager.getUser(id);

mav.addObject("user",user);

}

protectedvoidonShow(HttpServletRequestrequest,

HttpServletResponseresponse,ModelAndViewmav)throwsException{

Integerid=newInteger(request.getParameter("id"));

Useruser=userManager.getUser(id);

mav.addObject("user",user);

}

publicvoidsetUserManager(UserManageruserManager){

this.userManager=userManager;

}

}

相关推荐