struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <include file="cn/itcast/validate/struts_validate.xml"></include> </struts>
struts_validate.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="validate" namespace="/validate" extends="struts-default"> <action name="validateAction_*" class="cn.itcast.validate.ValidateAction" method="{1}"> <result name="success">/validate/success.jsp</result> <result name="input">/validate/login.jsp</result> </action> </package> </struts>
ValidateAction.java
package cn.itcast.validate; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class ValidateAction extends ActionSupport { private String username; public String getUsername() { return username; } public void setUsername(String username) { System.out.println("ValidateAction ***** setusername()"); this.username = username; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } private String psw; /* * 验证方法: * *改动作类action必须继承ActionSupport或者实现Validateable接口 * *必须保证serUsername(),validate(),login()方法的执行顺序如下: * *setUsername() * *validate() * *login() **如果登陆失败,如何处理: **this.addFildError(key,value); **key:错误提示字段 **value:错误提示信息 **什么时候才是验证通过? **验证通过:1.map集合存在;2.map集合存在时为, **验证不通过:map集合存在,并且不为空 **分析需求: **用户名不能为null,"" **密码不能为null,"",并且密码的长度6-12之间 * *针对所有业务方法进行验证还是针对某个指定业务方法进行验证? * *重写的validate()方法,针对所有业务方法进行验证 * * 重写的validate()方法加上要验证的制定的业务方法名(业务方法名的首字母大写),实现针对某个指定的业务方法进行验证 * *为什么要这样进行拼接?因为struts2框架底层拼接,如果不这样写底层就找不到对应方法名 * (non-Javadoc) * @see com.opensymphony.xwork2.ActionSupport#validate() */ public void validateLogin() { System.out.println("ValidateAction ****** validate()"); if(username==null||username.equals("")){ this.addFieldError("error", "用户名不能为空"); } if(psw==null||psw.equals("")){ this.addFieldError("error", "密码不为空"); }else{ String pattern="^[0-9a-zA-Z]{6,12}"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(psw); boolean b = m.matches(); if(!b){ this.addFieldError("error", "密码的长度必须在6-12之间"); } } } public String login(){ // 验证 System.out.println("ValidateAction ****** login()"+"username:"+username+"psw:"+psw); return "success"; } public String test(){ System.out.println("validate ******* test()"); return "success"; } }
login.jsp
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <html> <head> <title>My JSP ‘login.jsp‘ starting page</title> </head> <body> <br> <s:fielderror/> <s:form name="loginForm" method="post" namespace="/validate" action="validateAction_login.action" theme="simple" > <table border="1"> <tr> <td>用户名</td> <td> <s:textfield name="username" /></td> </tr> <tr> <td>密码</td> <td><s:password name="psw" /></td> </tr> <tr> <td> </td> <td><s:submit value="登陆"/></td> </tr> </table> </s:form> </body> <a href="${pageContext.request.contextPath}/validate/validateAction_test.action">test其他的方法</a> </html>
时间: 2024-10-19 22:50:44