原创
struts2框架提供了一种基于验证文件的输入验证方式,将验证规则保存在特定的验证文件中。
验证文件的命名规则
一般情况下,验证文件的命名规则是:Action类名-validation.xml。如果一个Action有多个逻辑处理方法,
要为某个特定方法做验证,其命名规则是:Action类型-Action逻辑名-validation.xml(其中,Action逻辑名是指struts.xml中的对应该处理方法的action配置中的name值)。
struts2框架中提供了大量的内置校验器,在项目开发中,大部分校验功能都可以通过内置校验器来完成。struts2框架提供两种配置校验器的方式:字段校验器配置风格和非字段校验器配置风格。
下面通过字段校验器配置风格来举例。
一个客户输入验证如下:
客户输入界面
<%@ 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>
创建保存用户信息的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; } }
创建Acion验证输入:
package 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 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 "input"; } } }
struts.xml的配置
<struts> <!-- Overwrite Convention --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.convention.action.suffix" value="Controller"/> <constant name="struts.convention.action.mapAllMatches" value="true"/> <constant name="struts.convention.default.parent.package" value="rest-showcase"/> <constant name="struts.convention.package.locators" value="example"/> <!-- Uncomment the lines below to use Jackson XML bindings instead of the XStream library to handle XML serialisations --> <!-- <bean name="jacksonXml" type="org.apache.struts2.rest.handler.ContentTypeHandler" class="org.apache.struts2.rest.handler.JacksonXmlHandler" /> <constant name="struts.rest.handlerOverride.xml" value="jacksonXml"/> --> <package name="default" namespace="/" extends="struts-default"> <!-- 定义拦截器 --> <global-allowed-methods>index,show,create,update,destroy,deleteConfirm,edit,editNew</global-allowed-methods> <!-- 配置action --> <action name="register" class="Action.register"> <result name="success">/register283/Success.jsp</result> <result name="input">/register283/regist.jsp</result> </action> </package> </struts>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% 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%>"> <title>校验成功</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 校验通过,用户信息如下: <hr> 用户名:<s:property value="user.userName"/><br> 密码:<s:property value="user.userPwd"/><br> 确认密码:<s:property value="userPwd"/><br> 性别:<s:property value="user.userSex"/><br> 邮箱地址:<s:property value="user.userEmail"/><br> 基本情况:<s:property value="user.userBasicInfo"/> </body> </html>
成功页面
18:19:03
2018-11-16
转载于:https://www.cnblogs.com/chiweiming/p/9970833.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/101316.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...