Struts2——開發Action

一、Action開發的三種方式

參考:https://mp.weixin.qq.com/s/6YLd8CwsieXOht8cf_R-LQ

1.1 繼承ActionSupport類

在Action類中,需要用到數據校驗等(Struts已經幫我們實現的功能),就繼承ActionSupport類。

1.2 實現Action接口

不推薦。因爲ActionSuppot也實現了Action接口。

1.3 不繼承任何類、不實現任何接口

它就是一個普通的Java類。

public class PrivilegeAction  {
    public String login() {
        System.out.println("我是普通的javaAction,不繼承任何的類、不實現任何的接口");
        return "success";
    }
}
<struts>
<package name="privilige" extends="struts-default">
    <action name="login" class="privilegeaction.PrivilegeAction" method="login">
        <result name="success">/index.jsp</result>
    </action>
</package>
</struts>

注意:若使用到了Struts2一些特用的功能,就需要繼承ActionSupport類。

二、請求數據的封裝

2.1 提出問題

Servlet作用步驟
1、得到web層的數據、封裝數據
2、調用service層的邏輯業務代碼
3、將數據保存在域對象中,跳轉到對應的JSP頁面
域對象:request、response、Session、application之類的對象。
提出問題:在struts中,自己編寫的Action類是沒有域對象的。那麼,如何得到web層的數據、再將數據存到域對象中的呢?
解決問題:在struts中,Struts預先幫我們完成了對數據封裝的功能,它是通過params攔截器來實現數據封裝的。

<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

2.2 解決問題

2.2.1 向後端傳遞數據

register.jsp
基本信息

<form action="${pageContext.request.contextPath}/date01" method="post">
    用戶名:<input type="text" name="username"><br>
    密碼:<input type="text" name="psd"><br>
    年齡:<input type="text" name="age"><br>
    生日:<input type="text" name="birthday"><br>
    <input type="submit" value="註冊"><br>
</form>

對象
在JSP頁面,提交的name要寫成user.username之類的。

<form action="${pageContext.request.contextPath}/register" method="post">
    用戶名:<input type="text" name="user.username"><br>
    密碼:<input type="text" name="user.psd"><br>
    年齡:<input type="text" name="user.age"><br>
    生日:<input type="text" name="user.birthday"><br>
    <input type="submit" value="註冊"><br>
</form>

2.2.2 後端接收數據

Action封裝基本信息
在Action設置與JSP頁面相同的屬性,併爲它們編寫setter方法。
PrivilegeAction.java

private String username;
private String psd;
private int  age;
private Date birthday;
// 此處省略setter/getter方法。

Action封裝對象
1、創建一個User類,基本的信息和JSP頁面是相同的。
User.java

public class User {

    private String username;
    private String psd;
    private int  age;
    private Date birthday;
	// 此處省略setter/getter方法。
}

2、在Action中定義User對象出來,並給出setter和getter方法。
PrivilegeAction.java

public class PrivilegeAction extends ActionSupport {
    private User user;
	// 此處省略setter/getter方法。
    public String register() {
        System.out.println(user.getUsername());
        System.out.println(user.getPsd());
        System.out.println(user.getAge());
        System.out.println(user.getBirthday());
        return "success";
    }
}

三、得到域對象

3.1 通過ServletActionContext獲取

通過ServletActionContext得到Servlet API。
javax.servlet.ServletContext
由於每個用戶擁有一個Action對象,那麼底層爲了維護用戶拿到的是當前線程的request等對象,使用ThreadLocal來維護當前線程下的request、response等對象。

ServletContext context = ServletActionContext.getServletContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
HttpServletResponse response = ServletActionContext.getResponse();

3.2 通過ActionContext獲取

通過ActionContext類來得到request、response、session、application被Struts封裝的Map集合。

//得到ActionContext 對象
ActionContext context = ActionContext.getContext();
Map<String, Object> session = context.getSession();
Map<String, Object> application = context.getApplication();
Map<String, Object> request = context.getContextMap();

3.3 通過實現Aware接口獲取

當web容器發現該Action實現了Aware接口,會把相對應的資源通過Aware接口注射進去,實際上就是一種IOC。
Aware實際就是一種攔截器,攔截代碼在執行Action之前執行、將資源注射到Action中。
實現SessionAware, RequestAware, ApplicationAware接口,它就要在程序中實現三個方法。

private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
@Override
public void setApplication(Map<String, Object> map) {
	this.application = map;
}
@Override
public void setRequest(Map<String, Object> map) {
	this.request = map;
}
@Override
public void setSession(Map<String, Object> map) {
	this.session = map;
}

3.4 三種方法的選擇

方法 含義
ServletActionContext 需要導入Servlet的包,與Struts耦合了
ActionContext 只能在業務方法中使用ActionContext類得到對應的Map對象,若有多個方法,則每個方法都需要寫類似的代碼
xxxAware 可以在類上定義成員變量,以至於整個類都能使用。但是需要實現類、實現對應的方法

適用場景
1、需要使用到對象的其他方法,類似getContextPath()之類的,那麼只能使用ServletActionContext
2、平常的開發,使用ActionContext【獲取簡單,沒有耦合】
3、開發自己的BaseAction時,使用它。

四、日期轉換問題

提出問題:yyyy-MM-dd是Struts直接支持的日期格式,想讓其也支持yyyyMMdd,yyyy年MM月dd日這樣的日期格式。
解決問題:
1、編寫自定義轉換器類。
2、告訴Struts我們寫了轉換器類。
3、Struts根據對應的格式進行自動封裝。

4.1 自定義轉換器類

想要編寫自定義轉換器類,都是實現StrutsTypeConverter類的。

public class MyConvter extends StrutsTypeConverter {
    //需求,當Struts自動封裝數據時,也支持yyyyMMdd,yyyy年MM月dd日等格式的支持\
    SimpleDateFormat[] format = {new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyyMMdd"), new SimpleDateFormat("yyyy年MM月dd日")};
    /**
     * 把String轉換爲指定的類型 【String To Date】
     * @param map   當前上下文環境
     * @param strings  jsp表單提交的字符串的值
     * @param aClass   要轉換爲的目標類型
     */
    @Override
    public Object convertFromString(Map map, String[] strings, Class aClass) {
        //判斷是否有值
        if (strings == null) {
            return null;
        }
        //判斷是否是日期類型的
        if (Date.class != aClass) {
            return null;
        }
        //遍歷循環
        for (SimpleDateFormat dateFormat : format) {
            try {            
                dateFormat.parse(strings[0]);//解析傳遞進來的第一個就行啦
            } catch (ParseException e) {             
                continue;//如果格式不對,那麼就跳出當前的循環
            }
        }
        return null;
    }
    @Override
    public String convertToString(Map map, Object o) {
        return null;
    }
}

4.2 告訴Struts,我寫了轉換器類

4.2.1 全局轉換器

作用範圍:整個項目有效。
步驟
1、在src目錄下創建一個名爲xwork-conversion.properties的文件。
2、配置文件的內容:需要轉換的類類型=轉換器類的全名
如:java.util.Date=qwer.MyConvter

4.2.2 局部轉換器類

作用範圍:當前包下的Action類有效。
步驟
1、在當前的Action包下創建名爲Action名-conversion.properties的文件。
2、文件的內容爲:需要轉換的字段【如果是JavaBean裏的字段,需要寫上JavaBean的】=轉換器類的全名
如:user.birthday=qwer.MyConvter。

4.3 錯誤提示頁面

目的:發生了日期轉換的異常時,給出用戶更友好的提示。
在struts.xml文件中配置:如果返回的是input視圖,那麼跳轉到我們相對應的頁面上。

<result name="input">/error.jsp</result>

五、文件上傳和下載

5.1 使用Struts進行文件上傳

先前:使用FileUpload或者SmartUpload組件來完成文件上傳的功能。但是,FileUpload組件使用起來是比較麻煩的,SmartUPload解決中文的問題也非常麻煩。
解決方案:使用Struts進行文件上傳。Struts內部還是使用fileUpload上傳組件,但是它極大的簡化了具體操作。
在這裏插入圖片描述

5.1.1 JSP頁面

在註冊頁面上擁有兩個上傳文件控件。

<form action="${pageContext.request.contextPath}/register" method="post" enctype="multipart/form-data">
    <input type="file" name="photo"><br>
    <input type="file" name="photo1"><br>
    <input type="submit" value="註冊"><br>
</form>

5.1.2 Action

得到相對應的File對象、上傳文件名稱、上傳文件的類型。

package fileupload;
import java.io.File;
public class FileUploadAction {
    //上傳文件對應的File對象
    private File photo;
    private File photo1;
    //得到上傳文件的名稱
    private String photoFileName;
    private String photo1FileName;
    //得到上傳文件的類型
    private String photoContentType;
    private String photo1ContentType;
    //給出相對應的setter方法
    public String register() throws IOException {
        //得到上傳的路徑
        String path = ServletActionContext.getServletContext().getRealPath("upload");
        System.out.println(path);
        //創建文件對象
        File destFile = new File(path,photoFileName);
        //調用工具類方法,將文件拷貝過去
        FileUtils.copyFile(photo, destFile);
        return "success";
    }
}

5.2 使用Struts進行文件下載

先前:通過設置request消息頭來實現文件下載的。
Struts:請求服務器處理都是通過Action類來完成的,但是Action類的業務方法都是返回字符串。
因此,Struts提供< result type=“stream”>節點。通過stream來配置相對應的信息,從而實現下載!

5.2.1 JSP頁面

<c:if test="${files==null}">
    對不起,沒有下載的頁面
</c:if>
<c:if test="${files!=null}">
    <table border="1px">
        <tr>
            <td>編號</td>
            <td>文件名稱</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${files}" varStatus="file" var="fileName">
            <tr>
                <td>${file.count}</td>
                <%--如果直接寫fileName,輸出的名字帶有路徑,使用EL方法庫來截取--%>
                <td>${fn:substringAfter(fileName, "upload\\")}</td>
                <td>
                    <%--使用url標籤來構建url,不然超鏈接帶有中文,會出現亂碼--%>
                    <c:url var="url" value="down_downLoad">
                        <c:param name="fileName">${fn:substringAfter(fileName, "upload\\")}</c:param>
                    </c:url>
                    <a href="${url}">下載</a>
                </td>
            </tr>
        </c:forEach>
    </table>
</c:if>

5.2.2 Action

public class downLoadAction {
    //列出所有可以下載的文件
    public String list() {
        //得到upload文件夾
        String path = ServletActionContext.getServletContext().getRealPath("/upload");
        //創建file對象
        File file = new File(path);
        //列出文件下所有的文件
        File[] files = file.listFiles();
        //將這些文件存到request域中
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("files", files);
        return "list";
    }
     /**
     * 訪問Action的業務方法僅僅返回的是字符串。因此Struts在result節點提供了stream類型的type,
     * 指定了stream就代表着我這是要下載的...
     * <p>
     * 既然要下載文件,那麼肯定需要幾樣東西:
     * 1、文件名
     * 2、代表文件的流
     */
    public String downLoad() {
        return "downLoad";
    }
    //得到要下載的文件名,Struts提供了自動封裝的功能
    private String fileName;
    //如果文件名是中文的,那麼需要手動轉換,因爲超鏈接是get方法提交
    public void setFileName(String fileName) throws UnsupportedEncodingException {
        fileName = new String(fileName.getBytes("ISO8859-1"), "UTF-8");
        this.fileName = fileName;
        System.out.println(fileName);
    }
    //得到代表下載文件流,該方法由Struts調用
    public InputStream getAttrInputStream() {
        return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + fileName);
    }
    //下載時,顯示的名稱【如果是中文,可能會亂碼,因此要URLencode】---->在Struts.xml文件中通過${}可獲取
    public String getDownFileName() throws UnsupportedEncodingException {
        fileName = URLEncoder.encode(fileName, "UTF-8");
        return fileName;
    }
}

5.2.3 Struts.xml

<action name="down_*" class="fileupload.downLoadAction" method="{1}">
	<result name="{1}">/list.jsp</result>
	<result name="downLoad" type="stream">
		<!--運行下載的類型,指定爲所有的二進制文件-->
		<param name="contentType">application/octet-stream</param>
		<!-- 對應的是Action中屬性: 返回流的屬性【其實就是getAttrInputStream()】 -->
		<param name="inputName">attrInputStream</param>
		<!-- 下載頭,包括:瀏覽器顯示的文件名 -->               <!--${}這裏不是EL表達式-->
		<param name="contentDisposition">attachment;filename=${downFileName}</param>
		<!-- 緩衝區大小設置 -->
		<param name="bufferSize">1024</param>
	</result>
</action>

六、模型驅動

6.1 含義

含義:在Struts2中模型驅動就是用來封裝數據的,完成數據的自動封裝.。
提出問題:先前,
1、通過Sturts2的數據自動封裝功能,是用params攔截器完成的。
2、使用params攔截器完成數據自動封裝時,若封裝的是JavaBean對象,則在web表單中就必須的name寫上javaBean.屬性名(name = “user.username”)。
3、因此,web層和Action層就耦合了。因爲web層必須知道封裝的JavaBean對象是什麼,才能實現自動封裝!
解決問題:模型驅動。當不知道Action層的JavaBean對象是什麼,也能夠完成數據自動封裝!

6.2 模型驅動的實現原理

實現模型驅動功能也是由攔截器完成的。

 <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>

攔截方法的源碼

public String intercept(ActionInvocation invocation) throws Exception {	
	Object action = invocation.getAction();//得到當前要執行的Action對象
	//判斷該Action對象是否實現了ModelDriven接口
	if(action instanceof ModelDriven) {
		ModelDriven modelDriven = (ModelDriven)action;	
		ValueStack stack = invocation.getStack();//獲取值棧對象	
		Object model = modelDriven.getModel();//得到model的對象
		//把對象存到值棧對象中
		if(model != null) {
			stack.push(model);
		}
		if(this.refreshModelBeforeResult) {
			invocation.addPreResultListener(new ModelDrivenInterceptor.RefreshModelBeforeResult(modelDriven, model));
		}
	}
	return invocation.invoke();
}

6.3 使用數據模型驅動

6.3.1 實現ModelDriven接口

實現ModelDriven接口,重寫方法。實現接口時,要封裝的對象是什麼,形參類型就給什麼。

public class UserAction extends ActionSupport implements ModelDriven<User> {
    public String login() {
        return SUCCESS;
    }
    @Override
    public User getModel() {
        return null;
    }
}

6.3.2 對象實例化

實現ModelDriven接口,重寫方法。實現接口時,要封裝的對象是什麼,形參類型就給什麼。

public class UserAction extends ActionSupport implements ModelDriven<User> { 
    private User user = new User();//這裏一定要實例化
    // setter/getter方法。
    @Override
    public User getModel() {
        return user;
    }
    @Override
    public String execute() throws Exception {
        System.out.println(user);
        return SUCCESS;
    }
}

6.3.3 JSP頁面

JSP提交頁面,直接寫上JavaBean對象的屬性就行了。不需要寫上JavaBean對象的名稱!

<form action="${pageContext.request.contextPath}/user_execute">
    <table border="1">
        <tr>
            <td>用戶名:<input type="text" name="username"></td>
        </tr>
        <tr>
            <td> 密碼:<input type="password" name="password"></td>
        </tr>
        <tr>
            <td>電話:<input type="text" name="cellphone"></td>
        </tr>
        <tr>
            <td> 郵箱:<input type="text" name="email"></td>
        </tr>
        <tr>
            <td><input type="submit" value="提交"></td>
        </tr>
    </table>
</form>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章