Struts2_学习笔记(五)----访问Web元素、include、默认Action、Action总结

Struts2_学习笔记(五)—-访问Web元素、include、默认Action、Action总结

一、访问Web元素(参考Struts2_1200_AccessWebElements)

Struts2提供机制访问session、request、application
取Map类型request,session,application

1) 直接取session、request、application(较少使用)
代码如下:

  package com.bjsxt.struts2.user.action;

 import java.util.Map;

 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionSupport;

 public class LoginAction1 extends ActionSupport {

    private Map request;
    private Map session;
    private Map application;

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

    public String execute() {
        request.put("r1", "r1");
        session.put("s1", "s1");
        application.put("a1", "a1");
        return SUCCESS; 
    }


 }

2)使用DI(dependencyinjection—)依赖注入)或者称为IoC (inverse of control—)控制反转)—-(经常使用)
Action类分别实现RequestAware,SessionAware, ApplicationAware接口,并实现相应的setXXX()方法
代码如下:

 package com.bjsxt.struts2.user.action;

 import java.util.Map;

 import org.apache.struts2.interceptor.ApplicationAware;
 import org.apache.struts2.interceptor.RequestAware;
 import org.apache.struts2.interceptor.SessionAware;

 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionSupport;

 public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {

    private Map<String, Object> request;
    private Map<String, Object> session;
    private Map<String, Object> application;


    public String execute() {
        request.put("r1", "r1");
        session.put("s1", "s1");
        application.put("a1", "a1");
        return SUCCESS; 
    }

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

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

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


 }

取真实类型HttpServletRequest,HttpSession, ServletContext的引用:

1)直接直接取session、request、application(较少使用)
代码如下:

package com.bjsxt.struts2.user.action;

 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.ActionSupport;

 public class LoginAction3 extends ActionSupport {

    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;

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

    public String execute() {
        request.setAttribute("r1", "r1");
        session.setAttribute("s1", "s1");
        application.setAttribute("a1", "a1");
        return SUCCESS; 
    }

 }

2) 使用DI(dependencyinjection)或者称为IoC (inverse of control)—-(较少使用)
代码如下:

package com.bjsxt.struts2.user.action;

 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;

 import org.apache.struts2.interceptor.ServletRequestAware;

 import com.opensymphony.xwork2.ActionSupport;

 public class LoginAction4 extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;



    public String execute() {
        request.setAttribute("r1", "r1");
        session.setAttribute("s1", "s1");
        application.setAttribute("a1", "a1");
        return SUCCESS; 
    }



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

 }

二、include(参考Struts2_1300_IncludeModules)

可以将其他的xxx.xml包含到struts.xml中,其意义在于项目开发时,各个模块的配置文件可以单独编写,再最后的联调时,将所有的xml包含到struts.xml中,struts.xml中只进行公共的配置。

<include file="login.xml" />

三、默认Action(参考Struts2_1400_DefaultAction)

<package>中添加默认Action配置
<default-action-refname="index"></default-action-ref>

四、Action总结

  1. 实现一个Action的最常用方式:从ActionSupport继承
  2. DMI动态方法调用 !
  3. 通配符配置 * {1} {2} …
  4. *_*
  5. 接收参数的方法(一般用属性或者DomainModel来接收)
  6. 简单参数验证addFieldError
    一般不使用Struts2的UI标签
  7. 访问Web元素
    a)Map类型
    IoC
    依赖Struts2
    b)原始类型
    IoC
    依赖Struts2
  8. 包含文件配置
  9. 默认action处理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xulimessage

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值