Struts2能夠對客戶端的輸入進行校驗,經過重寫ActionSupport的validate方法來實現,具體以下:css
首先經過用struts標籤庫建立一個form表單,表單中控件的name與action中的域名稱相同,接着在validate方法中編寫驗證代碼,若驗證失敗則會自動返回input。代碼以下:html
<%@ 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>My JSP 'index.jsp' starting page</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> <s:actionerror/> <s:form action="signIn"> <s:textfield label="UserName" name="username"></s:textfield> <s:textfield label="PassWord" name="password"></s:textfield> <s:textfield label="age" name="age"></s:textfield> <s:submit>Sign in</s:submit> </s:form> </body> </html>
package cn.net.bysoft.lesson4; import com.opensymphony.xwork2.ActionSupport; public class SignInAction extends ActionSupport { public String signIn() throws Exception { return SUCCESS; } public String signOut() throws Exception { return SUCCESS; } public void validateSignIn() { if(username.equals("")){ addFieldError("username", "username invalid"); } if(password.equals("")){ addFieldError("password", "password invalid"); } if(age < 0 || age >= 150) { addActionError("age error"); } } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } private String username; private String password; private int age; private static final long serialVersionUID = 1100577442199735808L; }
在action中,能夠將錯誤信息經過addFieldError方法進行保存,也能夠經過addActionError方法保存。java
此處有一細節,若在一個action中想編寫多個驗證方法,能夠將執行方法首字母大寫後與validate合併起來命名驗證方法,如signIn()處理方法的驗證方法爲validateSignIn()。execute的驗證方法爲validateExecute()。apache
<!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.custom.i18n.resources" value="message"></constant> <package name="struts2_3_24_1" extends="struts-default"> <action name="helloWorld" class="cn.net.bysoft.lesson1.HelloWorldAction"> <result>lesson1/hello.jsp</result> </action> <action name="login" class="cn.net.bysoft.lesson2.LoginAction"> <result name="success">/lesson2/success.jsp</result> <result name="input">/lesson2/login.jsp</result> </action> <action name="point" class="cn.net.bysoft.lesson3.PointAction"> <result name="success">/lesson3/output.jsp</result> <result name="input">/lesson3/index.jsp</result> </action> <action name="signIn" class="cn.net.bysoft.lesson4.SignInAction" method="signIn"> <result name="success">/lesson4/output.jsp</result> <result name="input">/lesson4/index.jsp</result> </action> <action name="signOut" class="cn.net.bysoft.lesson4.SignInAction" method="signOut"> <result name="success">/lesson4/ok.jsp</result> <result name="input">/lesson4/output.jsp</result> </action> </package> </struts>
另外,類型轉換錯誤的異常信息由struts定了,好比頁面年齡輸入一個a,可是age是int類型,則會引起類型轉換異常Invalid field value for field "age".若角色這個提示不合適,能夠自定義類型轉換異常,有兩種方式:less
第一種是定義一個全局的屬性文件,好比在src中定義一個message.properties,並在struts的配置文件中配置進入。<constant name="struts.custom.i18n.resources" value="message"></constant>jsp
xwork.default.invalid.fieldvalue={0} convert error\!
{0}位置是咱們的報錯屬性名稱。ide
第二種是定義一個局部的屬性文件,在建立action的包下建立一個同名的屬性文件,好比在cn.net.bysoft.lesson4包下建立一個SignInAction.properties文件,不須要在struts配置文件中配置。工具
invalid.fieldvalue.age=\u5E74\u9F84\u7C7B\u578B\u8F6C\u6362\u9519\u8BEF
在屬性文件中沒法使用漢字,能夠使用native2ascii工具把漢字轉換成Unicode字符。
最終運行結果以下:ui
輸入空的姓名和密碼,並配輸入超過200歲的年齡,能夠看到姓名和密碼的錯誤被放到了addFieldError中,而年齡的驗證被放到了addActionError中。this
在輸入一個類型轉換錯誤的年齡,能夠看到類型轉換錯誤被struts自動放到了addFieldError中,而且經過咱們的屬性文件替換成了中文的錯誤信息。
最後輸入正確的信息則能夠提交經過。