开发者社区> 问答> 正文

Spring已集成jsp的环境下同时集成Velocity 400 请求出错 

1、集成使用的jar:Spring 3.0.5 ,Velocity-1.7,velocity-tools-1.4 2、配制后缀(*.vm)映射。修改web.xml,添加如下代码  

<servlet> <servlet-name> my</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet>

<servlet-mapping>
    <servlet-name> my</servlet-name>
    <url-pattern>*.vm</url-pattern>
</servlet-mapping></pre> 

并相应的创建一个my-servlet.xml(webapp\WEB-INF\my-servlet.xml),因为DispatcherServlet会去找这个配制文件并初始化,这个相当重要。   3、配制解析*.vm的请求  

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 配制velocity的解析视图 --> <!-- VelocityResolver 继承Spring的VelocityViewResolver,并扩展其layout模版布局 --> <bean id="viewResolver2" class="com.my.velocity.VelocityResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".vm" /> <property name="contentType" value="text/html;charset=UTF-8" /> <!-- VelocityLayoutView 继承 Spring的VelocityToolboxView,用于支持toolbox.xml中的类  --> <property name="viewClass" value="com.my.velocity.VelocityLayoutView" />
<property name="layoutUrl" value="/WEB-INF/velocity/layout/default.vm"/> <property name="layoutKey" value="layout"/> <property name="layoutDir" value="/WEB-INF/velocity/layout/" /> <property name="toolboxConfigLocation" value="/WEB-INF/velocity/toolbox.xml"/> </bean> <!-- velocity的本身配制 --> <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/" /> <property name="configLocation" value="/WEB-INF/velocity/velocity.properties"/> </bean> <!-- 配制 .vm 解析UIRI相对应的Controller类--> <import resource=" dispatcher-servlet-my.xml"/> </beans>
  4、页面请求与表单处理(/WEB-INF/dispatcher-servlet-blog.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean name="/my/.vm" class="com.my.blog.controller.DiaryController"> <property name="diaryService" ref="diaryService" /> </bean> </beans>
如:显示 /blog/user/diary_cata.vm的数据,在DiaryController.java写一个如下方法即可:
public ModelAndView diary_cata(HttpServletRequest req,HttpServletResponse resp){ ModelAndView mv = new ModelAndView("blog/diary/diary_cata"); return mv; }
提交表单数据同理:
public ModelAndView save_diary(HttpServletRequest req,HttpServletResponse resp){ ModelAndView mv = new ModelAndView("blog/diary/save_diary"); return mv; }

展开
收起
kun坤 2020-05-28 16:06:07 648 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
The Reactive Cloud Native Arch 立即下载
集成智能接入网关APP:优化企业级移动办公网络 立即下载
云效助力企业集成安全到DevOps中 立即下载

相关实验场景

更多