首页 技术 正文
技术 2022年11月13日
0 收藏 811 点赞 3,089 浏览 3093 个字

1、需要导入的jar包:

slf4j-api-1.7.21.jar

validation-api-1.0.0.GA.jar

hibernate-validator-4.0.1.GA.jar

2、访问页面编码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" > <title></title>
<script type="text/javascript" src="js/jquery-1.8.3.js"></script> </head> <body>
<h1>数据校验</h1>
<form action="${pageContext.request.contextPath }/first.do" method="post">
成绩:<input name="score" /> <span>${scoremsg }</span><br/><br/>
姓名:<input name="name"/><span>${namemsg }</span><br/><br/>
电话:<input name="phone"/><span>${phonemsg }</span><br/><br/>
<input type="submit" value="注册"/>
</form>
</body>
</html>

3、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
"> <!-- 配置包扫描器-->
<context:component-scan base-package="cn.happy.controller"></context:component-scan> <!-- 配置验证器 -->
<bean id="myvalidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
</bean> <mvc:annotation-driven validator="myvalidator"/>
</beans>

4、controller控制器

package cn.happy.controller;import javax.validation.Valid;import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import cn.happy.entity.UserInfo;@Controller
public class FirstController {
@RequestMapping("/first.do")
public ModelAndView doFirst(@Valid UserInfo info,BindingResult br){
ModelAndView mv=new ModelAndView();
mv.setViewName("/WELCOME.jsp");
//记录到底是哪个字段验证失败了
//有一个可以侦测到验证错误总数的方法
int errorCount = br.getErrorCount();
if (errorCount>0) {
//证明模型验证失败
FieldError score = br.getFieldError("score");
FieldError name = br.getFieldError("name");
FieldError phone = br.getFieldError("phone");
if (score!=null) {
mv.addObject("scoremsg",score.getDefaultMessage());
} if (name!=null) {
mv.addObject("namemsg",name.getDefaultMessage());
} if (phone!=null) {
mv.addObject("phonemsg",phone.getDefaultMessage());
}
mv.setViewName("/index.jsp");
} //高中 英文版的吻别
return mv ;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918