实现Action
1.实现Action
(1)Action类里包含了对用户请求的处理逻辑,Action类也被称为业务控制器;
(2)Struts2不需要Action类实现任何接口或继承任何基类(但通常应该包含一个无参数的execute()方法);
(3)Struts2通常直接使用Action封装HTTP请求参数,所以,Action类中应该包含与请求参数对应的实例变量,并且为这些实例变量提供setter、getter方法;
(4)注意,系统是通过setter、getter方法来处理请求参数的;
(5)Action类中的实例变量,不仅可以封装请求参数,也可以封装处理结果,同样需要为该变量提供setter、getter方法;
(6)示例:
//Action可以不需要实现任何接口和继承任何类 public class TestAction { private String username; private String password; //必须为其提供setter、getter方法 public void setUsername(String username){ this.username = username; } public String getUsername(){ return this.username; } public void setPassword(String password){ this.password = password; } public String getPassword(){ return this.password; } //提供处理请求的execute方法 public String execute(){ return "success"; } }
2.Action接口和ActionSupport基类
(1)为了规范Action,Struts2中提供了一个Action接口,在这个接口中提供了五个字符串常量和一个无参的execute()方法;
(2)Struts2还提供了一个ActionSupport基类,这个基类是默认的Action实现类,使用继承此基类的方式可以简化Action的开发;
3.Action访问Servlet API(使用ActionContext访问)
(1)Action可以通过ActionContext类来访问Servlet API;
(2)Action通过ActionContext访问Servlet API示例:
JSP部分:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!-- 导入Struts2的标签库 --> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>模拟登陆页面</title> </head> <body> <s:form action="login2"> <s:textfield name="username" key="user"/> <s:password name="password" key="pass"/> <s:submit key="login"/> </s:form> </body> </html>
<%@ 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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陆成功</title> </head> <body> 本站访问次数为:${applicationScope.count}<br/> ${sessionScope.user} ,您已经登陆! ${requestScope.tip} </body> </html>
<%@ 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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陆失败</title> </head> <body> ${requestScope.tip } </body> </html>
Action部分:
import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; public class Test2Action implements Action{ //用于封装请求的成员变量 private String username; private String password; //setter、getter方法 public void setUsername(String username){ this.username = username; } public String getUsername(){ return this.username; } public void setPassword(String password){ this.password = password; } public String getPassword(){ return this.password; } @Override public String execute() throws Exception { //创建ActionContext对象 ActionContext context = ActionContext.getContext(); //通过ActionContext访问application范围的属性 Integer count = (Integer)context.getApplication().get("count"); if(count==null){ count = 1; }else{ count = count + 1; } //通过ActionContext设置Application范围的属性 context.getApplication().put("count", count); //通过ActionContext设置Session范围的属性 context.getSession().put("user", getUsername()); if(getUsername().equals("test")&&getPassword().equals("test")){ //通过ActionContext设置request范围的属性 context.put("tip","服务器提示:您已成功登陆!"); return SUCCESS; }else{ //通过ActionContext设置request范围属性 context.put("tip", "服务器提示:登录失败!"); return ERROR; } } }
xml配置片段:
<action name="test2"> <result>/WEB-INF/_3/login.jsp</result> </action> <action name="login2" class="_3_Action.Test2Action"> <result name="error">/WEB-INF/_3/error.jsp</result> <result name="success">/WEB-INF/_3/success.jsp</result> </action>
4.Action直接访问Servlet API
(1)为了使Action可以直接访问Servlet API Struts2提供了如下几个接口:
ServletContextAware:实现该接口的Action可直接访问Web应用的ServletContext实例;
ServletRequestAware:实现该接口的Action可直接访问用户请求的HttpServletRequest实例;
ServletResponseAware:实现该接口的Action可直接访问服务器响应的HttpServletResponse实例;
(2)示例:
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletResponseAware; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; public class Test3Action implements Action,ServletResponseAware{ private String username; private String password; private HttpServletResponse response; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } //实现XXXXXXAware需要实现的方法 @Override public void setServletResponse(HttpServletResponse response) { // TODO Auto-generated method stub this.response = response; } @Override public String execute() throws Exception { // TODO Auto-generated method stub //创建ActionContext对象 ActionContext context = ActionContext.getContext(); //通过ActionContext访问application范围的属性 Integer count = (Integer)context.getApplication().get("count"); if(count==null){ count = 1; }else{ count = count + 1; } //通过ActionContext设置application范围的属性 context.getApplication().put("count", count); //通过ActionContext设置session范围的属性 context.getSession().put("user", getUsername()); if(getUsername().equals("test")&&getPassword().equals("test")){ //通过response添加Cookie Cookie c = new Cookie("user",getUsername()); c.setMaxAge(60*60); response.addCookie(c); context.put("tip", "服务器提示:登陆成功!"); return SUCCESS; }else{ context.put("tip", "服务器提示:登陆失败!"); return ERROR; } } }
(3)注意:不要在Action中对客户端生成响应!
5.使用ServletActionContext访问Servlet API
(1)可以使用ServletActionContext工具类访问Servlet API;
(2)示例:修改前一个程序
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletResponseAware; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; public class Test3Action implements Action,ServletResponseAware{ private String username; private String password; private HttpServletResponse response; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } //实现XXXXXXAware需要实现的方法 @Override public void setServletResponse(HttpServletResponse response) { // TODO Auto-generated method stub this.response = response; } @Override public String execute() throws Exception { // TODO Auto-generated method stub //创建ActionContext对象 ActionContext context = ActionContext.getContext(); //通过ActionContext访问application范围的属性 Integer count = (Integer)context.getApplication().get("count"); if(count==null){ count = 1; }else{ count = count + 1; } //通过ActionContext设置application范围的属性 context.getApplication().put("count", count); //通过ActionContext设置session范围的属性 context.getSession().put("user", getUsername()); if(getUsername().equals("test")&&getPassword().equals("test")){ //通过response添加Cookie Cookie c = new Cookie("user",getUsername()); c.setMaxAge(60*60); ServletActionContext.getResponse().addCookie(c); //修改内容 context.put("tip", "服务器提示:登陆成功!"); return SUCCESS; }else{ context.put("tip", "服务器提示:登陆失败!"); return ERROR; } } }