在MVC模式中,我们对servlet、session、application三大对象并不陌生,是JSP的内置对象,对于我们在页面与servlet之间传值,交互等,有很大作用,到了Struts1中,我们依然可以建立一个**ACTION来继承Action,然后在public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response){}中使用request与response等,那么在Struts2中execute()方法不需要继承任何类,那么我们如何实现以上的作用呢,在Struts2中有三个方法,可以实现上面的功能。
1、使用Struts2的ActionContext对象
- public String execute(){
- ActionContext context = ActionContext.getContext();
- String path=this.NONE;
- if("liping".equals(this.getUsername())&"123456".equals(this.getUserpassword())){
- path="succ";
- context.put("name", this.getUsername());
- }else{
- context.getSession().put("message", "你登录失败了!");
- path="fail";
- }
- return path;
- }
2、使用Struts2的ServletActionContext对象
- public String execute(){
- ServletActionContext context=null;
- String path=this.NONE;
- if("liping".equals(this.getUsername())&"123456".equals(this.getUserpassword())){
- path="succ";
- //context.put("name", this.getUsername());
- context.getRequest().setAttribute("name", this.getUsername());
- }else{
- //context.getSession().put("message", "你登录失败了!");
- context.getRequest().getSession().setAttribute("message", "你登录失败了!");
- path="fail";
- }
- return path;
- }
3、需使用org.apache.struts2.interceptor org.apache.struts2.util包下的
//ServletRequestAware,SessionAware,ServletContextAware,ServletResponseAware
- public class LoginAction implements Action,ServletRequestAware,SessionAware,ServletContextAware,ServletResponseAware{
- private HttpServletRequest request;
- private Map<String, Object> session;
- private ServletContext application;
- private HttpServletResponse response;
- private String username;
- private String userpassword;
- private String abc;
- public String getAbc() {
- return abc;
- }
- public void setAbc(String abc) {
- this.abc = abc;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getUserpassword() {
- return userpassword;
- }
- public void setUserpassword(String userpassword) {
- this.userpassword = userpassword;
- }
- //使用传统的servlet session application对象
- //需使用org.apache.struts2.interceptor org.apache.struts2.util包下的
- //ServletRequestAware,SessionAware,ServletContextAware,ServletResponseAware
- public String execute1(){
- //ActionContext context = ActionContext.getContext();
- String path=this.NONE;
- if("liping".equals(this.getUsername())&"123456".equals(this.getUserpassword())){
- path="succ";
- request.setAttribute("name", this.getUsername());
- this.setAbc("欢迎你!");
- }else{
- //context.getSession().put("message", "你登录失败了!");
- session.put("message", "你登录失败了!");
- path="fail";
- }
- return path;
- }
- public void setServletRequest(HttpServletRequest request) {
- this.request=request;
- }
- public void setSession(Map<String, Object> session) {
- this.session=session;
- }
- public void setServletContext(ServletContext application) {
- this.application=application;
- }
- public void setServletResponse(HttpServletResponse response) {
- this.response=response;
- }
- }
这样,就可以实现了。一般第二种方法比较常用。