`

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"/>
<property name="requestContextAttribute" value="rc"/>
</bean>
</beans>

红色部分就是请求上下文的配置,配置到velocity上下文后,就可以在vm模版里面直接使用了:${rc.contextPath}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics