【发布时间】:2014-01-30 17:42:32
【问题描述】:
我无法获取第二个请求的 ModelAttribute。 我的第一个请求是 initForm() 方法,我准备了 Command 对象并能够在 jsp 中显示命令。
当我进行 ajax 调用时,我通过 initForm() 填充命令和我想要在 editForm 中的命令。
这是我的春季表格
<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 方法。
-
更新了我的问题。我清楚吗??
标签: java spring spring-mvc controller modelattribute