最近学习了一下springmvc中后台校验功能的实现,特别写了一个demo以备以后查看,主要使用注解方式来进行配置。
1、在jsp页面引入spring的标签
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
2、建立数据提交表单
<form:form method="post" action="reg.do" commandName="userBean">
<table>
<tr>
<td>
UserName:
<font color="red"> <form:errors path="userName"></form:errors>
</font>
</td>
</tr>
<tr>
<td>
<form:input path="userName"/>
</td>
</tr>
<tr>
<td>
Age:
<font color="red"> <form:errors path="age"></form:errors>
</font>
</td>
</tr>
<tr>
<td>
<form:input path="age"/>
</td>
</tr>
<tr>
<td>
password:
<font color="red"> <form:errors path="password"></form:errors>
</font>
</td>
</tr>
<tr>
<td>
<form:password path="password"/>
</td>
</tr>
<tr>
<td>
Confirm Password:
<font color="red"> <form:errors path="confirmPassword"></form:errors>
</font>
</td>
</tr>
<tr>
<td>
<form:password path="confirmPassword"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit">
</td>
</tr>
</table>
</form:form>
3、新建表单对象(javabean)
package springapp.model;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;
public class UserBean {
@NotEmpty
@Size(min = 1, max = 20)
private String userName;
@NumberFormat(style = Style.NUMBER)
@NotNull(message = "Age must not be blank")
@Min(value = 1, message = "Age must more then 1")
@Max(value = 100, message = "Age must less then 100")
private Integer age;
@NotEmpty(message = "Password must not be blank.")
@Size(min = 1, max = 10, message = "Password must between 1 to 10 Characters.")
private String password;
@NotEmpty
private String confirmPassword;
/** get and set **/
}
在对表单对象的错误信息提示的时候 我采用了几种方式。userName的错误信息配置在资源文件中(message.properties),如下所示:
NotEmpty.userBean.userName=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A
#用户名不能为空
如果需要把错误提示信息配置在资源文件中,需要在spring-servlet.xml文件对 ReloadableResourceBundleMessageSource这个类进行注册如下:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/message" />
</bean>
Age和password的提示信息就直接写在UserBean中,比如:
@NumberFormat(style = Style.NUMBER)
@NotNull(message = "Age must not be blank")
@Min(value = 1, message = "Age must more then 1")
@Max(value = 100, message = "Age must less then 100")
private Integer age;
对password与Confirm password的校验就写在校验器中。如
package springapp.action;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import springapp.model.UserBean;
@Component("regValidation")
public class RegValidation {
public boolean supports(Class<?> clazz) {
return UserBean.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
UserBean userBean = (UserBean) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName",
"NotEmpty.userBean.userName",
"User Name must not be Empty.");
String userName = userBean.getUserName();
if (!(userBean.getPassword()).equals(userBean.getConfirmPassword())) {
errors.rejectValue("password",
"matchingPassword.userBean.password",
"Password and Confirm Password Not match.");
}
}
}
5、新建处理表单的Action
package springapp.action;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import springapp.model.UserBean;
@Controller
public class UserAction {
@RequestMapping(value = "reg.do", method = RequestMethod.GET)
public String init(Model model) {
UserBean useBean = new UserBean();
model.addAttribute("userBean", useBean);
return "reg";
}
@RequestMapping(value="reg.do",method=RequestMethod.POST)
public String reg(Model model,@Valid UserBean userBean,BindingResult result)
{
regValidation.validate(userBean, result);
if(result.hasErrors())
{
return "reg";
}
model.addAttribute("userBean", userBean);
return "regSuccess";
}
@Autowired
private RegValidation regValidation;
}
访问路径:http://localhost:80/SpringMVCValidation/reg.do
分享到:
相关推荐
4.2 报名流程:用户选择考试类型,填写报名信息,系统进行初步校验,通过后跳转至支付页面,完成支付后保存报名记录,后台进行审核确认。 4.3 数据库设计:根据业务需求设计合理的数据库表结构,包括用户表、考试...
##### 3.2 SpringMVC SpringMVC是Spring框架的一个模块,专门用于Web应用的开发。它实现了MVC(Model-View-Controller)模式,将Web应用分为模型层、视图层和控制器层,有助于保持代码的清晰和可维护性。 ##### ...
- **技术选型**:采用SpringBoot框架作为后端开发基础,结合SSM(Spring、SpringMVC、MyBatis)框架,实现高效稳定的后台服务。 - **前端技术**:使用JSP页面技术进行前端展示,配合AJAX技术实现无刷新加载数据,...
- **游戏管理**:管理员可以通过后台管理系统对游戏进行增删改查等操作。 - **公司简介管理**:展示公司的基本信息,如发展历程、团队介绍等。 - **用户管理**:管理注册用户的信息,包括登录验证、权限设置等功能。...