Spring MVC结果转换

一、返回视图——ModelAndView

1、视图路径
默认在当前Control的路径下,/表示项目部署的根目录。
例如:
new ModelAndView(“home.jsp”)返回的路径是/user/home.jsp
new ModelAndView(“/home.jsp”)返回的路径是/home.jsp

@Controller
@RequestMapping("/user")
public class UserController{

    @Resource(name="userService")
    private UserService userService;

    @RequestMapping(value="/home")
    public ModelAndView home(){
        return new ModelAndView("/home.jsp");
    }
}

2、视图解析器

常见的视图:jsp、freemarker、velocity

<!-- freemarker视图解析器的配置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="suffix">
            <value>.ftl</value>
        </property>
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView
            </value>
        </property>
        <property name="contentType" value="text/html;charset=UTF-8"></property>
    </bean>
    <bean id="freemarkerConfiguration"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <description>
            FreeMarker Config in web tier.
        </description>
        <property name="templateLoaderPaths">
            <list>
                <value>/WEB-INF/freemarker</value>
            </list>
        </property>
        <property name="freemarkerSettings">
            <props>
                <prop key="default_encoding">UTF-8</prop>
                <prop key="output_encoding">UTF-8</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="number_format">#</prop>
                <prop key="whitespace_stripping">true</prop>
                <prop key="classic_compatible">true</prop>
            </props>
        </property>
        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape" />
                <entry key="html_escape" value-ref="fmHtmlEscape" />
            </map>
        </property>
    </bean>


    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />

    <bean id="fmHtmlEscape" class="freemarker.template.utility.HtmlEscape" />

常见视图解析器的配置,可参考:
http://blog.csdn.net/dearsny/article/details/45275709

二、返回String

<mvc:annotation-driven>  
    <mvc:message-converters>  
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.ObjectToStringHttpMessageConverter"/>
    </mvc:message-converters>  
</mvc:annotation-driven>

三、返回JSON

1、使用jackson
首先引入jackson的jar包,然后添加下面的配置。

<mvc:annotation-driven>  
    <mvc:message-converters>  
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    </mvc:message-converters>  
</mvc:annotation-driven>

2、使用fastjson
首先引入fastjson的jar包,然后添加下面的配置。

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8"/>
        </bean>
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            <property name="charset" value="UTF-8"/>
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json</value>
                    <value>text/html;charset=UTF-8</value>
                </list>
            </property>
            <property name="features">
                <list>
                    <value>WriteMapNullValue</value>
                    <value>QuoteFieldNames</value>
                    <value>WriteDateUseDateFormat</value>
                    <value>WriteEnumUsingToString</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

四、静态资源的访问

对于html、css、js等静态资源,访问时,是不需要经过服务端处理的,也就是不需要让DispatcherServlet进行拦截,所以,静态资源的访问需要添加下面的配置。

<mvc:resources location="/resources/" mapping="/resources/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/> 
<mvc:resources location="/css/" mapping="/css/**"/>

附:
/**的意思是所有文件夹及里面的子文件夹
/*的意思是所有文件夹,不含子文件夹
/的意思是web项目的根目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值