S
S
Stancialeta2016-07-08 16:38:55
Java
Stancialeta, 2016-07-08 16:38:55

How to get array from Spring when loading JSP page?

You need to write the array to a variable when the page loads. The code below doesn't work:

<c:set var="array" value="${pageContext.request.contextPath}/spring_link/getarray"/>

home.jsp content:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<c:set var="array" value="${pageContext.request.contextPath}/spring_link/getarray"/>
</body>
</html>

Controller content:
@RequestMapping(value = "/getarray", method = RequestMethod.GET)
    public String getArray(ModelMap model) {

        Map<String, Class>  array = Class.getArray();
        model.put("array ", "array");
        return "home";
    }

Way controller -> home.jsp do not suggest. Scripts can't be used either.
Since you need to get exactly the java collection, not JSON, the use of Ajax is out of place along the way.
As I understand it, my task is contrary to the MVC concept, but my mentor insisted on just such an approach.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Kosarev, 2016-07-09
@Stancialeta

This:
creates an array variable in the JSP with a string equal to ${pageContext.request.contextPath}/spring_link/getarray
Even more, it is impossible to get data from controllers from a view returned by a non-Spring WebMVC controller (more precisely, it is possible, but these are crutches). In standard WebMVC, the controller decides which view to give back and what data to pass to the view via models. This is not JSF / CDI for you, although if you really want to, you can cross JSF / CDI with Spring, but these are also perversions.
There is only one correct option - the controller returns the desired view and passes the necessary data to it.
PS: why solve problems in ways that contradict established concepts?

M
Mikhail Ivanov, 2016-07-09
@Mishany

Handles data from Spring like this:

<c:forEach items="${arrays}" var="array"> //принимаем массив 
    ${array.String}
    ${array.Class.(поля)}
 </c:forEach>

From the controller send:
@RequestMapping(value = "/getarray", method = RequestMethod.GET)
    public String getArray(ModelMap model) {

        Map<String, Class>  array = Class.getArray();
        model.addAttribute("arrays ", array); // передаем в jsp массив
        return "home";
    }

I hope I gave you exactly what you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question