搭建springmvc項目,在寫測試類的時候,視圖解析器一直轉發不成功

首先個人一些配置文件以下html

web.xmljava

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

    <!-- 配置 DispatcherServlet -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!--加載的時候建立-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--設置處理規則-->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>


</web-app>

springmvc.xmlweb

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

    <!-- 配置自定掃描的包 -->
    <context:component-scan base-package="controller"></context:component-scan>

    <!--配置視圖解析器,解析規則爲 prefix+返回的String+suffix-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--返回類,自動轉爲json-->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <!-- 用於避免響應頭過大 -->
                <property name="writeAcceptCharset" value="false"/>
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

</beans>

index.jspspring

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>aaaa</title>
</head>
<body>
 <a href="/test/hello.action">hello</a>
</body>
</html>

success.jspjson

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body> 轉發成功 ----111
</body>
</html>

TestController後端

package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/test") public class TestController { @RequestMapping("hello.action") @ResponseBody public String hello(){ return "success"; } }

目錄結構spring-mvc

 

這沒毛病啊,這麼就死活跳轉不成功了呢,並且頁面上顯示的都是successmvc

排查了各個配置文件,沒問題,那應該是controller的問題,原來我在controller類方法上順手加了 @ResponseBody 標籤,先後端分離的開發習慣欲哭無淚啊app

網上對這個標籤的解釋是這樣的前後端分離

@requestBody註解經常使用來處理content-type不是默認的application/x-www-form-urlcoded編碼的內容,好比說:application/json或者是application/xml等。通常狀況下來講經常使用其來處理application/json類型

好吧,刪了這個標籤,idea出現了一個很友好的小圖標

再次啓動項目跳轉,成功

相關文章
相關標籤/搜索