Reputation: 459
I'm Learning Spring validation .Though it may be duplicate but i do not know why this error is coming again again.
I checked my code with a lot of examples my code is ok but still it gives exception. How I can remove this exception.
Error is
Neither BindingResult nor plain target object for bean name 'validateform' available as request attribute
My Controller
@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>
spring-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>
Entity Class
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 + "]";
}
}
Upvotes: 1
Views: 376
Reputation: 28519
When you're using a form:form
tag, the command object must already be available in the request, when the jsp page is rendered.
You can use a @ModelAttribute
method and @SessionAttributes
to alleviate your issue. Usually, @ModelAttribute
mehtod is invoked on every request, but if model attribute is already available in the session attribute store it will be picked up from there. This suits your scenario, the code would be something like
@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
}
Upvotes: 1