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" />4、页面请求与表单处理(/WEB-INF/dispatcher-servlet-blog.xml)。
<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>
<?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; }
有些自己实现的类没有放出来,你这一套配置就相当于没啥用撒。######两个继承的类见:http://www.oschina.net/code/snippet_1092_3741######
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。