JavaWeb學習:Struts2資料校驗
阿新 • • 發佈:2020-12-03
index.jsp
<%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <s:form action="user_login" namespace="/" method="post"> <s:textfieldname="name" label="姓名" /> <s:textfield name="password" label="密碼" /> <s:submit name="submit" value="submit" /> </s:form> </body> </html>
一、手動編碼
①、Action(setxxx方法為了將畫面值設定到對應的屬性中)
public class UserAction extends ActionSupport { // 接收頁面資料 privateString name; private String password; public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } /*=======================驗證execute方法===============================*/ @Override public String execute() throwsException { System.out.println("執行login方法"); return NONE; } @Override public void validate() { super.validate(); } /*=======================驗證指定方法=============================== * * 格式:validate + 方法名(方法名首字母大寫) * * */ public String login() { System.out.println("執行login方法"); return NONE; } public void validateLogin() { if (name == null || name.trim().length() == 0) { this.addFieldError("name", "姓名不能為空"); } if (password == null || password.trim().length() == 0) { this.addFieldError("password", "密碼不能為空"); } } }
②、struts.xml
<package name="interceptorDemo" extends="struts-default" namespace="/"> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="user_*" class="com.xxx.web.action.UserAction" method="{1}"> <result name="input">/demo1/index.jsp</result> </action> </package>
原理:為什麼要使用this.addFieldError(String name, Stringmessage);
二、XML配置
Ⅰ、在Action所在包下,建立一個XML檔案
校驗當前ActionClass中所有方法的格式:ActionClassName-validation.xml
如:校驗UserAction中所有方法
校驗當前ActionClass中指定方法的格式:ActionClassName-ActionName-validation.xml,ActionName對應的是struts.xml檔案對應的action標籤的name屬性的值
如:校驗UserAction中login方法
<action name="user_*" class="com.xxx.web.action.UserAction" method="{1}"> <result name="input">/demo1/index.jsp</result> </action>
Ⅱ、引入DTD
struts2-core-2.5.xx.jar包下有xwork-validator-1.0.3.dtd
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
Ⅲ、進行配置
<validators> <field name="name"> <field-validator type="requiredstring"> <message>使用者名稱不能為空</message> </field-validator> </field> </validators>
requiredstring就是校驗規則:
Ⅳ、Action
public class UserAction extends ActionSupport { // 接收頁面資料 private String name; private String password; public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } public String login() { System.out.println("執行login方法"); return NONE; } }
執行校驗結果:無論name中有沒有值都通不過校驗,原因是name無法獲取到值,缺少get方法
程式碼如下
public class UserAction extends ActionSupport { // 接收頁面資料 private String name; private String password; public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public String getPassword() { return password; } public String login() { System.out.println("執行login方法"); return NONE; } }
校驗規則:
<validators> <field name="password"> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">12</param> <message>密碼需要6-12位</message> </field-validator> </field> <!-- 校驗確認密碼 --> <field name="repassword"> <field-validator type="fieldexpression"> <!-- <![CDATA[(password==repassword)]]> xml將CDTAT中的內容識別為正常的XML資料內容 --> <param name="expression"><![CDATA[(password==repassword)]]></param> <message>兩次密碼不一致</message> </field-validator> </field> <!-- 校驗年齡 --> <field name="age"> <field-validator type="int"> <param name="min">18</param> <param name="max">60</param> <message>年齡不合法!必須是18-60歲之間</message> </field-validator> </field> <!-- 校驗郵箱 --> <field name="email"> <field-validator type="email"> <message>郵箱格式不正確</message> </field-validator> </field> <!-- 校驗確認密碼 --> <field name="phone"> <field-validator type="regex"> <!-- <![CDATA[******]]> xml將CDTAT中的內容識別為正常的XML資料內容 --> <param name="expression"><![CDATA[^13\d{9}$]]></param> <message>電話號碼不正確</message> </field-validator> </field> </validators>