spring mvc EL ModelAndView的 Model 值 在jsp中不顯示
阿新 • • 發佈:2019-01-24
問題:
spring mvc開發過程中, 經常會給model addAttribute, 然後通過EL在jsp中顯示,比如 ${msg},
但是有時候會出現jsp最後顯示的還是${msg},而不是msg賦值過的。
Spring Controller
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class ABCController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("HelloWorldPage");
model.addObject("msg", "hello world");
return model;
}
}
JSP page
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
${msg}
</body>
</html>
解決方法:
原因在於老的JSP 1.2 宣告.
1. JSP 1.2
如果你用的jsp1.2版本的DTD
web.xml
<!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>
//...
</web-app>
EL是預設關閉的。。。必須手動開啟。<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<%@ page isELIgnored="false" %>
</head>
<body>
${msg}
</body>
</html>
2. JSP 2.0
JSP2.0預設是開啟支援EL的,所以宣告jsp2.0就可以直接用了。
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
//...
</web-app>
spring mvc開發過程中, 經常會給model addAttribute, 然後通過EL在jsp中顯示,比如 ${msg},
但是有時候會出現jsp最後顯示的還是${msg},而不是msg賦值過的。
Spring Controller
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class ABCController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("HelloWorldPage");
model.addObject("msg", "hello world");
return model;
}
}
JSP page
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
${msg}
</body>
</html>
解決方法:
原因在於老的JSP 1.2 宣告.
1. JSP 1.2
如果你用的jsp1.2版本的DTD
web.xml
<!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>
//...
</web-app>
EL是預設關閉的。。。必須手動開啟。<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<%@ page isELIgnored="false" %>
</head>
<body>
${msg}
</body>
</html>
2. JSP 2.0
JSP2.0預設是開啟支援EL的,所以宣告jsp2.0就可以直接用了。
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
//...
</web-app>