gpt4 book ai didi

java - 使用注释验证表单时出现 Spring mvc 异常

转载 作者:行者123 更新时间:2023-12-02 06:38:50 26 4
gpt4 key购买 nike

我是 spring mvc 的新手。我创建了示例应用程序,该应用程序使用字段名称、年龄、ID 创建表单。 Controller 方法是

@RequestMapping(value = "/", method = RequestMethod.GET)

public ModelAndView student() {
return new ModelAndView("home", "command", new Student());
}
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@Valid Student student,BindingResult result, Model model) {

if(result.hasErrors()) {
return "home";
}

model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());

return "result";
}

我的模型类Student.java

public class Student {
private Integer age;
@NotEmpty @Email
private String name;
private Integer id;

public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}

当我输入正确的数据(即电子邮件 ID)时,它工作正常,但当我输入无效的电子邮件或 NULL 时,它会给出异常。它应该显示适当的错误消息,而不是给出错误。我的看法 Home.jsp 是

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Student Information</h2>
<form:form method="POST" action="/controller/addStudent" >
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
<form:errors path="name" >Invalid Name</form:errors>
</tr>
<tr>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>
</tr>
</table>
</form:form>
</body>
</html>

异常(exception)是:

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 12

9: <form:form method="POST" action="/controller/addStudent" >
10: <table>
11: <tr>
12: <td><form:label path="name">Name</form:label></td>
13: <td><form:input path="name" /></td>
14: <form:errors path="name" >Invalid Name</form:errors>
15: </tr>

根本原因

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute

最佳答案

使用@ModelAttribute注释Student Student:

public String addStudent(@ModelAttribute @Valid Student student,BindingResult result, 
Model model)

编辑:

new Student() 属性名称更改为 student (因为如果将 Student 称为“student”,它会更具可读性):

    @RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("home", "student", new Student());
}

并将 post Controller 方法更改为:

public String addStudent(@ModelAttribute("student") @Valid Student student,BindingResult result, Model model)

现在更改 home.jspform:

<form:form method="POST" action="/controller/addStudent" modelAttribute="student"> 

关于java - 使用注释验证表单时出现 Spring mvc 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19357820/

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