我正在学习Spring验证。虽然它可能重复,但我不知道为什么会再次出现此错误。
我用很多例子检查了我的代码我的代码没问题,但它仍然给出了异常。我如何删除此异常。
错误是
Neither BindingResult nor plain target object for bean name 'validateform' available as request attribute
我的控制器
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value="/")
public String homepage(){
return "home";
}
@RequestMapping(value="login", method=RequestMethod.POST)
public String submitForm(@ModelAttribute("validateform") @Valid FormEntities validateform, BindingResult result, ModelMap model) {
if(result.hasErrors()) {
return "home";
}
model.addAttribute("message", "Successfully saved person: " + validateform.toString());
return "home";
}
}
Jsp Page
<form:form action="login" commandName="validateform" method="post">
<label for="nameInput">Name: </label>
<form:input path="name" placeholder="Name"/>
<form:errors path="name" cssclass="error" />
<br />
<label for="ageInput">Age: </label>
<form:input path="age" placeholder="Age" />
<form:errors path="age" cssclass="error" />
<br />
<label for="phoneInput">Phone: </label>
<form:input path="phone" placeholder="Phone NO"/>
<form:errors path="phone" cssclass="error" />
<br />
<label for="emailInput">Email: </label>
<form:input path="email" placeholder="Email "/>
<form:errors path="email" cssclass="error" />
<br />
<label for="birthdayInput">Birthday: </label>
<form:input path="birthday" placeholder="MM/DD/YYYY" />
<form:errors path="birthday" cssclass="error" />
<br />
<label for="genderOptions">Gender: </label>
<form:select path="gender" >
<form:option value="">Select Gender</form:option>
<form:option value="MALE">Male</form:option>
<form:option value="FEMALE">Female</form:option>
</form:select>
<form:errors path="gender" cssclass="error" />
<br />
<label for="newsletterCheckbox">Newsletter? </label>
<form:checkbox path="receiveNewsletter" id="newsletterCheckbox" />
<form:errors path="receiveNewsletter" cssclass="error"></form:errors>
<br />
<br />
<input type="submit" value="Submit" />
</form:form>
弹簧context.xml中
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.mine.formValidation" />
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basename" value="/WEB-INF/messages"></beans:property>
</beans:bean>
实体类
public class FormEntities {
@Size(min=5, max=30)
private String name;
@NotEmpty @Email
private String email;
@NotNull @Min(13) @Max(110)
private Integer age;
@Size(min=11)
private String phone;
@NotNull
private Gender gender;
@DateTimeFormat(pattern="MM/dd/yyyy")
@NotNull @Past
private Date birthday;
private Boolean receiveNewsletter;
public enum Gender {
MALE, FEMALE
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Boolean getReceiveNewsletter() {
return receiveNewsletter;
}
public void setReceiveNewsletter(Boolean receiveNewsletter) {
this.receiveNewsletter = receiveNewsletter;
}
@Override
public String toString() {
return "Subscriber [name=" + name + ", email=" + email + ", age=" + age
+ ", phone=" + phone + ", gender=" + gender + ", birthday="
+ birthday + ", receiveNewsletter=" + receiveNewsletter + "]";
}
}
答案 0 :(得分:1)
当您使用form:form
标记时,在呈现jsp页面时,命令对象必须已在请求中可用。
您可以使用@ModelAttribute
方法和@SessionAttributes
来缓解您的问题。通常,每次请求都会调用@ModelAttribute
mehtod,但如果会话属性存储中已有model属性,则会从那里获取它。这适合您的场景,代码类似于
@Controller
@SessionAttributes("validateform")
public class HomeController {
// invoked only initially to create the FormEntities object
@ModelAttribute("validateform")
public FormEntities createFormEntities() {
return new FormEntities();
}
... // the rest of your controller
}