spring MVC 使用validation验证无效果
packagecom.liked.controller;importjavax.validation.Valid;importorg.springframework.st...
package com.liked.controller;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.liked.pojo.Customer;
@Controller
@RequestMapping("/customer")
public class SignUpController {
@RequestMapping(value = "/signup.h", method = RequestMethod.POST)
public String addCustomer(
@Valid Customer customer,
BindingResult result)throws Exception {
if (result.hasFieldErrors()) {
return "pages/SignUpForm";
} else {
return "pages/Done";
}
}
@RequestMapping(value="/form.h")
public String displayCustomerForm(ModelMap model)throws Exception {
model.addAttribute("customer", new Customer());
return "pages/SignUpForm";
}
}
--------------------------------------------
public class Customer {
@NotEmpty
String name;
@Range(min = 1, max = 150)
int age;
--------------------------------------
<form:form method="POST" commandName="customer" action="signup.h">
<form:errors path="*" cssClass="errorblock" element="div" />
<table>
<tr>
<td>Customer Name :</td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssClass="error" /></td>
</tr>
<tr>
<td>Customer Age :</td>
<td><form:input path="age" /></td>
<td><form:errors path="age" cssClass="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
------------------------------------------------
在表单中无论是否输入正确还是错误的值都会正常通过,晕了,是jar冲突问题吗? 展开
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.liked.pojo.Customer;
@Controller
@RequestMapping("/customer")
public class SignUpController {
@RequestMapping(value = "/signup.h", method = RequestMethod.POST)
public String addCustomer(
@Valid Customer customer,
BindingResult result)throws Exception {
if (result.hasFieldErrors()) {
return "pages/SignUpForm";
} else {
return "pages/Done";
}
}
@RequestMapping(value="/form.h")
public String displayCustomerForm(ModelMap model)throws Exception {
model.addAttribute("customer", new Customer());
return "pages/SignUpForm";
}
}
--------------------------------------------
public class Customer {
@NotEmpty
String name;
@Range(min = 1, max = 150)
int age;
--------------------------------------
<form:form method="POST" commandName="customer" action="signup.h">
<form:errors path="*" cssClass="errorblock" element="div" />
<table>
<tr>
<td>Customer Name :</td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssClass="error" /></td>
</tr>
<tr>
<td>Customer Age :</td>
<td><form:input path="age" /></td>
<td><form:errors path="age" cssClass="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
------------------------------------------------
在表单中无论是否输入正确还是错误的值都会正常通过,晕了,是jar冲突问题吗? 展开
展开全部
建议你在后台验证,用struts2的拦截器来配置,再写个Action类,
写好属性,做好get/set操作;
public String execute() throws Exception {
if("验证值1".equals(属性名1) && "验证值2".equals(属性名2)){
return "success";
}
return "flase";
}
希望对你有帮助哈。谢谢。。。。
写好属性,做好get/set操作;
public String execute() throws Exception {
if("验证值1".equals(属性名1) && "验证值2".equals(属性名2)){
return "success";
}
return "flase";
}
希望对你有帮助哈。谢谢。。。。
追问
我用的是springMVC 抱歉我不能选你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询