Struts2中Action的使用(Struts2_Action)

时间:2022-08-07 02:26:50

一、Action概要

Struts2中Action的使用(Struts2_Action)

二、动态调用

 Struts2中Action的使用(Struts2_Action)

三、通配符(规则:*_*等价于{1_2})

Struts2中Action的使用(Struts2_Action)

四、获取参数

  1 1、方式一:逐个获取(推荐使用)
  2 package com.aaron.action.param;
  3 import com.opensymphony.xwork2.ActionSupport;
  4 public class ParamMethodOneAction extends ActionSupport{
  5     private int id;
  6     private String userName;
  7     private String userPass;
  8     @Override
  9     public String execute() throws Exception {
 10         System.out.println("id:"+id+"---"+"userName:"+userName+"---"+"userPass:"+userPass);
 11         this.addFieldError("id", id+"");
 12         this.addFieldError("userName", userName);
 13         this.addFieldError("userPass", userPass);
 14
 15         return SUCCESS;
 16     }
 17     public int getId() {
 18         return id;
 19     }
 20     public void setId(int id) {
 21         this.id = id;
 22     }
 23     public String getUserName() {
 24         return userName;
 25     }
 26     public void setUserName(String userName) {
 27         this.userName = userName;
 28     }
 29     public String getUserPass() {
 30         return userPass;
 31     }
 32     public void setUserPass(String userPass) {
 33         this.userPass = userPass;
 34     }
 35 }
 36
 37 2、方式二:用某个entity获取(推荐使用)
 38 package com.aaron.action.param;
 39
 40 import com.aaron.action.model.User;
 41 import com.opensymphony.xwork2.ActionSupport;
 42
 43 /**
 44  * 传递参数一般使用这种方式
 45  * @author qiuyong
 46  */
 47 public class ParamMethodTwoAction extends ActionSupport {
 48
 49     private User user;
 50     @Override
 51     public String execute() throws Exception {
 52         System.out.println("id:"+user.getId()+"---"+"userName:"+user.getUserName()+"---"+"userPass:"+user.getUserPass());
 53         this.addFieldError("id", user.getId()+"");
 54         this.addFieldError("userName", user.getUserName());
 55         this.addFieldError("userPass", user.getUserPass());
 56         return SUCCESS;
 57     }
 58
 59     public User getUser() {
 60         return user;
 61     }
 62
 63     public void setUser(User user) {
 64         this.user = user;
 65     }
 66
 67 }
 68
 69 3、方式三:利用inplModelDriven(不推荐使用)
 70 package com.aaron.action.param;
 71 import com.aaron.action.model.User;
 72 import com.opensymphony.xwork2.ActionSupport;
 73 import com.opensymphony.xwork2.ModelDriven;
 74
 75 /**
 76  * @author qiuyong
 77  * 这种方式一般不用
 78  */
 79 public class ParamMethodThreeAction extends ActionSupport implements ModelDriven<User>{
 80     private User user=new User();
 81
 82     @Override
 83     public String execute() throws Exception {
 84         System.out.println("id:"+user.getId()+"---"+"userName:"+user.getUserName()+"---"+"userPass:"+user.getUserPass());
 85         this.addFieldError("id", user.getId()+"");
 86         this.addFieldError("userName", user.getUserName());
 87         this.addFieldError("userPass", user.getUserPass());
 88         return SUCCESS;
 89     }
 90
 91     public User getModel() {
 92         return user;
 93     }
 94
 95     public User getUser() {
 96         return user;
 97     }
 98
 99     public void setUser(User user) {
100         this.user = user;
101     }
102 }

五、访问Web元素

 1、Map类型_Ioc方式(推荐使用这种方式)

 /**
  * (推荐使用这种方式)利用Ioc创造Map类型request、session、application
  * @author qiuyong
  */
 public class WebElementMapIocAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
     Map<String,Object> request;
     Map<String,Object> session;
     Map<String,Object> application;

     @Override
     public String execute() throws Exception {
         request.put("request", "MapIocRequest");
         session.put("session","MapIocSession");
         application.put("application","MapIocApplication");
         return SUCCESS;
     }

     public void setRequest(Map<String, Object> request) {
         this.request=request;
     }

     public void setSession(Map<String, Object> session) {
         this.session=session;
     }

     public void setApplication(Map<String, Object> application) {
         this.application=application;
     }

 }

 2、Map类型_Struts容器

 /**
  * @author qiuyong
  * 利用Struts容器创造Map类型request、session、application
  */
 public class WebElementMapActionContextAction extends ActionSupport {
     Map request;
     Map session;
     Map application;

     public WebElementMapActionContextAction() {
         request = (Map) ActionContext.getContext().get("request");
         session = ActionContext.getContext().getSession();
         application = ActionContext.getContext().getApplication();

     }

     @Override
     public String execute() throws Exception {
         request.put("request", "MapActionContextRequest");
         session.put("session","MapActionContextSession");
         application.put("application","MapActionContextApplication");
         return SUCCESS;
     }
 }

 3、Servlet原始类型_Ioc方式

 /**
  * @author qiuyong
  * 利用Ioc的方式创造原始类型的request、session、application
  */
 public class WebElementRealIocAction extends ActionSupport implements ServletRequestAware {
     HttpServletRequest request;
     HttpSession session;
     ServletContext application;

     @Override
     public String execute() throws Exception {
         request.setAttribute("request","RealIocRequest");
         session.setAttribute("session","RealIocSession");
         application.setAttribute("application","RealIocApplication");
         return SUCCESS;
     }

     public void setServletRequest(HttpServletRequest request) {
         this.request=request;
         this.session=request.getSession();
         this.application=session.getServletContext();
     }
 }

 4、Servlet原始类型_Struts容器

 /**
  * @author qiuyong
  * 利用Struts容器创造原始类型的request、session、application
  */
 public class WebElementRealActionContextAction extends ActionSupport {
     HttpServletRequest request;
     HttpSession session;
     ServletContext application;

     public WebElementRealActionContextAction() {
         this.request=ServletActionContext.getRequest();
         this.session=request.getSession();
         this.application=session.getServletContext();
     }

     @Override
     public String execute() throws Exception {
         request.setAttribute("request","RealActionContextRequest");
         session.setAttribute("session","RealActionContextSession");
         application.setAttribute("application","RealActionContextApplication");
         return SUCCESS;
     }
 }

Struts2中Action的使用(Struts2_Action)

 六、包含其他xml文件配置

Struts2中Action的使用(Struts2_Action)

源码链接:http://pan.baidu.com/s/1bpAAz19

借鉴:来自马士兵视频|Struts2的思考

相关文章