struts2繼承ActionSupport類
1.struts2架構搭建流程,請看上一個部落格。
2.在src目錄下建立一個包,即com.hnpi.action,在包下建一個class類,即HelloWordAction,
注意:使用繼承ActionSupport類來實作Action的方式,如下所示:

import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
//省略了
}
ActionSupport類本身實作了Action接口,是以繼承ActionSupport類就相當于實作了Action接口。除此之外,ActionSupport類還實作了其它幾個接口,來為程式員提供更多使用的功能,這些接口和Struts2的一些其他特性相結合,可以實作基本的資料驗證功能和國際化。接口如下所示:
com.opensymphony.xwork2.Validateable; //提供validate()方法來為Action增加驗證的功能
com.opensymphony.xwork2.Validateaware; //提供方法來儲存和恢複action或field級的錯誤資訊
com.opensymphony.xwork2.TextProvider; //提供擷取本地資訊文本的功能
com.opensymphony.xwork2.LocaleProvider;//提供getLocale()方法來擷取本地消息
3.要實作資料驗證的功能,隻需要在Action類中覆寫實作validate方法即可;在validate方法内部,對請求傳遞過來的資料進行校驗,如果不滿足要求,那麼添加例外資訊到父類用于存放例外的集合中。示例代碼如下:
package com.hnpi.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String account;
private String password;
private String submitFlag;
public String execute() throws Exception {
this.businessExecute();
return "toWelcome";
}
public void validate(){
if(account==null || account.trim().length()==0){
this.addFieldError("account", "賬号不可以為空");
}
if(password==null || password.trim().length()==0){
this.addFieldError("password", "密碼不可以為空");
}
if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){
this.addFieldError("password", "密碼長度至少為6位");
}
}
/**
* 示例方法,表示可以執行業務邏輯處理的方法,
*/
public void businessExecute(){
System.out.println("使用者輸入的參數為==="+"account="+account+",password="+password+",submitFlag="+submitFlag);
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSubmitFlag() {
return submitFlag;
}
public void setSubmitFlag(String submitFlag) {
this.submitFlag = submitFlag;
}
}
從上面的示例可以看出,在validate方法中,可以對使用者請求中傳遞過來的資料進行驗證,同一個資料可以進行多方面的驗證。
如果驗證結果是資料不正确,那麼就使用父類提供的addFieldError方法來添加驗證的錯誤消息。addFieldError方法有兩個參數,前面的是消息的key值,後面是具體的消息。
4.建立視圖層 jsp頁面
1>login.jsp登入頁面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; utf-8">
<title>登入頁面</title>
<style type="text/css">
ul,li {
list-style-type:none;
margin:0px;
float:left;
}
</style>
</head>
<body>
<form action="helloworld" method="post">
<input type="hidden" name="submitFlag" value="login"/>
<div>
<font color=red><s:fielderror fieldName="account"/></font>
<br/>
賬号:<input type="text" name="account">
</div>
<div>
<font color=red><s:fielderror fieldName="password"/></font>
<br/>
密碼:<input type="password" name="password">
</div>
<input type="submit" value="送出">
</form>
</body>
</html>
2>登陸成功頁面
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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%>" target="_blank" rel="external nofollow" >
<title>錄入頁面</title>
</head>
<body>
登陸成功 <br>
</body>
</html>
3>在JSP頁面中利用<s:fielderror/>标簽在相應的字段處輸出錯誤資訊。但是,在實際開發中,<s:fielderror/>它會輸出全部的錯誤資訊内容。而如果想選擇性地輸出指定錯誤資訊。我們可以使用如下代碼解決:
<!-- 方法一 -->
<s:fielderror>
<s:param>username</s:param> <!--顯示指定的 username字段的 錯誤消息-->
<s:fielderror/>
<!-- 方法二 -->
<s:fielderror fieldName="username"/> <!--顯示指定的 username字段的 錯誤消息-->
5.validate方法是沒有傳回值的,那麼當驗證後,如果有資料沒有通過驗證,該傳回到什麼頁面呢?這就需要在struts.xml中的Action配置裡面,添加一個名稱為input的result配置,也就是說,如果validate方法中,有資料沒有通過驗證,那麼會自動跳轉回到該action中名稱為input的result所配置的頁面。示例如下:
<?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="default" extends="struts-default">
<action name="register"
class="com.hnpi.action.RegisterAction" method="t1">
<result name="success">/index.jsp</result>
</action>
<action name="helloworld" class="com.hnpi.action.HelloWorldAction">
<result name="toWelcome">/welcome.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
6.建立消息的配置檔案,在Action類的路徑下建立一個同名的properties檔案,也就是檔案名為HelloWorldAction.properties,即右擊包名,建立others,找到general,點選File,建properties檔案。
然後在裡面按照key=value的格式,添加要使用的錯誤消息。示例如下:
k1=\u5E10\u53F7\u4E0D\u5141\u8BB8\u4E3A\u7A7A
k2=\u5BC6\u7801\u4E0D\u5141\u8BB8\u4E3A\u7A7A
k3=\u5BC6\u7801\u957F\u5EA6\u5FC5\u987B\u57286\u4F4D\u4EE5\u4E0A
其實是把中文的消息轉換成了相應的unicode編碼,比如k1後面的value值,其實就是“帳号不允許為空”的unicode編碼。隻有這樣,在程式裡面讀取到這些值的時候才會正确顯示中文。有很多工具可以把中文轉換成unicode編碼,比如,native2ascii工具就可以實作。
7.Action裡面,就修改validate方法,原來是直接寫的中文字元串,現在應該修改成從配置檔案中擷取資訊了,示例如下:
public void validate(){
if(account==null || account.trim().length()==0){
this.addFieldError("account", this.getText("k1"));
}
if(password==null || password.trim().length()==0){
this.addFieldError("password", this.getText("k2"));
}
if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){
this.addFieldError("password", this.getText("k3"));
}
}
這樣我們繼承ActionSupport類的資訊驗證項目就完成了。