Spring MVC - 为@RequestMapping方法配置StringHttpMessageConverter,该方法返回解释为逻辑视图名称的字符串

时间:2015-05-31 15:34:09

标签: java spring spring-mvc java-ee web

我尝试创建一个返回字符串的@RequestMapping方法(这是一个JSP文件的路径)

@RequestMapping(value="/home", method=RequestMethod.GET)
    public String home(){
        logger.info("home");
        return "blog/home";
    }

直到我在mvc:annotation-driven文件中定义@ControllerAdvice(我需要将其设置为能够使用dispatcher-servlet.xml)时才能正常工作。这就是我的dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"    
    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">

    <import resource="controllers.xml" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/*;charset=UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

</beans>

home()方法的返回解释为HTTP正文。 我在我的日志中遇到了这个问题

Written [redirect:/blog/home] as "text/html" using [org.springframework.http.converter.StringHttpMessageConverter@61f77969]

我不知道我错过了这条道路&#34; blog / home&#34;不解释为视图参考

0 个答案:

没有答案