I tried every possible way to fix it but I failed. So, I have my controller working and I have also written code to handle validation but It is not throwing any error even if I send some invalid data.I have just applied @notNull and @notEmpty validation for now to just test.Here is my code. Please help me to find out what is wrong with it ?
registerForm.jsp
<form:form method="POST" action="addRegistration" commandName="regForm">
<table>
<tr>
<td><form:label path="name">Name : </form:label></td>
<td><form:input path="name" /></td>
<td align="left"><form:errors path="name" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="age">Age : </form:label></td>
<td><form:input path="age" /></td>
<td align="left"><form:errors path="age" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="email">Email : </form:label></td>
<td><form:input path="email" /></td>
<td align="left"><form:errors path="email" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="refer">Refer : </form:label></td>
<td><form:input path="refer" /></td>
<td align="left"><form:errors path="refer" cssClass="error"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
SpringFormDem-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="registration" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
</bean>
<bean id="myBeansValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</beans>
RegisterationController.java
@Controller
public class RegistartionController {
@RequestMapping(value="/registrationForm", method = RequestMethod.GET)
public String registrationForm(Model model)
{
model.addAttribute("regForm", new RegistationDetails());
//return new ModelAndView("registrationForm", "command", new RegistationDetails());
return "registrationForm";
}
@RequestMapping(value = "/addRegistration", method = RequestMethod.POST)
public String addRegistration(@ModelAttribute("regForm") @Valid RegistationDetails register, BindingResult result, ModelMap model)
{
String ret;
System.out.println(result.toString());
if(result.hasErrors())
{
ret = "registrationForm";
}
else
{
model.addAttribute("name", register.getName());
model.addAttribute("age", register.getAge());
model.addAttribute("email", register.getEmail());
model.addAttribute("refer", register.getRefer());
ret = "displayResult";
}
return ret;
}
}
RegistartionDetail.java
public class RegistationDetails {
@NotNull(message = "Your name can not be null")
@NotEmpty(message = "Your name can not be null")
private String name;
@NotNull(message = "Your email can not be null")
@NotEmpty(message = "Your email can not be null")
private String email;
@NotNull(message = "Your refer can not be null")
@NotEmpty(message = "Your refer can not be null")
private String refer;
@NotNull(message = "Your age can not be null")
@NotEmpty(message = "Your age can not be null")
private String age;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the refer
*/
public String getRefer() {
return refer;
}
/**
* @param refer the refer to set
*/
public void setRefer(String refer) {
this.refer = refer;
}
/**
* @return the age
*/
public String getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(String age) {
this.age = age;
}
}
SOLUTION :
From the comment I found out that my configuration file was missing :
<mvc:annotation-driven />
When I added this line to the code then it threw me an error:
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 30; The prefix "mvc" for element "mvc:annotation-driven" is not bound
To solve this error I required to add few lines to configuration file :
<beans
...
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
...
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
</beans>
Thank you everyone.
SOLUTION :
Here is the solution that worked for me.
From the comment I found out that my configuration file was missing :
<mvc:annotation-driven />
When I added this line to the code then it threw me an error:
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 30; The prefix "mvc" for element "mvc:annotation-driven" is not bound
To solve this error I required to add few lines to configuration file :
<beans
...
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
...
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
</beans>
Thank you everyone.