spring + velocity + sitemesh2 乱码问题

以前都是用spring + jsp + sitemesh2,近来想学习下velocity。工程很快搭起来了,但是sitemesh在处理代码的时候出现了中文乱码,搞了一个上午终于搞定,在此mark一下。当然,这个方法解决了我的问题,不一定能解决大家的问题,在此发出来,仅供参考。


1.web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <display-name>NewLand</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/applicationContext.xml</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.velocity.properties</param-name>
        <param-value>/WEB-INF/config/velocity.properties</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Spring MVC Servlet -->
    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Filter 定义 -->
    <!-- Character Encoding filter -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

<!--     SiteMesh Web-Page Layout filter -->
    <filter>
        <filter-name>sitemeshFilter</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sitemeshFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>sitemesh-velocity</servlet-name>
        <servlet-class>com.opensymphony.module.sitemesh.velocity.VelocityDecoratorServlet</servlet-class>
        <load-on-startup>10</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>sitemesh-velocity</servlet-name>
        <url-pattern>*.vm</url-pattern>
    </servlet-mapping>
</web-app>

以下配置需要注意下,VelocityDecoratorServlet会自行初始化一个VelocityEngine的对象,需要指定velocity的配置文件。如果不指定改配置,在运行时会出现请求页面编码正常,但是被sitemesh加上的header和footer文件里面中文出现乱码。(两个用的不是同一个VelocityEngine,spring使用一个,sitemesh使用一个。。有时间研究研究velocity的单例应用模式,避免多处配置)

    <context-param>
        <param-name>org.apache.velocity.properties</param-name>
        <param-value>/WEB-INF/config/velocity.properties</param-value>
    </context-param>

2.velocity配置

#encoding
input.encoding=UTF-8
output.encoding=UTF-8
contentType=text/html;charset=UTF-8
default.contentType=text/html;charset=UTF-8
default.encoding=UTF-8
#autoreload when vm changed
file.resource.loader.cache=false
file.resource.loader.modificationCheckInterval=1


3.spring-mvc.xml配置

<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:annotation-config />

    <!-- 自动扫描且只扫描@Controller -->
    <context:component-scan base-package="com.test.web" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>

    <!-- 定义JSP文件的位置 -->
    <!-- <bean -->
    <!-- class="org.springframework.web.servlet.view.InternalResourceViewResolver"> -->
    <!-- <property name="prefix" value="/WEB-INF/views/" /> -->
    <!-- <property name="suffix" value=".jsp" /> -->
    <!-- </bean> -->

    <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/views/" />
        <property name="configLocation" value="/WEB-INF/config/velocity.properties" />
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="cache" value="false" />
        <property name="prefix" value="" />
        <property name="suffix" value=".vm" />
        <!-- 如果你需要使用Spring 对 Velocity宏命令的支持, 将这个属性设为true  -->
        <property name="exposeSpringMacroHelpers" value="true" />
        <!--注意下面这句,如果没有,会导致乱码 -->
        <property name="contentType">
            <value>text/html;charset=UTF-8</value>
        </property>
    </bean>

    <mvc:annotation-driven />

    <!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL -->
    <mvc:default-servlet-handler />

    <!-- 定义无需Controller的url<->view直接映射 -->
    <mvc:view-controller path="/" view-name="index" />

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.Throwable">error/500</prop>
            </props>
        </property>
    </bean>
</beans>


4.sitemesh配置

a.decorators.xml

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/layouts">
    <excludes>
        <pattern>/static/*</pattern>
    </excludes>

    <decorator name="default" page="default.vm">
        <pattern>/*</pattern>
    </decorator>
    
    <decorator name="none">
            <!-- These files will not get decorated. -->
            <pattern>/anotherdir/*</pattern>
        </decorator>
</decorators>

b.sitemesh.xml

<sitemesh>
    <property name="decorators-file" value="/WEB-INF/decorators.xml"/>
    <excludes file="${decorators-file}"/>

	<page-parsers>
		<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
	</page-parsers>

	<decorator-mappers>

		<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
			<param name="property.1" value="meta.decorator" />
			<param name="property.2" value="decorator" />
		</mapper>

		<mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper">
		</mapper>

		<mapper class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
			<param name="match.MSIE" value="ie" />
			<param name="match.Mozilla [" value="ns" />
			<param name="match.Opera" value="opera" />
			<param name="match.Lynx" value="lynx" />
		</mapper>

		<mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
			<param name="decorator" value="printable" />
			<param name="parameter.name" value="printable" />
			<param name="parameter.value" value="true" />
		</mapper>

		<mapper class="com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper">
			<param name="decorator" value="robot" />
		</mapper>

		<mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
			<param name="decorator.parameter" value="decorator" />
			<param name="parameter.name" value="confirm" />
			<param name="parameter.value" value="true" />
		</mapper>

		<mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper">
		</mapper>

		<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
			<param name="config" value="${decorators-file}" />
		</mapper>

	</decorator-mappers>

</sitemesh>

项目文件存放结构



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值