Struts2之手工編碼方式進行輸入校正,struts2校正

來源:互聯網
上載者:User

Struts2之手工編碼方式進行輸入校正,struts2校正

輸入校正的流程
1、類型轉換器對請求參數執行類型轉換,並把轉換後的值賦給action中的屬性
2、如果在執行類型轉換的過程中出現異常,系統會將異常資訊儲存到ActionContext,conversionError攔截器將異常資訊封裝到fieldError裡。不管類型轉換是否出現異常,都會進入第3步。
3、系統通過反射技術先調用action中的validateXxx()方法,Xxx為方法名。
4、再調用action中的validate()方法
5、經過上面4步,如果系統中的fieldError存在錯誤資訊(即存放錯誤資訊的集合的size大於0),系統自動將請求轉寄至名稱為input的視圖。如果系統中的fieldError沒有任何錯誤資訊,系統將執行action中的處理方法。

在struts2中,我們可以實現對action的所有方法進行校正,也可以實現對action的指定方法進行校正。

對於輸入校正struts2提供兩種實現方法:
1、採用手工編寫代碼實現

2、基於XML配置方式實現

相對於第一種,第二種用起來比較方便。

登入頁面index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>輸入校正</title></head><body>    <s:fielderror/><form action="${pageContext.request.contextPath}/control/manage_save.action" method="post"> 系統管理使用者名:<input type="text" name="username" />不可為空<br> 手機號碼:<input type="text" name="mobilephone" /> 不可為空,並且要符合手機號碼格式1,3/5/8,後面9位元字<br><input type="submit" value="提交" /></form></body></html>
package struts2.validate.action;import java.util.regex.Pattern;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class PersonAction extends ActionSupport{private String username;private String mobilephone;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getMobilephone() {return mobilephone;}public void setMobilephone(String mobilephone) {this.mobilephone = mobilephone;}public String update() {ActionContext.getContext().put("message", "更新成功!");return "message";}public String save() {ActionContext.getContext().put("message", "儲存成功");return "message";}//對action中的所有方法校正@Overridepublic void validate() {if(this.username==null || this.username.trim().equals("")){this.addFieldError("username", "使用者名稱不可為空!");}if(this.mobilephone==null || this.mobilephone.equals("")){this.addFieldError("mobilephone", "手機號不可為空!");}else{if(!Pattern.compile("^1[358]\\d{9}$").matcher(mobilephone).matches()){this.addFieldError("mobilephone", "手機號格式不正確!");}}super.validate();}}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>結果</title>  </head>    <body>    ${message}  </body></html>
通過重寫validate()方法實現,validate()方法會校正action中所有與execute方法簽名相同的方法。當某個資料校正失敗時,我們應該調用addFieldError()方法往系統的fieldErrors添加校正失敗資訊(為了使用addFieldError()方法,action可以繼承ActionSupport),如果系統的fieldErrors包含失敗資訊,struts2會將請求轉寄到名為input的result。在input視圖中可以通過<s:fielderror/>顯示失敗資訊
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"         "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="csdn" namespace="/control" extends="struts-default">        <action name="manage_*" class="struts2.validate.action.PersonAction" method="{1}">           <result name="message">/WEB-INF/page/message.jsp</result>           <result name="input">/index.jsp</result>        </action></package></struts>

validate()方法會對action中的所有方法進行校正,如果只需要對指定方法進行校正,可通過重寫validateXxx()方法實現,validateXxx()只會校正action中方法名為Xxx()的方法。其中Xxx()的第一個字母要大寫。其他配置書寫步驟和寫validate()方法一樣。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.