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总结
- 实现一个Action的最常用方式:从ActionSupport继承
- DMI动态方法调用 !
- 通配符配置 * {1} {2} …
*_*
- 接收参数的方法(一般用属性或者DomainModel来接收)
- 简单参数验证addFieldError
一般不使用Struts2的UI标签 - 访问Web元素
a)Map类型
IoC
依赖Struts2
b)原始类型
IoC
依赖Struts2 - 包含文件配置
- 默认action处理