原创
struts2的输入验证有两种方式:
- 使用validate()方法实现验证
- 使用验证文件实现验证
下面通过一个例子介绍validate()方法验证——实现客户注册输入验证
设计的JSP页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% 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> <title>注册页面</title> <style type="text/css"> body{font-size:12px;} #title{color:#FF7B0B;font-size:20px;font-weight:bold;} .td{height:30px;} .info{color:#BBBBBB;} </style> <script language="javascript"> function check(frm){ if(frm.accept.checked==false){ alert("您需要仔细阅读用户使用协议,并同意接受协议!"); return false; } return true; } </script> </head> <body> <s:fielderror cssStyle="color:red"></s:fielderror> <s:form action="register" method="post" theme="simple" onsubmit="return check(this)" namespace="/"> <table border="0" align="center" width="1000"> <tr><td colspan="3" align="center" height="40" id="title">填写注册信息<br/></td></tr> <tr><td align="right">用户名:*</td><td><s:textfield name="user.userName"/></td><td class="info">用户名由字母开头,后跟字母、数字或下划线!</td></tr> <tr><td align="right">密码:*</td><td><s:password name="user.userPwd"/></td><td class="info">设置登录密码,至少6位!</td></tr> <tr><td align="right">确认密码:*</td><td><s:password name="userPwd"/></td><td class="info">请再输入一次你的密码!</td></tr> <tr><td align="right">性别:*</td><td><s:radio name="user.userSex" list="{'男','女'}"/></td><td class="info">请选择你的性别!</td></tr> <tr><td align="right">邮箱地址:*</td><td><s:textfield name="user.userEmail"/></td><td class="info">请填写您的常用邮箱,可以用此邮箱找回密码!</td></tr> <tr><td align="right" valign="top">基本情况:*</td><td colspan="2"><s:textarea name="user.userBasicInfo" rows="5" cols="50"/></td></tr> <tr><td colspan="3" align="center" height="40"><s:checkbox name="accept" value="false"/>我已经仔细阅读并同意接受用户使用协议</td></tr> <tr><td colspan="3" align="center" height="40"><input type="submit" value="确认"/> <input type="reset" value="取消"/></td></tr> </table> </s:form> </body> </html>
页面
设计User.java的JavaBean存放用户信息
public class User { private String userName; private String userPwd; private String userSex; private String userEmail; private String userBasicInfo; public User(){}; public String getUserName() { return userName; } public void setUserName(String usreName) { this.userName = usreName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; } public String getUserEmail() { return userEmail; } public void setUserEmail(String userEmail) { this.userEmail = userEmail; } public String getUserBasicInfo() { return userBasicInfo; } public void setUserBasicInfo(String userBasicInfo) { this.userBasicInfo = userBasicInfo; } }
设计带validate()方法的Action用来检验用户输入的信息,成功跳转输入用户输入的信息,否则返回注册页面并且输入错误信息。
import JavaBean.User; import java.sql.*; import com.opensymphony.xwork2.ActionSupport; import java.util.regex.*; import DBase.connection; public class register extends ActionSupport{ private User user; private String userPwd; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public void validate(){ if(user.getUserName()==null || user.getUserName().length()<=0){ addFieldError("user.userName","用户名不能为空!"); }else{ String f1="[a-zA-Z]\w*"; if(!Pattern.matches(f1, user.getUserName())){ addFieldError("user.userName","用户名格式不正确"); } } if(user.getUserPwd()==null || user.getUserPwd().length()<=0){ addFieldError("user.userPwd","密码不能为空!"); }else{ if(user.getUserPwd().length()<6){ addFieldError("user.userPwd","密码长度不能小于6!"); } } if(userPwd==null || userPwd.length()<=0){ addFieldError("userPwd","确认密码不能为空!"); }else{ if(!userPwd.equals(user.getUserPwd())){ addFieldError("userPwd","密码不一致!"); } } if(user.getUserEmail()==null || user.getUserEmail().length()<=0){ addFieldError("user.userEmail","邮件地址不能为空!"); }else{ String f2="\w+([-+.']\w+)*@\w+([-.]\w+)*.\w+([-.]\w+)*"; if(!Pattern.matches(f2, user.getUserEmail())){ addFieldError("user.userEmail","邮箱格式不正确!"); } } } public boolean add(){ Connection con=connection.getConnection(); //获取数据库连接 String sql="insert into Users(username,userpwd,usersex,useremail,userbasicinfo) values(?,?,?,?,?)"; try { PreparedStatement pre=con.prepareStatement(sql); pre.setString(1,user.getUserName()); pre.setString(2, user.getUserPwd()); pre.setString(3, user.getUserSex()); pre.setString(4, user.getUserEmail()); pre.setString(5, user.getUserBasicInfo()); int n=pre.executeUpdate(); if(n==1){ return true; } } catch (SQLException e) { System.out.println("error"); return false; } return false; } public String regist() throws Exception{ if(add()==true){ return "success"; }else{ return "error"; } } }
编写一个类来获取数据库的连接
import java.sql.*; public class connection { public static Connection getConnection() { String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=********"; String userName = "sa"; String userPwd = "*******"; Connection dbConn=null; try{ Class.forName(driverName); dbConn=DriverManager.getConnection(dbURL,userName,userPwd); return dbConn; }catch(Exception e){ e.printStackTrace(); } return null; } public static void closeDB(Connection con,PreparedStatement pstm,ResultSet rs){ try{ if(rs!=null){ rs.close(); } if(pstm!=null){ pstm.close(); } if(con!=null){ con.close(); } }catch(SQLException e){ e.printStackTrace(); } } }
23:14:01
2018-11-15