开发者

When using .jsp's view spring mvc, and passing ModelAndView, how to output model?

开发者 https://www.devze.com 2023-01-05 09:07 出处:网络
If I have a modelandView like: ModelAndView mav = new ModelAndView(); mav.setViewName(\"index\"); mav.addObject(\"message\", \"hello, world\");

If I have a modelandView like:

ModelAndView mav = new ModelAndView();

mav.setViewName("index");
mav.addObject("message", "hello, world");

return mav;

In index.开发者_如何学Pythonjsp, how do I output the value of "message"?

And what if I passed in:

mav.addObject("user", currentUser);

It seems the docs jump straight into forms handling.


You can output it by using the EL language like this:

${message} and ${user}

In your jsp. Spring with automatically scan the jsp and process these and other EL expressions.


You can use

<c:if test="${message!=null}">
        ${message}
    </c:if>


1- If you are using the old JSP 1.2 descriptor, defined by DTD for example 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>

The EL is disabled or ignored by default, you have to enable it manually, so that it will outputs the value store in the message model.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<%@ page isELIgnored="false" %>
</head>
<body>
           ${message}
</body>
</html>

2- In JSP 2.0

If you are using the standard JSP 2.0 descriptor, defined by w3c schema ,for example 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>

The EL is enabled by default, and you should see the value stored in the message model, which is hello world.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号