.Struts2的Action可以不需实现任何接口或继承任何类,就是一个包含了execute()方法的POJO,下面是一个简单的Action的实现
- package com;
- import com.opensymphony.xwork2.ActionContext;
- public class LogonAction {
- private String userName;
- private String userPwd;
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getUserPwd() {
- return userPwd;
- }
- public void setUserPwd(String userPwd) {
- this.userPwd = userPwd;
- }
- public String execute(){
- if(userName.equals("callan") && userPwd.equals("fjf")){
- // ActionContext的getSession()返回的不是一个HttpSession,是一个Map
- // 但struts2的拦截器会负责该session与httpsession的转换
- ActionContext.getContext().getSession().put("userName", "callan");
- return "succee";
- } else {
- return "error";
- }
- }
- }
package com;
import com.opensymphony.xwork2.ActionContext;
public class LogonAction {
private String userName;
private String userPwd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String execute(){
if(userName.equals("callan") && userPwd.equals("fjf")){
// ActionContext的getSession()返回的不是一个HttpSession,是一个Map
// 但struts2的拦截器会负责该session与httpsession的转换
ActionContext.getContext().getSession().put("userName", "callan");
return "succee";
} else {
return "error";
}
}
}
配置文件
- <package name="logon" extends="struts-default">
- <action name="logon" class="com.LogonAction">
- <result name="succee">/logon/welcome.jsp</result>
- <result name="error">/logon/error.jsp</result>
- </action>
- </package>
当请求时,Struts运行时根据struts.xml里的Action映射,实例化com.LogonAction类,
默认情况下会调用其execute方法。不过我们可以通过配置改变他的方法,配置如下:
- <action name="logon" method="userLogon" class="com.LogonAction">
- <result name="succee">/logon/welcome.jsp</result>
- <result name="error">/logon/error.jsp</result>
- </action>
Method配置调用的方法名为userLogon,这时当请求logon.action时会调用userLogon()方法,不会在去调用默认的execute()方法
2. 虽然Action可以不实现任何接口或继承任何类,但大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重载execute()方法.(比如说我们需要request和response对象时)
- public class LogonAction2 extends ActionSupport{
- private String userName;
- private String userPwd;
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getUserPwd() {
- return userPwd;
- }
- public void setUserPwd(String userPwd) {
- this.userPwd = userPwd;
- }
- public String execute(){
- // 得到request对象
- HttpServletRequest request = ServletActionContext.getRequest();
- HttpServletResponse response = ServletActionContext.getResponse();
- return SUCCESS;
- }
- }
public class LogonAction2 extends ActionSupport{
private String userName;
private String userPwd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String execute(){
// 得到request对象
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
return SUCCESS;
}
}
SUCCESS是在ActionSupport定义的,同时还定义了ERROR,INPUT,LOGIN,NONE,在配置result的name时,他们的值分别为success,error,input,login,none 3.继承ActionSupport类能得到request,response等对象,通过ioc方式也能得到request,response(实现ServletRequestAware, ServletResponseAware接口)
- public class LogonAction implements SessionAware, ServletRequestAware, ServletResponseAware {
- private String userName;
- private String userPwd;
- private Map session;
- private HttpServletRequest request;
- private HttpServletResponse response;
- public String execute(){
- // session是一个map
- // struts2的拦截器会负责该session与httpsession的转换
- // 在页面上取值和在session中取一样.session.getAttribute(userName)
- session.put("userName", "callan");
- return "succee";
- }
- public void setSession(Map session) {
- this.session = session;
- }
- public void setServletRequest(HttpServletRequest request) {
- this.request = request;
- }
- public void setServletResponse(HttpServletResponse response) {
- this.response = response;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getUserPwd() {
- return userPwd;
- }
- public void setUserPwd(String userPwd) {
- this.userPwd = userPwd;
- }
- }