Java / Spring / JSP - how to output value added using ModelAndView.addObject
I have the following code in my controller
final ModelAndView m = new ModelAndView(开发者_JAVA百科);
m.addObject("date", "15" );
In my view I have been able to output this by doing
${date}
However how can I print it out using out.print or assign it to a new variable e.g.
<% out.print( ${date} ) %>
Thanks
From within your jsp page, you can retrieve the value of date
with:
<%
Integer myDate = (Integer) request.getAttribute("date");
out.println(myDate);
%>
You could use a little JSTL help to set the variable to the page scope.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:set var="date" value="${date}"/>
<%
String date = (String) pageContext.getAttribute("date");
out.println(date);
%>
If I am not mistaken, it's:
(Integer)pageContext.getAttribute("date")
so it will be:
<% out.println((Integer)pageContext.getAttribute("date")); %>
in your code.
精彩评论