Ian morgan
Ian morgan

Reputation: 945

Java / Spring / JSP - how to output value added using ModelAndView.addObject

I have the following code in my controller

final ModelAndView m = new ModelAndView();
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

Upvotes: 4

Views: 8363

Answers (3)

izilotti
izilotti

Reputation: 4937

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); 
%>

Upvotes: 1

ccheneson
ccheneson

Reputation: 49410

From within your jsp page, you can retrieve the value of date with:

<%
Integer myDate = (Integer) request.getAttribute("date");
out.println(myDate);
%>

Upvotes: 1

Winston Chen
Winston Chen

Reputation: 6879

If I am not mistaken, it's:

(Integer)pageContext.getAttribute("date")

so it will be:

<% out.println((Integer)pageContext.getAttribute("date")); %>

in your code.

Upvotes: 0

Related Questions