Question

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:

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).

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.

@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<Capitolospesa>());

    mav.setViewName("proposte/newEditProposte.jsp");

    return mav;
}

@RequestMapping("/selectCapitoloSpesaFromProposte")
public ModelAndView selectCapitoloSpesaFromProposte(@ModelAttribute ProposteWeb proposte, @ModelAttribute HashSet<Capitolospesa> 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;
}

The jsp:

<script language="Javascript">       
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;
}
</script>
....
....
the form
....
....
the foreach iterating on the array "capitolospesas" showing it in a table
....
....
<input type="button" value="selectCapitoloSpesaFromProposte"    name=selectCapitoloSpesaFromProposte    onclick="return selectCapitoloSpesa();">

I made the post in this way because depending on the button clicked (there are various buttons) I need the form to behave differently.

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.

If anyone has any suggestions please please post them.

Was it helpful?

Solution

When the JSP is sent to the client (ie. rendered in to HTML), the ONLY data that is preserved is that which is in the resulting HTML document. No Java objects exist, only text - which can be returned to the server as a form submission. So the JSP may access java objects, but once it has generated the web page, it is forgotten. Remember: a web page is just a text document, and all that is exchanged between web browser and server is text. Java objects don't get passed over.

To preserve a java object (which you can amend when the form is submitted), you must store it elsewhere. Specifically you would use the session object to store the object directly, or to store some other pointer to it (eg. a login name that you can use to reference a database entry, or a file name to read from etc.).

To use the session value in a normal servlet do something like:

HttpSession session = request.getSession(true);
session.setAttribute( "keyForObject", myObject );

to store the object. . . then

HttpSession session = request.getSession(true);
myObject = (ObjectType)session.getAttribute( "keyForObject" );

to retrieve it.

In a Spring RequestMapping, you just need to add a HttpSession parameter to get the session object:

@RequestMapping
public void myMethod(... HttpSession session)

OTHER TIPS

You have to save the object in your controller, either in a session variable or a database (or other persistent store). There's no way you'll get it back from the ModelAndView object. That object was sent to the JSP and was garbage collected afterwards. You're in a new request, there's no way you can access model objects from a previous request unless you save them somewhere.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top