spring mvc + velocity 页面使用contextPath

添加文件/webapp/WEB-INF/spring/webmvc-config.xml
文件内容
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="writeAcceptCharset" value="false" />
</bean>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<bean class="com.baifubao.superhero.service.rest.client.CustomAllEncompassingFormHttpMessageConverter">
<constructor-arg name="charset" value="GBK" />
</bean>
<bean class="com.baifubao.superhero.service.rest.client.CustomMappingJackson2HttpMessageConverter">
<constructor-arg name="charset" value="GBK" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

<context:component-scan base-package="com.baifubao" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

<context:property-placeholder location="classpath:payfront-app-config.properties" order="1" ignore-unresolvable="true"/>

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.baifubao.superhero.util.log.mdc.WebLogMdcHandlerInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.baifubao.superhero.service.rest.context.ServiceContextInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.baifubao.superhero.util.web.security.access.BnsAccessHandlerInterceptor">
<property name="enable" value="${payfront.tomcat.access.interceptor.enable}" />
</bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/services/**" />
<bean class="com.baifubao.superhero.util.log.digest.web.WebDigestLogHandlerInterceptor" c:loggerName="PAGE-DIGEST-LOGGER" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/services/**" />
<bean class="com.baifubao.superhero.util.log.digest.web.WebDigestLogHandlerInterceptor" c:loggerName="SERVICE-DIGEST-LOGGER" />
</mvc:interceptor>
</mvc:interceptors>

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>

<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource
requests to the container's default Servlet -->
<mvc:default-servlet-handler/>

<!-- Resolves localized messages*.properties and application.properties files in the application to allow for internationalization.
The messages*.properties files translate Roo generated messages which are part of the admin interface, the
application.properties resource bundle localizes all application specific messages such as entity names and menu items. -->
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application" p:fallbackToSystemLocale="false"/>

<!-- Enable this for integration of file upload functionality -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"/>

<!-- Velocity -->
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/view"/>
<property name="velocityProperties">
<props>
<prop key="input.encoding">GBK</prop>
<prop key="output.encoding">GBK</prop>
<prop key="contentType">text/html;charset=GBK</prop>

<prop key="file.resource.loader.cache">${velocity.file.resource.loader.cache}</prop>
<prop key="file.resource.loader.modificationCheckInterval">${velocity.file.resource.loader.modificationCheckInterval}</prop>
<prop key="velocimacro.library.autoreload">${velocity.velocimacro.library.autoreload}</prop>

<prop key="velocimacro.library">macro.vm</prop>
</props>
</property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="suffix" value=".vm"/>
<property name="prefix" value="vm/" />
<property name="layoutUrl" value="layouts/default.vm" />
<property name="contentType" value="text/html;charset=GBK" />

<property name="cache" value="${velocity.view.resolver.cache}"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="exposeRequestAttributes" value="true"/>
<property name="exposeSessionAttributes" value="true"/>
[color=red]<property name="requestContextAttribute" value="rc"/> [/color]
</bean>
</beans>

红色部分就是请求上下文的配置,配置到velocity上下文后,就可以在vm模版里面直接使用了:${rc.contextPath}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值