SpringMVC model attribute returns null in post

I was developing a trivial SpringMVC 3 application but got stuck somewhere. Essentially, the model attribute whose fields are filled in in the GET operation is returned in the POST NULL field (even if I don't). I checked this and other forums and the only answer I came up with was injecting editors for the classes that I had to put in the model, an initializer that could be used to register custom editors and make it available to the application (in the servletname file -servlet.xml). All the operations I did, but definitely no luck. I was wondering if someone out there could give me a hand. Thank.

Next controller:

@Controller
@RequestMapping(value="/nourish")
public class NourishController {

private PetDAO pdao = new PetDAO();
private UserDAO udao = new UserDAO();
private FeedVirtualPet feedvp = new FeedVirtualPet();

@RequestMapping(method = RequestMethod.GET)
public String nourish(Model model, HttpServletRequest request){
    NourishPetDTO npdto = new NourishPetDTO();
    PetDTO      pdto=pdao.findPetByBusinessKey((PetDTO)request.getSession().getAttribute("pet"));
    npdto.setPet(pdto);
    npdto.setAmt(0);
    model.addAttribute("npdto", npdto);
    return "nourish";
}


@RequestMapping(method = RequestMethod.POST)
public String nourishPOST(@ModelAttribute("npdto") NourishPetDTO npdto,
        //BindingResult result,
        HttpServletRequest request){

    System.out.println("*****nourishPOST.npdto.amt: "+npdto.getAmt());
    System.out.println("*****nourishPOST.npdto.pet.petname: "+npdto.getPet().getPetName());
    System.out.println("*****nourishPOST.npdto.pet.hunger:     "+npdto.getPet().getHunger());
    PetDTO pdto = feedvp.feed(npdto.getPet(), npdto.getAmt());
    request.getSession().setAttribute("user", pdto.getOwner());
    return "redirect:detailPet";
}
}

      

has methods for both GET and POST operations and is linked to the following jsp - in this view, all model data is correctly displayed via EL:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" session="true"  pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Nourish your pet!!</title>
</head>
<body>
Your stats for <h3>${npdto.pet.petName}</h3><br>
    Please note that any value you put inside will:
        <ol>
            <li>Subtract value to your current hunger level</li>
            <li>Add (value) to your current health level</li>
        </ol>
    Please note that any value you'll put will in many manners "resized":
    <ol>
        <li>It must be even. If not, a default 0 value will be  applied</li>
        <li>It can't be greater than 4. If it greater, the maxium value  of 4 will be anyway considered.</li>
        <li>If it ain't a number, a default zero value will be passed</li>
    </ol>
<table>
    <tr><td>Health</td><td>${npdto.pet.health}</td></tr>
    <tr><td>Hunger</td><td>${npdto.pet.hunger}</td></tr>
</table>
<form action="nourish" method="post"  >
    nourishment: <input type="text" name="amt"/>
    <input type="submit" value="Nourish!"/>
</form>
</body>
</html>

      

(Note that I did not use the model attribute returning NULL to make sure I didn’t do anything with it)

POST operations are completed with the command

System.out.println("*****nourishPOST.npdto.pet.petname:"+npdto.getPet().getPetName());

      

because Tomcat is returning NullPointerException.

As mentioned, I was looking for a solution to this problem and all I could find was to add Editor classes and register Editors for the binder. The result is still the same.

Anyway, these are classes:

NourishPetEditor.java

public class NourishPetEditor extends PropertyEditorSupport {

private PetEditor pedit;

public PetEditor getPedit() {
    return pedit;
}


public NourishPetEditor() {
    // TODO Auto-generated constructor stub
    pedit =  new PetEditor();
}

@Override 
public String getAsText(){
    NourishPetDTO npdto= (NourishPetDTO)getValue();
    return super.getAsText()+","+npdto.getAmt();
}

public NourishPetDTO makeNourishPetDTOInstance(String [] parts){

    NourishPetDTO npdto = new NourishPetDTO(); 
    npdto.setPet(pedit.makePetDTOInstance(parts));
    npdto.setAmt(Integer.parseInt(parts[9]));
    return npdto;

}


public void setAsText(String key){
    String []parts = key.split(",");
    NourishPetDTO npdto = makeNourishPetDTOInstance(parts);
    setValue(npdto);
}
}

      

PetEditor.java

package com.virtualpet.dtoeditors;
import java.beans.PropertyEditorSupport;

import com.virtualpet.virtualpet_daos.PetDAO;  
import com.virtualpet.virtualpet_dtos.PetDTO;
import com.virtualpet.virtualpet_dtos.UserDTO;
public class PetEditor extends PropertyEditorSupport{

private PetDAO pdao;

public PetEditor() {
    // TODO Auto-generated constructor stub
}

public PetEditor(PetDAO pdao) {
    // TODO Auto-generated constructor stub
    this.pdao = pdao;

}


public String getAsText(){
    PetDTO pdto = (PetDTO) this.getValue();

    return pdto.getClass().getName()+","+ //0
    pdto.getPetName()+","+ //1
    pdto.getHealth()+","+  //2
    pdto.getHunger()+","+  //3
    pdto.getMood()+","+","+ //4
    pdto.getOwner().getClass().getName()+","+ //5
    pdto.getOwner().getUsername()+","+ //6
    pdto.getOwner().getEmail()+","+pdto.getOwner().getPassword(); //7,8

}

public void setAsText(String key) throws IllegalArgumentException {
    String []parts = key.split(",");
    PetDTO pdto = makePetDTOInstance(parts);
    setValue(pdto);
}


public UserDTO makeUserDTOInstance(String[] parts)
        throws InstantiationException, IllegalAccessException,
        ClassNotFoundException {

            UserDTO udto = (UserDTO)Class.forName(parts[5]).newInstance();
            udto.setUsername(parts[6]);
            udto.setEmail(parts[7]);
            udto.setPassword(parts[8]);
            return udto;
}


public PetDTO makePetDTOInstance(String[]parts){
    try{
        PetDTO pdto = (PetDTO) Class.forName(parts[0]).newInstance();
        pdto.setPetName(parts[1]);
        pdto.setHealth(Integer.parseInt(parts[2]));
        pdto.setHunger(Integer.parseInt(parts[3]));
        pdto.setMood(Integer.parseInt(parts[4]));

        UserDTO udto = makeUserDTOInstance(parts);

        pdto.setOwner(udto);
        return pdto;
    }
    catch (Exception e){
        throw new IllegalArgumentException();
    }
}
}

      

I will spare you UserEditor as it is very similar to PetEditor. Finally, the initializer binds custom editors and model classes.

VirtualPetDTOInitializer.java

package com.virtualpet.dtoeditors;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

import com.virtualpet.virtualpet_dtos.NourishPetDTO;
import com.virtualpet.virtualpet_dtos.PetDTO;
import com.virtualpet.virtualpet_dtos.UserDTO;

public class VirtualPetDTOInitializer implements WebBindingInitializer {

public void initBinder(WebDataBinder binder, WebRequest arg1) {
    // TODO Auto-generated method stub
        binder.registerCustomEditor(UserDTO.class, new UserEditor( ));
        binder.registerCustomEditor(PetDTO.class, new PetEditor( ));
        binder.registerCustomEditor(NourishPetDTO.class, new NourishPetEditor());
}
}

      

This class defines the property value in dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<bean id="userDao"
    class="com.virtualpet.virtualpet_daos.UserDAO"/>
<bean id="petDao"
    class="com.virtualpet.virtualpet_daos.PetDAO" />
<bean    class="org.springframwork.web.servlet.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="com.virtualpet.dtoeditors.VirtualPetDTOInitializer"/>
    </property>
</bean>
</beans>

      

As a complete newbie to Spring MVC, I must tell you that this error is what I got even before I implemented these classes. So it looks like they are not a factor and yet their implementation is all I could find, the aboud model attribute returned null after POST. Can anyone please help? Thank you in advance.

+3


source to share


1 answer


You need to do one of the following:

  • Save values npdto

    in hidden forms

  • Save npdto

    in session

  • Reread npdto

    from database in message handler

You probably want # 2, in which case add @SessionAttributes("npdto")

on top of your controller.



You should also add a parameter SessionStatus

to your message handler and call sessionStatus.complete()

to clear the item from the session when you no longer need it.

See Spring MVC: validation, post-redirect-fetch, partial updates, optimistic Concurrency, field guards for a reference answer.

+2


source







All Articles