Spring 验证没有抛出任何错误
Spring validation is not throwing any error
我尝试了所有可能的方法来修复它,但我失败了。所以,我让我的控制器工作,我也编写了代码来处理验证,但即使我发送一些无效的 data.I 也不会抛出任何错误,现在只是将 @notNull 和 @notEmpty 验证应用到 test.Here 是我的代码。请帮我找出问题所在?
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;
}
}
解决方案:
从评论中我发现我的配置文件丢失了:
<mvc:annotation-driven />
当我将这一行添加到代码中时,它抛出了一个错误:
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
为了解决这个错误,我需要在配置文件中添加几行:
<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>
谢谢大家
您需要有一个绑定结果来检查是否有错误。
快速 google 给了我这个例子:
http://codetutr.com/2013/05/28/spring-mvc-form-validation/
从第一次检查您的代码开始,您如何传递要验证的详细信息,查看您的表单,您没有设置文本字段供用户传递任何值.....如果我 运行 您在文本编辑器中的表单不存在任何字段,您是否尝试过将输入类型设置为文本。类型="text"
例如...
解决方案:
这是对我有用的解决方案。
从评论中我发现我的配置文件丢失了:
<mvc:annotation-driven />
当我将这一行添加到代码中时,它抛出了一个错误:
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
为了解决这个错误,我需要在配置文件中添加几行:
<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>
谢谢大家
我尝试了所有可能的方法来修复它,但我失败了。所以,我让我的控制器工作,我也编写了代码来处理验证,但即使我发送一些无效的 data.I 也不会抛出任何错误,现在只是将 @notNull 和 @notEmpty 验证应用到 test.Here 是我的代码。请帮我找出问题所在?
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;
}
}
解决方案:
从评论中我发现我的配置文件丢失了:
<mvc:annotation-driven />
当我将这一行添加到代码中时,它抛出了一个错误:
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
为了解决这个错误,我需要在配置文件中添加几行:
<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>
谢谢大家
您需要有一个绑定结果来检查是否有错误。 快速 google 给了我这个例子: http://codetutr.com/2013/05/28/spring-mvc-form-validation/
从第一次检查您的代码开始,您如何传递要验证的详细信息,查看您的表单,您没有设置文本字段供用户传递任何值.....如果我 运行 您在文本编辑器中的表单不存在任何字段,您是否尝试过将输入类型设置为文本。类型="text"
例如...
解决方案:
这是对我有用的解决方案。
从评论中我发现我的配置文件丢失了:
<mvc:annotation-driven />
当我将这一行添加到代码中时,它抛出了一个错误:
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
为了解决这个错误,我需要在配置文件中添加几行:
<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>
谢谢大家