一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 詳解在Java的Struts2框架中配置Action的方法

詳解在Java的Struts2框架中配置Action的方法

2020-04-13 11:24Ai2015WER JAVA教程

這篇文章主要介紹了詳解在Java的Struts2框架中配置Action的方法,講解了包括struts.xml中的action配置及基于注解方式Action配置的兩個方式,需要的朋友可以參考下

在Struts2中Action部分,也就是Controller層采用了低侵入的方式。為什么這么說?這是因為在Struts2中action類并不需要繼承任何的基類,或實現任何的接口,更沒有與Servlet的API直接耦合。它通常更像一個普通的POJO(通常應該包含一個無參數的execute方法),而且可以在內容定義一系列的方法(無參方法),并可以通過配置的方式,把每一個方法都當作一個獨立的action來使用,從而實現代碼復用。
例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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類里的屬性,既可以封裝參數,又可以封裝處理結果。系統并不會嚴格區分它們。

但是為了使用戶開發的Action類更規范,Struts2為我們提供了一個接口Action,該類定義如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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通常不會實現該接口,而是繼承該接口的實現類ActionSupport.

該類代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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():獲取系統的ActionContext實例。

4)Map getSession():返回一個Map對象,該對象模擬了HttpSession實例.

5)Map getParameters():獲取所有的請求參數,模擬了HttpServletRequest.getParameterMap()

你也許會奇怪為什么這些方法老是返回一個Map?這主要是為了便于測試。至于它是怎么把Map對象與實際的Servlet API的實例進行轉換的,這個我們根本就不要擔心,因為struts2已經內置了一些攔截器來幫我們完成這一轉換。

為了直接使用Servlet的API,Struts2為我們提供了以下幾個接口。

1)ServletContextAware:實現該接口的Action可以直接訪問ServletContext實例。

2)ServletRequestAware:實現該接口的Action可以直接訪問HttpServletRequest實例。

3)ServletResponseAware:實現該接口的Action可以直接訪問HttpServletResponse實例。

以上主要講了action訪問servlet,下面讓我們來看一下Struts2的Action是如何實現代碼復用的。就拿UserAction來說,我如果讓這個action既處理用戶注冊(regist)又處理登錄(longin)該如何改寫這個action呢?改寫后的UserAction如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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屬性.

?
1
2
3
4
5
6
7
8
9
10
11
<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屬性。

?
1
2
3
4
5
<action name=”*Action” class=”example.UserAction” method=”{1}”>
 
    <result name=”success”>/success.jsp</result>
 
</action>

使用通配符的方式過于靈活,下面是一個較復雜的配置情況。

?
1
2
3
4
5
<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
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ 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
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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>
  <!-- 請求參數的編碼方式 -->
  <constant name="struts.i18n.encoding" value="UTF-8"/>
  <!-- 指定被struts2處理的請求后綴類型。多個用逗號隔開 -->
  <constant name="struts.action.extension" value="action,do,go,qqi"/>
  <!-- 當struts.xml改動后,是否重新加載。默認值為false(生產環境下使用),開發階段最好打開 -->
  <constant name="struts.configuration.xml.reload" value="true"/>
  <!-- 是否使用struts的開發模式。開發模式會有更多的調試信息。默認值為false(生產環境下使用),開發階段最好打開 -->
  <constant name="struts.devMode" value="false"/>
  <!-- 設置瀏覽器是否緩存靜態內容。默認值為true(生產環境下使用),開發階段最好關閉 -->
  <constant name="struts.serve.static.browserCache" value="false" />
  <!-- 指定由spring負責action對象的創建 
  <constant name="struts.objectFactory" value="spring" />
  -->
  <!-- 是否開啟動態方法調用 -->
  <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
</struts></span>

 
 LoginAction.java
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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 一組結果的數組
  4) @Result(name="success",location="/msg.jsp") 一個結果的映射
  5) @Action(value="login") 指定某個請求處理方法的請求URL。注意,它不能添加在Action類上,要添加到方法上。
  6) @ExceptionMappings 一級聲明異常的數組
  7) @ExceptionMapping 映射一個聲明異常

由于這種方式不是很常用,所以大家只做了解即可

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女跪式抽搐gif动态图 | 日韩一级精品视频在线观看 | 天天色踪合合 | 女海盗斯蒂内塔的复仇2免费观看 | 色五月天天 | 精品蜜臀AV在线天堂 | 日本亚洲免费 | 91传媒在线观看 | 久久香蕉电影 | 肉性天堂| 亚欧有色在线观看免费版高清 | 久久久久久久国产精品视频 | 天美麻豆| 毛片 ftp| 日本不卡在线一区二区三区视频 | 久草在线草a免费线看 | 色五月天天 | 涩涩漫画免费 | 波多在线 | 大好硬好深好爽想要视频 | 美女用手扒开粉嫩的屁股 | 白丝爆动漫羞羞动漫软件 | 暴露狂婷婷 | 三叶草私人研究所 | 乌克兰粉嫩摘花第一次 | 日韩综合第一页 | 夫承子液by免费阅读 | 欧美人曾交 | 日本无吗免费一二区 | 奶茶视频有容乃大 | 欧美肥胖老妇做爰变态 | 天堂一区二区在线观看 | 日本搜子同屋的日子2国语 日本爽p大片免费观看 | 大胸纲手被羞羞漫画网站 | 办公室里被迫高h | 特黄特色大片免费视频大全 | 久久受www免费人成_看片中文 | 亚洲AV精品无码喷水直播间 | 久久无码人妻中文国产 | 成人免费体验区福利云点播 | 亚洲国产经典 |