我無法爲第二個請求獲取ModelAttribute。 我的第一個請求是initForm()方法,我準備了Command對象並能夠在jsp中顯示該命令。無法在spring中填充modelAttribute
通過initForm()我填充命令和那個命令,我想在editForm,當我會做ajax調用。
這裏是我的春天形式
<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>
我的Ajax請求:
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()
方法,我有方法Ajax調用去控制器的editForm()
。
這裏是我的控制器:
@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」),但沒有奏效。
我是Spring 3.0的新手,我遵循這裏給出的所有解決方案,但沒有任何成果。可以幫助我嗎?
什麼是第二個請求?你在說什麼模型屬性? –
由ajax調用的第二個請求,它來自editForm方法,我通過map.get(「xxx」)得到它 –
當我點擊編輯鏈接時,它是我發送的ajax請求。讓我添加ajax方法。 –