2014-01-11 4 views
0

두 번째 요청에 대해 ModelAttribute를 가져올 수 없습니다. 첫 번째 요청은 initForm() 메소드입니다. Command 객체를 준비했고 jsp에서 명령을 표시 할 수있었습니다.봄에 modelAttribute를 채울 수 없습니다.

initForm()을 통해 나는 ajax 호출을 수행 할 때 editForm에서 원하는 명령과 명령을 채 웁니다. 내가 컨트롤러의 editForm()을 이동하는 방법 아약스 호출이

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

     } 

    ) 
    } 

방법 editStudentDetails()에서 :

여기 내 봄 양식

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

내 아약스 요청입니다.

@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"; 
} 
} 

심지어 @ModelAttribute ("studentDetailsCommand")을 시도했지만 작동하지 않았다 :

여기 내 컨트롤러입니다.

저는 스프링 3.0을 처음 접했고 여기에 제공된 모든 해결책을 따랐지만 아무 것도 효과가 없었습니다. 아무도 도와 주실 수 없습니까?

+0

두 번째 요청은 무엇입니까? 어떤 모델 속성에 대해 이야기하고 있습니까? –

+0

editForm 메서드에서 오는 아약스 호출에 의해 두 번째 요청이 map.get ("xxx") –

+0

통해 얻는 편집 링크를 클릭하면 아약스 요청을 내가 send.Let 날 그 아약스 메서드를 너무 추가 할 수 있습니다. –

답변

1

모델 속성은 하나의 수명주기 동안 만 존재합니다. HttpServletRequest. my answer here을 읽는 것을 고려하십시오. 당신의 initForm 방법에서

, 당신은이 모델 속성에 command라는 속성을 추가

map.addAttribute("command", command); 

를 다음을 수행합니다. 이 속성은 결국 HttpServletRequest 속성으로 들어가 JSP에 사용할 수있게됩니다. 여기

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

우선으로, modelAttributecommandName 즉, 동일한 목적을 가지고있다. 모델에서 속성을 찾으십시오. commandName을 제거하면 studentDetailsCommand이라는 모델 속성이 없기 때문에 예외가 발생합니다. 여기서 commandName의 값이 modelAttribute의 값을 덮어 씁니다.

Servlet 컨테이너에서 JSP 렌더링이 완료되면 렌더링 된 내용이 HTTP 응답 본문으로 전송됩니다. 이 시점에서 요청이 처리되고 HttpServletRequest 및 모델 특성이 가비지 수집됩니다.

AJAX를 통해 새 요청을 보내면 studentDetailsCommand이라는 모델 속성이 없습니다. 실제로는 없었습니다.

Flash Attributes을 사용해보십시오.

관련 :

+0

그래서 내 문제를 해결할 수있는 방법을 말해 줄 수 있어요. 형식에서 내 modelAttribute를 제거하거나 "명령"으로 변경해야합니까? 제 제안에 따라 업데이트 된 질문을 확인하십시오. 좋은 설명을 위해서 –

+0

+1. –

+0

@ShoaibChikate 네,'modelAttribute' 또는'commandName'을 사용하십시오. 둘 다 사용할 수는 없습니다. 내 답변에 명시된 바와 같이 플래시 속성을 살펴보십시오. –

관련 문제