0

I am unable to get the ModelAttribute for second request. My first request is initForm() method I prepared Command object and able to display the command in jsp.

Through initForm() I am populating command and that command I want in editForm when I will do ajax call.

Here is my spring form

<form:form method="POST" action="addstudentdetails.htm" commandName="command">
 Ignore what is inside this 

 Name: Shoaib Age:23  <a href="#" onclick="editstudentdetails(1,0)">edit</a>

</form:form>

My ajax request:

function editStudentDetails(studentId,index){
       $.ajax(
        {url:"editstudentdetails.htm",
         method:"GET",
         data:{"action":"edit","id":studentId,"index":index},
            success: function(data) {
                   jQuery("#studentDetailsDiv").html(data)
            }

        }

      )
   }

In editStudentDetails() method I have method ajax call to go editForm() of the controller.

Here is my controller:

@Controller

public class StudentDetailsController {

@Autowired
private StudentDetailsDAO studentDetailsDAO;

@RequestMapping(value="/studentdetails.htm",method = RequestMethod.GET)
public String initForm(HttpServletRequest request,ModelMap map){
    String action=request.getParameter("action");
    StudentDetailsCommand command=new StudentDetailsCommand();
    System.out.println("in controller"+action);
    command.setStudents(studentDetailsDAO.findAll());
    map.addAttribute("command", command);

    return "studentdetails";
}

@RequestMapping(value="/editstudentdetails.htm",method = RequestMethod.GET)
public String editForm(ModelMap map,HttpServletRequest request){
  map.addObject("index", request.getParameter("index"));
    StudentDetailsCommand command=(StudentDetailsCommand)map.get("command");
  System.out.println(command);
  System.out.println(command.getStudents());//NullPointerException here.
  map.addObject("command", command);
    return "studentdetails";
 }
}

Even tried @ModelAttribute("studentDetailsCommand") but didn't worked.

I am new to Spring 3.0 and I followed all solutions which are given here but nothing worked.Can anyone help me out please?

4
  • What is the second request? What model attribute are you talking about? Commented Jan 11, 2014 at 19:00
  • second request made by ajax call which comes in editForm method and i getting it through map.get("xxx") Commented Jan 11, 2014 at 19:01
  • When i click on edit link it is ajax request which i send.Let me add that ajax method too. Commented Jan 11, 2014 at 19:09
  • Updated my question.Am I clear to u?? Commented Jan 11, 2014 at 19:11

1 Answer 1

1

Model attributes only exist during the life cycle of one HttpServletRequest. Consider reading my answer here.

In your initForm method, you do the following

map.addAttribute("command", command);

this add an attribute named command to the model attributes. This attribute will eventually find its way into the HttpServletRequest attributes and be available to your JSP. In here

<form:form [...] modelAttribute="studentDetailsCommand" commandName="command">

first of all, modelAttribute and commandName have the same purpose, ie. to find an attribute in the model. If you remove commandName you will get an Exception because there is no model attribute named studentDetailsCommand. Here your commandName's value is overwriting your modelAttribute's value.

When the Servlet container is finished rendering your JSP, the rendered content is sent as the body of the HTTP response. At this point, the request has been handled and the HttpServletRequest and the model attributes are garbage collected.

When you send your new request through AJAX, there is no longer any model attribute named studentDetailsCommand (there actually never was).

Consider using Flash Attributes.

Related:

Sign up to request clarification or add additional context in comments.

12 Comments

So can u tell me how i can solve my problem.Should i remove my modelAttribute from form or change it to "command".Please check my updated question which i followed your suggestion.
@ShoaibChikate Yes, use either modelAttribute or commandName, not both. As I stated in my answer, look into flash attributes.
What happening here is initForm is populating attributes and when another request gets starts then command is not their in the modelMap....so shud i add command as flashAttribute in initForm()
@ShoaibChikate That will make it available in the next request. If that is what you want to do, then yes.
@ShoaibChikate That's what RedirectAttributes and FlashMap does already. The RedirectAttributes stores your flash attributes in a ModelMap which is merged with the HttpServletRequest attributes. That ModelMap is also used to generate a FlashMap which is stored in the HttpSession and retrieved in the next request.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.