开发者社区> 问答> 正文

springMVC3.2+velocity1.7一般是怎么配置的呢:报错

需要配置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>



spring-mvc.xml(velocity部分)
<!-- 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

这样配置哪里错了呢,求解

展开
收起
kun坤 2020-06-12 22:18:05 452 0
1 条回答
写回答
取消 提交回答
  • 求解答 ,问题困扰很久了 ######@红薯######昨天的问题还没有解决啊,应该贴一下有什么报错的截图 ######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

    2020-06-12 22:18:14
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载