詳解在Java的Struts2框架中配置Action的方法
在Struts2中Action部分,也就是Controller層采用了低侵入的方式。為什么這么說?這是因為在Struts2中action類并不需要繼承任何的基類,或實現(xiàn)任何的接口,更沒有與Servlet的API直接耦合。它通常更像一個普通的POJO(通常應該包含一個無參數(shù)的execute方法),而且可以在內(nèi)容定義一系列的方法(無參方法),并可以通過配置的方式,把每一個方法都當作一個獨立的action來使用,從而實現(xiàn)代碼復用。
例如:
package example; public class UserAction { private String username; private String password; public String execute() throws Exception { //………….. return “success”; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
action訪問servlet
在這個Action類里的屬性,既可以封裝參數(shù),又可以封裝處理結果。系統(tǒng)并不會嚴格區(qū)分它們。
但是為了使用戶開發(fā)的Action類更規(guī)范,Struts2為我們提供了一個接口Action,該類定義如下:
publicinterface Action { publicstaticfinal String ERROR="error"; publicstaticfinal String INPUT="input"; publicstaticfinal String NONE="none"; publicstaticfinal String LOGIN="login"; publicstaticfinal String SUCCESS="success"; public String execute()throws Exception; }
但是我們寫Action通常不會實現(xiàn)該接口,而是繼承該接口的實現(xiàn)類ActionSupport.
該類代碼如下:
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable { ................ public void setActionErrors(Collection errorMessages) { validationAware.setActionErrors(errorMessages); } public Collection getActionErrors() { return validationAware.getActionErrors(); } public void setActionMessages(Collection messages) { validationAware.setActionMessages(messages); } public Collection getActionMessages() { return validationAware.getActionMessages(); } public Collection getErrorMessages() { return getActionErrors(); } public Map getErrors() { return getFieldErrors(); } //設置表單域校驗錯誤 public void setFieldErrors(Map errorMap) { validationAware.setFieldErrors(errorMap); } public Map getFieldErrors() { return validationAware.getFieldErrors(); } public Locale getLocale() { ActionContext ctx = ActionContext.getContext(); if (ctx != null) { return ctx.getLocale(); } else { LOG.debug("Action context not initialized"); return null; } } //獲取國際化信息的方法 public String getText(String aTextName) { return textProvider.getText(aTextName); } public String getText(String aTextName, String defaultValue) { return textProvider.getText(aTextName, defaultValue); } public String getText(String aTextName, String defaultValue, String obj) { return textProvider.getText(aTextName, defaultValue, obj); } ......... //用于訪問國際化資源包的方法 public ResourceBundle getTexts() { return textProvider.getTexts(); } public ResourceBundle getTexts(String aBundleName) { return textProvider.getTexts(aBundleName); } //添加action的錯誤信息 public void addActionError(String anErrorMessage) { validationAware.addActionError(anErrorMessage); } //添加action的普通信息 public void addActionMessage(String aMessage) { validationAware.addActionMessage(aMessage); } public void addFieldError(String fieldName, String errorMessage) { validationAware.addFieldError(fieldName, errorMessage); } public void validate() { } public Object clone() throws CloneNotSupportedException { return super.clone(); } .......... }
前面說到struts2并沒有直接與Servlet的API耦合,那么它是怎么訪問Servlet的API的呢?
原來struts2中提供了一個ActionContext類,該類模擬了Servlet的API。其主要方法如下:
1)Object get (Object key):該方法模擬了HttpServletRequest.getAttribute(String name)方法。
2)Map getApplication()返回一個Map對象,該對象模擬了ServletContext實例.
3)static ActionContext getContext():獲取系統(tǒng)的ActionContext實例。
4)Map getSession():返回一個Map對象,該對象模擬了HttpSession實例.
5)Map getParameters():獲取所有的請求參數(shù),模擬了HttpServletRequest.getParameterMap()
你也許會奇怪為什么這些方法老是返回一個Map?這主要是為了便于測試。至于它是怎么把Map對象與實際的Servlet API的實例進行轉換的,這個我們根本就不要擔心,因為struts2已經(jīng)內(nèi)置了一些攔截器來幫我們完成這一轉換。
為了直接使用Servlet的API,Struts2為我們提供了以下幾個接口。
1)ServletContextAware:實現(xiàn)該接口的Action可以直接訪問ServletContext實例。
2)ServletRequestAware:實現(xiàn)該接口的Action可以直接訪問HttpServletRequest實例。
3)ServletResponseAware:實現(xiàn)該接口的Action可以直接訪問HttpServletResponse實例。
以上主要講了action訪問servlet,下面讓我們來看一下Struts2的Action是如何實現(xiàn)代碼復用的。就拿UserAction來說,我如果讓這個action既處理用戶注冊(regist)又處理登錄(longin)該如何改寫這個action呢?改寫后的UserAction如下:
package example; public class UserAction extends ActionSupport { private String username; private String password; public String regist() throws Exception { //………….. return SUCCESS; } public String login() throws Exception { //………….. return SUCCESS; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
struts.xml中的action配置
是不是這么寫就ok了,當然不行我們還必須在struts.xml文件中配置一下。配置方法有兩種:
1) 使用普通的方式為Action元素指定method屬性.
<action name=”loginAction” class=”example.UserAction” method=”login”> <result name=”success”>/success.jsp</result> </action> <action name=”registAction” class=”example.UserAction” method=”regist”> <result name=”success”>/success.jsp</result> </action>
2) 采用通配符的方式為Action元素指定method屬性。
<action name=”*Action” class=”example.UserAction” method=”{1}”> <result name=”success”>/success.jsp</result> </action>
使用通配符的方式過于靈活,下面是一個較復雜的配置情況。
<action name=”*_*” class=”example.{1}Action” method=”{2}”> ………. </action>
其中占位符{1}與_的前一個*匹配,{2}與后一個*匹配。
基于注解方式Action配置:
下面要說的Action的配置不是在src/struts.xml中,而是用注解方式來進行配置的
前提是除了基本的那六個jar包之外,還需要一個struts-2.1.8.1\lib\struts2-convention-plugin-2.1.8.1.jar
不過struts.xml還是要有的
具體示例
Login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Struts2登錄驗證</title> <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> <body> <h3>Struts2登錄</h3><hr/> <form action="${pageContext.request.contextPath}/user/login.qqi" method="post"> <table border="1" width="500px"> <tr> <td>用戶名</td> <td><input type="text" name="loginname"/></td> </tr> <tr> <td>密碼</td> <td><input type="password" name="pwd"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="登錄"/></td> </tr> </table> </form> </body> </html>
src/struts.xml
<span style="font-size: large;"><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 請求參數(shù)的編碼方式 --> <constant name="struts.i18n.encoding" value="UTF-8"/> <!-- 指定被struts2處理的請求后綴類型。多個用逗號隔開 --> <constant name="struts.action.extension" value="action,do,go,qqi"/> <!-- 當struts.xml改動后,是否重新加載。默認值為false(生產(chǎn)環(huán)境下使用),開發(fā)階段最好打開 --> <constant name="struts.configuration.xml.reload" value="true"/> <!-- 是否使用struts的開發(fā)模式。開發(fā)模式會有更多的調(diào)試信息。默認值為false(生產(chǎn)環(huán)境下使用),開發(fā)階段最好打開 --> <constant name="struts.devMode" value="false"/> <!-- 設置瀏覽器是否緩存靜態(tài)內(nèi)容。默認值為true(生產(chǎn)環(huán)境下使用),開發(fā)階段最好關閉 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 指定由spring負責action對象的創(chuàng)建 <constant name="struts.objectFactory" value="spring" /> --> <!-- 是否開啟動態(tài)方法調(diào)用 --> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> </struts></span>
LoginAction.java
package com.javacrazyer.web.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.ExceptionMapping; import org.apache.struts2.convention.annotation.ExceptionMappings; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; /** * 使用注解來配置Action * */ @ParentPackage("struts-default") // 父包 @Namespace("/user") @Results( { @Result(name = "success", location = "/msg.jsp"), @Result(name = "error", location = "/error.jsp") }) @ExceptionMappings( { @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") }) public class LoginAction extends ActionSupport { private static final long serialVersionUID = -2554018432709689579L; private String loginname; private String pwd; @Action(value = "login") public String login() throws Exception { if ("qq".equals(loginname) && "123".equals(pwd)) { return SUCCESS; } else { return ERROR; } } @Action(value = "add", results = { @Result(name = "success", location = "/index.jsp") }) public String add() throws Exception { return SUCCESS; } public String getLoginname() { return loginname; } public void setLoginname(String loginname) { this.loginname = loginname; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
success.jsp和error.jsp我就不貼出來了
注解配置的解釋
1) @ParentPackage 指定父包
2) @Namespace 指定命名空間
3) @Results 一組結果的數(shù)組
4) @Result(name="success",location="/msg.jsp") 一個結果的映射
5) @Action(value="login") 指定某個請求處理方法的請求URL。注意,它不能添加在Action類上,要添加到方法上。
6) @ExceptionMappings 一級聲明異常的數(shù)組
7) @ExceptionMapping 映射一個聲明異常
由于這種方式不是很常用,所以大家只做了解即可
相關文章
Java實現(xiàn)經(jīng)典游戲推箱子的示例代碼
《推箱子》推箱子是一個古老的游戲,目的是在訓練你的邏輯思考能力。本文將利用Java實現(xiàn)這一經(jīng)典的小游戲,并采用了swing技術進行了界面化處理,需要的可以參考一下2022-02-02