Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send parameters to JSP and get them back updated in Spring MVC
    primarykey
    data
    text
    <p>I'm new to Spring MVC and JSP developing, so please be gentle telling me I'm doing something stupid :P Told that, the question:</p> <p>I studied a bit Spring MVC and got a nice piece of working code. Now I'm stuck on an apparently stupid problem: it seems I cannot get the objects I pass to the JSP back in the controller. What I need to do is: the user fills in two textbox, then he clicks on a button and the system should query the db, find the element the user searched for, add the element found to the array "capitolospesas" and re-render the jsp with all the objects in the array (previous + the one just added).</p> <p>The cycle I created is: controller ---> jsp ---> controller ---> jsp. The mapping "newProposte" initialises the page newEditProposte.jsp with blank values, the jsp correctly gets the values to display, the user edits/fills in what is needed and clicks the submit button. The control passes to the method "selectCapitoloSpesaFromProposte" where the system should perform the query and add the result to the array "capitolospesas", but it's always empty when I add the new object, so on the page it displays only the last element found.</p> <pre><code>@RequestMapping("/newProposte") public ModelAndView newProposte() { System.out.println("ProposteController.newProposte()"); ModelAndView mav = new ModelAndView(); mav.addObject("proposte", new ProposteWeb()); mav.addObject("newFlag", true); mav.addObject("capitolospesas", new HashSet&lt;Capitolospesa&gt;()); mav.setViewName("proposte/newEditProposte.jsp"); return mav; } @RequestMapping("/selectCapitoloSpesaFromProposte") public ModelAndView selectCapitoloSpesaFromProposte(@ModelAttribute ProposteWeb proposte, @ModelAttribute HashSet&lt;Capitolospesa&gt; capitolospesas) { ModelAndView mav = new ModelAndView(); mav.addObject("proposte", proposte); mav.addObject("newFlag", true); capitolospesas.add(capitolospesaDAO.findCapitolospesaByCapitoloSpesaCapitoloAndCapitoloSpesaArticolo(proposte.getCapitoloSpesaCapitoloProposte(), proposte.getCapitoloSpesaArticoloProposte())); mav.addObject("capitolospesas", capitolospesas); mav.setViewName("proposte/newEditProposte.jsp"); return mav; } </code></pre> <p>The jsp:</p> <pre><code>&lt;script language="Javascript"&gt; function selectCapitoloSpesa() { alert("selectCapitoloSpesaFromProposte"); document.getElementById("tabProposteForm").action = "${pageContext.request.contextPath}/selectCapitoloSpesaFromProposte"; submitForm(); return true; } function submitForm() { alert("submit"); document.getElementById("tabProposteForm").submit(); return true; } &lt;/script&gt; .... .... the form .... .... the foreach iterating on the array "capitolospesas" showing it in a table .... .... &lt;input type="button" value="selectCapitoloSpesaFromProposte" name=selectCapitoloSpesaFromProposte onclick="return selectCapitoloSpesa();"&gt; </code></pre> <p>I made the post in this way because depending on the button clicked (there are various buttons) I need the form to behave differently.</p> <p>I read all over the various tutorials that to pass parameters to the jsp page I have to add them to the ModelAndView object, and I correctly do this. In the jsp I have access to every object I pass to it, in this case "newFlag", "capitolospesas" and "proposte". What I don't understand is how can I get those objects when they come back FROM the jsp? Until now I used @ModelAttribute annotation before the only parameter I needed to save, edit or delete the only entity I had on a page, but now I need to get multiple objects from the page. In the code pasted above I tried using multiple times the @ModelAttribute annotation, but without success; the object "proposte" is correctly filled with the things typed by the user, but I debugged the code to find that everytime the method "selectCapitoloSpesaFromProposte" starts a new array "capitolospesas" is instantiated instead of getting the values from the array I passed earlier to the jsp.</p> <p>If anyone has any suggestions please please post them.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload