struts action

action : 包含对用户请求的处理逻辑, 称为业务控制器, 是应用的核心. 每一个需要用到的action都必须定义到struts.xml中.

如何实现action

1.直接实现action处理类

package slowalker.crazy.struts;

import com.opensymphony.xwork2.ActionContext;

public class LoginAction {
    //提供两个属性封装Http请求参数
    private String username;
    private String password;

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String setPassword() {
        return password;
    }

    public String execute() {
        if(username.equals("slowalker") && password.equals("1995")){
       if(username.equals("slowalker") && password.equals("1995")){
            ActionContext.getContext().getSession().put("user", username);
            //getContext()方法返回当前的ActionContext对象
            //getSession的返回值类型是Map不是Session
return "success"; } return "error"; } }

  配置 :

1     <package name="strutsqs" extends="struts-default">
2         
3         <!-- name由表单调用该action, class是被调用的动作 -->
4         <action name="Login" class="slowalker.crazy.struts.LoginAction">
5             <result name="success">/jsps/c3/Success.jsp</result>
6             <result name="error">/jsps/c3/Error.jsp</result>
7         </action>
8     </package>

 视图 :(不使用struts标签)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>login</title>
  </head>
  <body>
    <form action="Login">  <%-- 此处的action必须和配置中action标签中name 属性值相同 --%>
      <table>
        <tr><td>username:</td><td><input type="text" name="username"></td></tr>
        <tr><td>password:</td><td><input type="password" name="password"></td></tr>
        <tr><td><input type="submit" value="submit"></td></tr>
      </table>
    </form>
  </body>
</html>

 流程分析 :  (个人理解)

    1.访问login.jsp,提交一个action指向Login的表单 ==>

    2.进入web.xml过滤分配请求 ==>

    3.在web.xml中定义的核心控制器 StrutsPrepareAndExecuteFilte,拦截 ==>

    4.在struts.xml查找form action指向的action处理请求 ==>

    5.execute()方法返回一个字符串.在该action定义的result标签中查找相应的返回视图.

      

 2.实现Action接口

    代码整体与上文相同, 仅仅是实现了Action接口,并且返回可以使用在接口中定义好的值.

 例如上文中return "success" ====> return SUCCESS;

      return "error"     ====>  return ERROR;  

 1 package com.opensymphony.xwork2;
 2 
 3 public interface Action {
 4 
 5     public static final String SUCCESS = "success";
 6     public static final String NONE = "none";
 7     public static final String ERROR = "error";
 8     public static final String INPUT = "input";
 9     public static final String LOGIN = "login";
10 
11     public String execute() throws Exception;
12 
13 }

 3.继承ActionSupport类

  在该类中已经实现了一些诸如获取国际化信息, 数据校验, 默认的处理用户请求的方法.

  只需要重写自己需要的方法即可.

二 action访问ServletAPI(访问session, response, application域)

  1.直接访问servletAPI

 1 package slowalker.crazy.struts;
 2 
 3 import com.opensymphony.xwork2.ActionContext;
 4 /*
 5  * Action中获取servletAPI
 6  */
 7 public class LoginAction2 {
 8     //http参数
 9     private String username;
10     private String password;
11     
12     
13     public String getUsername() {
14         return username;
15     }
16     
17     public void setUsername(String username) {
18         this.username = username;
19     }
20     
21     public String getPassword() {
22         return password;
23     }
24     
25     public void setPassword(String password) {
26         this.password = password;
27     }
28     
29     public String execute() {
30         if(username.equals("slowalker") && password.equals("1995")) {
31             //向session中添加数据
32             ActionContext.getContext().getSession().put("user", username);
33             Integer counter = (Integer)ActionContext
34                     .getContext().getApplication().get("counter");
35             if (counter == null) counter = 1;
36             else counter ++;
37             //application范围
38             ActionContext.getContext().getApplication().put("counter", counter);
39             //request范围
40             ActionContext.getContext().put("tips", "ok");
41             
42             return "success";
43         }
44         
45         return "error";
46     }
47     
48 }

 视图:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10   name:${sessionScope.user}<br/>
11   times:${applicationScope.counter} <br/>
12   tips&nbsp; ${requestScope.tips }
13 </body>
14 </html>

2.action实现相应的接口访问servletAPI

  1) ServletContextAware : 实现该接口的Action可以直接访问web应用的ServletContext实例 该类包含以下静态方法

    PageContext getPageContext() 

    HttpServletResponse gethttpServletResponse()

    HttpServletRequest getHttpServletRequest()

    ServletContext getServletContext()  

  2)ServletRequestAware : 实现该接口的Action 可以直接访问web应用的HttpServletRequest实例

  3)ServletResponseAware : 实现该接口的Action 可以直接访问web应用的HttpServletresponse实例

 1 package slowalker.crazy.struts;
 2 
 3 import javax.servlet.http.HttpServletResponse;
 4 import org.apache.struts2.interceptor.ServletResponseAware;
 5 import com.opensymphony.xwork2.Action;
 6 import javax.servlet.http.Cookie;
 7 
 8 public class LoginAction3 implements Action, ServletResponseAware{
 9     //需要访问的对象
10     private HttpServletResponse response;
11     
12     private String username;
13     private String password;
14     
15     public String getUsername() {
16         return username;
17     }
18     public void setUsername(String username) {
19         this.username = username;
20     }
21     public String getPassword() {
22         return password;
23     }
24     public void setPassword(String password) {
25         this.password = password;
26     }
27     
28     @Override
29     public void setServletResponse(HttpServletResponse response) {
30         this.response = response;
31         
32     }
33     @Override
34     public String execute() throws Exception {
35         Cookie c = new Cookie("user", getUsername());
36         c.setMaxAge(60*60);
37         response.addCookie(c);
38         return SUCCESS;
39     }
40     
41     
42     
43     
44 }

视图:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="s" uri="/struts-tags"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Cookie</title>
 9 </head>
10 <body>
11     ${cookie.user.value}
12 </body>
13 </html>
原文地址:https://www.cnblogs.com/slowalker-lee/p/8058587.html