具有 "text/plain" 类型的 ModelAndView returns 源代码
ModelAndView returns source code with "text/plain" type
我的来源
@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 method" 时,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>
帮助
好的,我想我明白了。更改
<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.
您的页面似乎是作为纯资源文件从服务器返回的。这意味着 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>
其次,检查你的 beans 配置是否有正确的 viewResolver bean。
我的来源
@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 method" 时,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>
帮助
好的,我想我明白了。更改
<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.
您的页面似乎是作为纯资源文件从服务器返回的。这意味着 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>
其次,检查你的 beans 配置是否有正确的 viewResolver bean。