How to iterate over all my model attributes on my JSP page?

I am using Spring 3.2.11.RELEASE with JBoss 7.1.3.Final and Java 6. I have this method in the controller

@RequestMapping(value = "/method", method = RequestMethod.GET) public String myMethod(final Model model, final HttpServletRequest request, final HttpServletResponse response, final Principal principal) ... model.addAttribute("paramName", "paramValue"); 

Notice how I add attributes to my model. My question is, on the JSP page on which this page works, how can I iterate over all the attributes in my model and display them as HIDDEN input fields with the name INPUT, which is the name of the attribute, and the value that I inserted into using this attribute?

Edit: In response to the provided answer, a JSP solution is displayed here. Please note that there are no model attributes in it.

  <input type='hidden' name='javax.servlet.jsp.jspRequest' value='org.spring framework.web.context.support.ContextExposingHttpServletRequest@ 7a0a4c3f'> <input type='hidden' name='javax.servlet.jsp.jspPageContext' value=' org.apache.jasper.runtime.PageContextImpl@3939794a '> <input type='hidden' name='appVersion' value='???application.version???'> <input type='hidden' name='javax.servlet.jsp.jspResponse' value=' org.owasp.csrfguard.http.InterceptRedirectResponse@722033be '> <input type='hidden' name='javax.servlet.jsp.jspApplication' value=' io.undertow.servlet.spec.ServletContextImpl@14c1252c '> <input type='hidden' name='org.apache.taglibs.standard.jsp.ImplicitObjects' value=' javax.servlet.jsp.el.ImplicitObjectELResolver$ImplicitObjects@23 c27a49'> <input type='hidden' name='javax.servlet.jsp.jspOut' value=' org.apache.jasper.runtime.JspWriterImpl@b01a1ba '> <input type='hidden' name='javax.servlet.jsp.jspPage' value=' org.apache.jsp.WEB_002dINF.views.lti.launch_jsp@1dcc48bf '> <input type='hidden' name='javax.servlet.jsp.jspConfig' value=' io.undertow.servlet.spec.ServletConfigImpl@3fd40806 '> 
+5
source share
5 answers

The attributes of the model are objects "request area" you can do the following (I use JSTL):

  <c:forEach items="${requestScope}" var="par"> <c:if test="${par.key.indexOf('attrName_') > -1}"> <li>${par.key} - ${par.value}</li> </c:if> </c:forEach> 

Since without a filter you will have all the objects of the request area, I am filtered by the model attributes that we wanted to check

I tested this code:

 @RequestMapping(method = { RequestMethod.GET }, value = { "/*" }) public String renderPage(Model model) throws Exception { String requestedUrl = req.getRequestURI(); int indice = requestedUrl.lastIndexOf('/'); String pagina = requestedUrl.substring(indice + 1); try { String usernameUtente = "default username utente"; if (StringUtils.hasText(getPrincipal())) { usernameUtente = getPrincipal(); } model.addAttribute("usernameUtente", usernameUtente); model.addAttribute("webDebug", webDebug); for(int i = 0; i<10; i++) { model.addAttribute("attrName_"+i, "attrValue_"+i); } return pagina; } catch (Exception e) { String message = "Errore nell'erogazione della pagina " + pagina; logger.error(message, e); return "genericError"; } } 

And this is what I see as the output (I missed the wrong prints, but note that you will print ALL the objects in the request area:

 attrName_0 - attrValue_0 attrName_1 - attrValue_1 attrName_2 - attrValue_2 attrName_3 - attrValue_3 attrName_4 - attrValue_4 attrName_5 - attrValue_5 attrName_6 - attrValue_6 attrName_7 - attrValue_7 attrName_8 - attrValue_8 attrName_9 - attrValue_9 

Hope this helps

Angelo

+2
source

To avoid headaches with the parameters added by Spring and the Servlet container, it is better to use a separate map for the skip values ​​in the model. Just use @ModelAttribute and Spring will create and automatically add it to the model:

 @RequestMapping(value = "/method", method = RequestMethod.GET) public String myMethod(final Model model, @ModelAttribute("map") HashMap<String, Object> map) { map.put("paramName1", "value1"); map.put("paramName2", "value2"); //...and so on } 

Now you can iterate over this map in JSP:

 <c:forEach items="${map.keySet()}" var="key"> <input type="hidden" name="${key}" value="${map[key]}"/> </c:forEach> 

You can also access each element of the map as follows:

 <c:out value="${map.paramName1}"/> <c:out value="${map.paramName2}"/> ... 

If you don’t need any parameter to repeat, add it to the source ModelMap istead of a separate map.

+2
source

In essence, all you need is to repeat all the attributes of the page. Depending on what you use on jsp (scriptlets, jstl or smthing, like thymeleaf for html):

scriptlet:

 <form> <% Session session = request.getSession(); Enumeration attributeNames = session.getAttributeNames(); while (attributeNames.hasMoreElements()) { String name = attributeNames.nextElement(); String value = session.getAttribute(name); %> <input type='hidden' name="<% name %>" value="<% value %>"> <% } %> </form> 

JSTL:

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <h3>Page attributes:</h3> <form> <c:forEach items="${pageScope}" var="p"> <input type='hidden' name='${p.key}' value='${p.value}'> </c:forEach> </form> 

Thymeleaf:

 <form> <input th:each="var : ${#vars}" type='hidden' name="${var.key}" value="${var.value}"> </form> 
+1
source

It’s just that you can iterate using the foreach Jstl tag.

  <c:forEach items="${requestScope}" var="var"> <c:if test="${ !var.key.startsWith('javax.') && !var.key.startsWith('org.springframework')}"> <input type="hidden" name="${var.key}" value="${var.value}" /> </c:if> </c:forEach> 

Request attributes from the spring framework and from the servlet have prefixes, you do not need to add a prefix to your request attributes.

Rather, you can ignore all those attributes that have the prefix "org.springframework" or "javax.".

0
source

You can try the following:

 @RequestMapping(value = "/method", method = RequestMethod.GET) public String myMethod(final Model model, final HttpServletRequest request, final HttpServletResponse response, final Principal principal) ... //Create list for param names and another list for param values List<String> paramNames = new ArrayList(); List<String> paramValues = new ArrayList(); paramNames.add("paramName1"); paramValues.add("paramValue1"); paramNames.add("paramName2"); paramValues.add("paramValue2"); //paramValue1 is the value corresponding to paramName1 and so on... //add as many param names and values as you need ... //Then add both lists to the model model.addAttribute("paramNames", paramNames); model.addAttribute("paramValues", paramValues); 

Then in JSP you can iterate over the paramNames list and use varStatus.index to get the index of the current iteration round and use it to pull the value of the corresponding parameter value from the paramValues ​​list. Like this -

  <form id='f' name='myform' method='POST' action='/path/to/servlet'> <c:forEach items="${paramNames}" var="paramName" varStatus="status"> <input type='hidden' name='${paramName}' value='${paramValues[status.index]}'> </c:forEach> </form> 

You can add other input elements to the form as needed, but the above should generate all hidden input elements for each of the parameters that you set in the Model.

-1
source

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


All Articles