ModelAndView返回“text / plain”类型的源代码

时间:2015-01-03 10:57:33

标签: java jsp spring-mvc

我的来源

@RequestMapping(value = "/", method=RequestMethod.GET)
public ModelAndView mainPage() {

    ModelAndView model = new ModelAndView("uploadPage");
    model.addObject("msg", "hello world");

    return model;
}

我的web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/mvc/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>
</web-app>

但是当执行“mainPage方法”时,jsp页面看起来像是带有“text / plain”类型的源代码,而不是正常页面...

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ page isELIgnored="false" %>
<title>Hello Spring MVC</title>
</head>
<body>
    <h2>
        <c:out value="${msg}" />
    </h2>
</body>
</html>

帮助 enter image description here

2 个答案:

答案 0 :(得分:2)

好的,我知道了。

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

到这个

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

无论哪种方式适合你。致this answer

答案 1 :(得分:1)

您的页面似乎是作为普通资源文件从服务器返回的。这意味着Spring根本没有被调用,你的文件可能会错过.jsp扩展名。 首先检查你的web.xml,看看DispatcherServlet是否被正确映射以获取你的URL。如果不确定,请填写

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

其次,检查你的bean配置是否有正确的viewResolver bean。