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

I have the following code in the controller

final ModelAndView m = new ModelAndView();
m.addObject("date", "15" );

In my opinion, I was able to deduce this by doing

${date}

However, how can I print it using out.print or assign it to a new variable for example.

<% out.print( ${date} ) %>

thank

+3
source share
3 answers

If I'm not mistaken, this is:

(Integer)pageContext.getAttribute("date")

therefore it will be:

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

in your code.

0
source

While on the jsp page, you can get the value datewith:

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

JSTL .

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

Source: https://habr.com/ru/post/1741420/


All Articles