標籤:validate struts2 java
【Struts2】★☆之struts2對Action提交方法進行驗證
在實際的開發項目中,我們通常採用的是js對我們輸入的值進行驗證,例如,使用者名稱的長度,密碼長度,等等。但是這樣做,不好之處就是我們可以通過人為的將開發人員的驗證js注掉,這樣就導致驗證失敗,對後台安全性是一個很大的威脅,在採用struts2進行開發時,我們可以採用架構內建的校正器,對我們的Action進行校正。本文所講訴的就是如何使用重寫struts2中的ActionSupport裡面的validate方法對輸入值進行校正。
ok,看下面代碼!
1、首先搭建基本的struts2開發環境搭建struts2開發環境
2、編寫我們的Action方法
package csg.struts2.action;import java.util.regex.Pattern;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;/** * * @author 小夜的傳說 * @2014-7-20 * @validate * @csg.struts2.action * @StrutsAction * @2014-7-20下午7:21:26 */public class StrutsAction extends ActionSupport {private static final long serialVersionUID = 1L;private String username;private String mobile;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}public String update(){ActionContext.getContext().put("message", "更新成功");return "success";}public String save(){ActionContext.getContext().put("message", "儲存成功");return "success";}/** * 全域方法進行驗證 *//*@Overridepublic void validate() {if(this.username==null||"".equals(this.username.trim())){this.addFieldError("username", "使用者名稱不可為空");}if(this.mobile==null||"".equals(this.mobile.trim())){this.addFieldError("mobile", "手機號不可為空");}else{if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){this.addFieldError("mobile", "手機號格式不正確");}}super.validate();}*//** * 單個方法進行驗證 */public void validateSave() {if(this.username==null||"".equals(this.username.trim())){this.addFieldError("username", "使用者名稱不可為空");}if(this.mobile==null||"".equals(this.mobile.trim())){this.addFieldError("mobile", "手機號不可為空");}else{if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){this.addFieldError("mobile", "手機號格式不正確");}}super.validate();}}
在這裡講解一下,我們的validate()方法會對我們Action裡面的所有方法進行驗證,但是比如說我們的get,list方法是不需要驗證的所以通過validateXxx這樣就可以對我們單個方法進行驗證(validateXxx注意我們需要被驗證的方法名首字母一定要大寫)
ok,
3、編寫我們的jsp提交頁面(index.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <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> <s:fielderror/><!--擷取驗證失敗之後的提示資訊--> <body> <form action="/validate/test/list_save" method="post"> 使用者名稱:<input type="text" name="username"/>不可為空<br/> 手機號:<input type="text" name="mobile"/>不可為空符合手機號格式<br/> <input type="submit" value="提交"/> </form> </body></html>
大家注意了,當我們驗證成功之後,我的提示資訊通過ActionContext.getContext()直接放在request範圍裡面,那麼我們的驗證失敗之後的資訊呢?這個就是放在ActionSupport裡面這個屬性中(看一下源碼就知道了),ActionSupport裡面有如下這段代碼!
public void addFieldError(String fieldName, String errorMessage) { validationAware.addFieldError(fieldName, errorMessage); }
但是當我們驗證失敗之後,ActionSupport預設返回的是return "input"視圖,所以我們需要在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><package name="struts" namespace="/test" extends="struts-default"><action name="list_*" class="csg.struts2.action.StrutsAction" method="{1}"><result name="success">/WEB-INF/page/success.jsp</result><result name="input">/index.jsp</result></action></package></struts>
那麼在index.jsp中我們就可以直接通過struts標籤來取到
this.addFieldError裡面的值。
ok,我們驗證成功之後,直接轉寄到success.jsp頁面,裡面直接通過el運算式,${message}取到值
源碼下載:通過struts2中的validate()方法進行Action驗證-源碼
本文出自 “諾言永遠依戀小柴、、、” 部落格,請務必保留此出處http://1936625305.blog.51cto.com/6410597/1440580
【Struts2】★☆之struts2對Action提交方法進行驗證