需要配置velocity实现layout功能
我的配置是这样的:
web.xml(velocity和springmvc部分)
<!-- spring mvc servlet --> <servlet> <description>spring mvc servlet</description> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc config</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- spring mvc servlet end --> <!-- velocity servlet --> <servlet> <servlet-name>velocity</servlet-name> <servlet-class>org.apache.velocity.tools.view.servlet.VelocityLayoutServlet</servlet-class> <init-param> <param-name>org.apache.velocity.properties</param-name> <param-value>/WEB-INF/velocity.properties</param-value> </init-param> <init-param> <param-name>org.apache.velocity.toolbox</param-name> <param-value>/WEB-INF/tools.xml</param-value> </init-param> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping> <servlet-name>velocity</servlet-name> <url-pattern>*.vm</url-pattern> </servlet-mapping>
<!-- velocity引擎 --> <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath"> <value>/WEB-INF/velocity/</value><!-- 页面文件路径相对于webapp --> </property> <!--<property name="configLocation" value="/WEB-INF/velocity.properties" /> --> <property name="velocityProperties"> <props> <prop key="input.encoding">UTF-8</prop> <prop key="output.encoding">UTF-8</prop> <prop key="contentType">text/html;charset=UTF-8</prop> </props> </property> </bean> <!-- velocity视图解析器 --> <bean id="velocityViewResover" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"> <property name="exposeRequestAttributes" value="true" /> <property name="cache" value="false"/> <property name="layoutUrl" value="/layout/default.vm" /> <property name="prefix" value="" /> <property name="suffix" value=".vm" /> <property name="exposeSpringMacroHelpers" value="true" /> <property name="contentType" value="text/html;charset=UTF-8" /> <property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityLayoutView" /> <!-- 多ViewResovler配置 --> <!-- <property name="order" value="1" /> --> </bean>
velocity.properties
runtime.log.logsystem.class = org.apache.velocity.runtime.log.ServletLogChute,org.apache.velocity.tools.view.ServletLogChute # by default, load resources with webapp resource loader and string resource loader (in that order) resource.loader = webapp,string webapp.resource.loader.class = org.apache.velocity.tools.view.WebappResourceLoader string.resource.loader.class = org.apache.velocity.runtime.resource.loader.StringResourceLoader # allows getting and setting $request, $session and $application attributes using Velocity syntax, # like in #set($session.foo = 'bar'), instead of $session.setAttribute('foo','bar') runtime.introspector.uberspect = org.apache.velocity.util.introspection.UberspectImpl,org.apache.velocity.tools.view.WebappUberspector default.contentType=text/html input.encoding=UTF-8 output.encoding=UTF-8 encoding.default=UTF-8 #response.setContentType("text/html;charset=UTF-8"); #request.setContentType("text/html;charset=UTF-8"); # Filepath for error template, # relative to web application root directory tools.view.servlet.error.template = WEB-INF/velocity/Error.vm # Directory for layout templates, # relative to web application root directory tools.view.servlet.layout.directory = WEB-INF/velocity/layout/ # Filepath of the default layout template # relative to the layout directory # NOT relative to the root directory of the webapp! tools.view.servlet.layout.default.template = default.vm文件结构如下:
WEB-INF
-velocity
--Error.vm
--components
--layout
---default.vm
---layout.vm
---hreader.vm
---footer.vm
--templates
-velocity.properties
这样配置哪里错了呢,求解
求解答 ,问题困扰很久了 ######@红薯######昨天的问题还没有解决啊,应该贴一下有什么报错的截图 ######1:http://localhost:8080/project
这样访问正常(用的是默认的layout模版 default.vm)
2:http://localhost:8080/ project/userController/testDefaultLayout.do
这样访问就会找不到default.vm模版里面的#parse('layout/fixedHeader.vm'),报路径错误
3:把路径改成
#parse('WEB-INF/velocity/layout/fixedHeader.vm')能够找到fixedHeader.vm,但是首页不能访问,通过(#set($path=$link.getContextPath()))这种方式获取的路径也不可用,
4:如果testDefaultLayout.vm里面也有链接点击连接出现如下路径http://localhost:8080/project/userController/userController/testDefaultLayout.do所以又404错误
求解啊 希望表达清楚了
这里比较详细
http://www.cnblogs.com/tony-law/archive/2012/09/17/2689776.html
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。