当前位置: 代码迷 >> JavaScript >> springmvc跟jsp,velocity,freemarker等的整合
  详细解决方案

springmvc跟jsp,velocity,freemarker等的整合

热度:949   发布时间:2012-10-24 14:15:58.0
springmvc和jsp,velocity,freemarker等的整合
springmvc和jsp,velocity,freemarker等的整合


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

使用springmvc controller在spring-servlet.xml里面这样配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- - Application context definition for "springserver" DispatcherServlet. -->
<beans default-autowire="byName">
<!-- resourceBundle view begin 三个一起使用-->
<!--
   <bean id="velocityConfig"
   class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
   <property name="resourceLoaderPath" value="/template/velocity/" />
   </bean>
 
   <bean id="freemarkerConfig"
   class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
   <property name="templateLoaderPath"
   value="/template/freemarker/" />
   </bean>
 
   <bean id="viewResolver"
   class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
   <property name="basename">
   <value>views</value>
   </property>
   </bean>
-->

<!-- resourceBundle view end -->
<!-- jsp view begin 使用JSP -->
<bean id="jspViewResolver"
   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="viewClass">
    <value>org.springframework.web.servlet.view.JstlView</value>
   </property>
   <property name="prefix" value="/template/jsp/" />
   <property name="suffix" value=".jsp" />
</bean>

<!-- jsp view end -->

<!-- velocity view begin 使用VELOCITY-->
<!--
   <bean id="velocityConfig"
   class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
   <property name="resourceLoaderPath" value="/template/velocity/" />
   </bean>
 
   <bean id="velocityViewResolver"
   class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
   <property name="cache" value="false" />
   <property name="suffix" value=".vm" />
   <property name="exposeSpringMacroHelpers" value="true" />
   </bean>
-->

<!-- velocity view end -->

<!-- freemarker view begin 使用FREEMARKER-->
<!--
   <bean id="freemarkerConfig"
   class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
   <property name="templateLoaderPath"
   value="/template/freemarker/" />
   </bean>
   <bean id="freemarkerViewResolver"
   class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
   <property name="cache" value="false" />
   <property name="suffix" value=".ftl" />
   <property name="exposeSpringMacroHelpers" value="true" />
   </bean>
-->
<!-- freemarker view end -->

<bean id="multipartResolver"
   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <property name="maxUploadSize" value="2000000" />
</bean>

<bean id="methodNameResolver"
   class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
   <property name="paramName" value="method" />
   <property name="defaultMethodName" value="index" />
</bean>

</beans>

要使用哪个东东的时候就把注释放开就可以了。另外,三个同时使用时,要增加一个配置文件views.properties
#jsp area############################################################
#前面的这个user/userList要和controller里面的保持一致
user/userList.class=org.springframework.web.servlet.view.JstlView
user/userList.url=/template/jsp/user/userList.jsp
########################################################################

#velocity area ##########################################################
user/userEdit.class=org.springframework.web.servlet.view.velocity.VelocityView
user/userEdit.url=/user/userEdit.vm
##########################################################################

#freemarker area##############################################################
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层:
package com.sillycat.core.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.sillycat.core.model.User;
import com.sillycat.core.service.UserManager;
import com.sillycat.plugin.commons.base.BaseManageController;

public class UserController extends BaseManageController {

private UserManager userManager;

{
   listView = "user/userList";
   editView = "user/userEdit";
   showView = "user/userShow";
   successView = "redirect:/user.do";
}

protected void onList(HttpServletRequest request,
    HttpServletResponse response, ModelAndView mav) throws Exception {
   List list = userManager.getAllUser();
   mav.addObject("users", list);
}

protected void onEdit(HttpServletRequest request,
    HttpServletResponse response, ModelAndView mav) throws Exception {
   Integer id = new Integer(request.getParameter("id"));
   User user = userManager.getUser(id);
   mav.addObject("user", user);
}

protected void onShow(HttpServletRequest request,
    HttpServletResponse response, ModelAndView mav) throws Exception {
   Integer id = new Integer(request.getParameter("id"));
   User user = userManager.getUser(id);
   mav.addObject("user", user);
}

public void setUserManager(UserManager userManager) {
   this.userManager = userManager;
}
}
1 楼 viano 2011-03-11  
请问 您的代码测试过?
2 楼 sillycat 2011-03-11  
demo并展示过,不过三种一起使用,还是没有在实际的项目中使用过。