gpt4 book ai didi

java - BindingResult 和 bean 名称 'student' 的普通目标对象都不能作为请求属性 - Spring MVC

转载 作者:行者123 更新时间:2023-12-01 13:41:45 25 4
gpt4 key购买 nike

首先,我想说,我已经在 SO(和其他地方)上阅读了许多有关此错误的问题和答案,但对这个特定案例没有帮助。

我的简单 Spring MVC Web 应用程序的场景是这样的:用户根据 String 属性 indexNumber 搜索学生 (searchStudent.jsp)。 indexNumber 由用户输入的其他 3 个属性组成:programId、rollNumber、rollYear。例如,如果programId =“M5”,rollNumber =“10”和rollYear =“2013”​​,则indexNumber将为“M5 10/13”。这是在 View 中完成的。 indexNumber是每个学生的唯一标识符,但不是PK。重要提示:搜索学生并加载适当的页面本身就可以正常工作。

如果学生不存在,addAfterSearchStudent.jsp 页面应该加载用户在 searchStudent.jsp 中输入的预填充值(programId、rollNumber、indexNumber) - 我不想让用户输入此数据两次。我编写了下面的代码,我认为它会完全按照我在此处描述的方式进行操作,但我收到了上述错误。当我对这些属性进行硬编码并尝试保存数据时,这也可以正常工作。我相信问题在于将参​​数从一个jsp传递到另一个jsp。

这些是我的 Controller 的处理程序方法:

    /**
* Handles and retrieves the search student page
*/
@RequestMapping(value="/search", method = RequestMethod.GET)
public String getSearchStudent(Model model) {

logger.debug("Received request to show search student page");

model.addAttribute("index", new Student());
model.addAttribute("programList", programService.getAll());

return "searchStudent";
}

/**
* Searches for a student by delegating the processing to StudentService.
*/
@RequestMapping(value="/search", method = RequestMethod.POST)
public String postSearchStudent(@RequestParam(value = "program.programId") String programId,
@RequestParam(value = "rollNumber") String rollNumber,
@RequestParam(value = "rollYear") String rollYear,
@RequestParam(value = "indexNumber") String indexNumber,
Model model) {

logger.debug("Received request to search for a student");

model.addAttribute("program.programId", programId);
model.addAttribute("rollNumber", rollNumber);
model.addAttribute("rollYear", rollYear);
model.addAttribute("indexNumber", indexNumber);

// Call StudentService to do the actual searching
Student student = studentService.search(indexNumber);

// if student isn't in the db load addAfterSearchStudent.jsp, else load student.jsp
if (student == null)
return "addAfterSearchStudent";
else
return "student";
}

/**
* Handles and retrieves the add (after search) student page
*/
@RequestMapping(value="/addSearch", method = RequestMethod.GET)
public String getAddSearchStudent(@RequestParam(value = "program.programId") String programId,
@RequestParam(value = "rollNumber") String rollNumber,
@RequestParam(value = "rollYear") String rollYear,
@RequestParam(value = "indexNumber") String indexNumber,
Model model) {

logger.debug("Received request to show add (after search) student page");

Student student = new Student();
student.setRollNumber(rollNumber);
student.setRollYear(rollYear);
student.setIndexNumber(indexNumber);
Program program = programService.get(programId);
student.setProgram(program);
model.addAttribute("student", student);

return "addAfterSearchStudent";
}

/**
* Adds a new student after search by delegating the processing to StudentService.
* Displays a confirmation JSP page
*/
@RequestMapping(value="/addSearch", method = RequestMethod.POST)
public String postAddSearchStudent(@ModelAttribute("student") Student student) {

logger.debug("Received request to add new student after search");

// Call StudentService to do the actual adding
studentService.addAfterSearch(student);

// resolves to /WEB-INF/jsp/success/addStudentSuccess.jsp
return "/success/addStudentSuccess";
}

这是 searchStudent.jsp:

<c:url var="searchUrl" value="/essays/main/student/search" />
<form:form modelAttribute="index" method="POST" action="${searchUrl}">
...

这是 addAfterSearchStudent.jsp:

<c:url var="saveUrl" value="/essays/main/student/addSearch" />
<form:form commandName="student" method="POST" action="${saveUrl}">
...

我知道这很长,但我想呈现所有相关信息。有人知道我在哪里犯了错误吗?

最佳答案

<form:form commandName="student" ...>

您的 addAfterSearchStudent.jsp 需要一个名为“student”的对象。在您的 postSearchStudent() 方法中,您不会向模型中添加一个。

if (student == null)
return "addAfterSearchStudent";

您需要创建一个 Student 对象并添加它,就像在 getAddSearchStudent() 方法中执行的操作一样。

    Student student = new Student();
student.setRollNumber(rollNumber);
student.setRollYear(rollYear);
student.setIndexNumber(indexNumber);
Program program = programService.get(programId);
student.setProgram(program);
model.addAttribute("student", student);

关于java - BindingResult 和 bean 名称 'student' 的普通目标对象都不能作为请求属性 - Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com