Struts2(五) 校驗簡單資料型別方法 和 值棧的說明

u013457570發表於2016-12-25

一、值棧的講解、什麼是值棧呢?

通過對struts2的一段時間的接觸,將自己對OGNL的核心值棧說說,值棧:簡單的說,就是存放action的堆疊,當我們提交一個請求道伺服器端 action時,就有個堆疊,如果action在伺服器端進行跳轉,所有action共用一個堆疊,當需要儲存在action中的資料時,首先從棧頂開始 搜尋,若找到相同的屬性名(與要獲得的資料的屬性名相同)時,即將值取出,但這種情況可能出現找到的值不是我們想要的值,那麼解決此問題需要用TOP語法 和N語法來進行解決。 重最要的是遵循”後進先出“的原則。


值棧說明:

error是一個Map,Map裡面有包含著資料;例如上圖的errors舉例子,裡面的name就是個陣列,具體獲取值看以下案例。

二、Struts2 簡單資料型別校驗即將開始了;嘮叨下 這是一個其實原理很簡單

第一步:連線請求到對應的Action進行處理,在Action中定義同名字的引數,賦予set 和 get 方法;

第二步:對獲取到的引數進行判斷。

第三步:檢驗成功或失敗結果返回。

第四步:校驗失敗後頁面值顯示。

2.0、程式碼結構圖



2.1、JSP頁面請求

<a href="<%=path %>/test03_DataValiation_01?name=flx">使用addFieldError方法 和 s:fieldError標籤簡單處理資料校驗</a>

2.2、配置檔案 struts.xml 處理

<?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>

   <!--  <constant name="struts.enable.DynamicMethodInvocation" value="false" /> -->
   <!-- 設定為開發者模式  修改之後不需要重啟伺服器 -->
    <constant name="struts.devMode" value="false" />
    <package name="default" namespace="/" extends="struts-default">
    
    <!-- 簡單處理資料型別 -->
    <action name="test03_*" class="com.flx.actions.dataValiation.SimpleDataValiation" method="{1}">
    	<result>
    		/jsp/getParam.jsp
    	</result>
    	<result name="fails">
    		/jsp/fails.jsp
    	</result>
    </action>
    </package>
</struts>

2.3、Action處理類

package com.flx.actions.dataValiation;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author FuLX
 * 
 * @2016-12-25上午8:22:58
 * 
 *  功能:簡單資料型別驗證 使用addFieldError方法和s:fieldError標籤簡單處理資料校驗
 */

public class SimpleDataValiation extends ActionSupport {

	private static final String FINAL_FLX_SUCCESS = "success";
	private static final String FINAL_FLX_FAILS = "fails";

	private String name;

	public String DataValiation_01() {
		System.out.println("後臺接收到的引數:" + this.name);
		if (this.name != null && !"admin".equals(this.name)) {
			this.addFieldError("name", "資料校驗不通過");
			this.addFieldError("name", "name errors_01");
			return FINAL_FLX_FAILS;
		}
		return FINAL_FLX_SUCCESS;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

2.4、fails.jsp頁面說明

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引用Struts 標籤庫 -->
<%@taglib uri="/struts-tags" prefix="s"%>
<%
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>資料校驗失敗</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">
  </head>
  <body>
          錯誤資訊展示
   <s:fielderror fieldName="name" theme="simple"/>
   <br/>
   <h4>通過值棧去獲取值</h4>
  errors:  <s:property value="errors"/><br/>
  errors.name: <s:property value="errors.name"/><br/>
  errors.name[0]: <s:property value="errors.name[0]"/><br/>
  errors.name[1]: <s:property value="errors.name[1]"/><br/>
   <s:debug></s:debug>
  </body>
</html>

2.5、頁面顯示


相關文章