Struts2学习笔记——AccessWebElements(访问web元素)

可以使用Map类型来接收HttpServletRequest  ServletContext和HttpSession。

struts.xml:





  	
  	
  	
  	
  	
  	 
  	
        
             /user_login_success.jsp  
        
    


然后有四种不同的方式来对这三个进行操作:

LoginAction1:

package com.smile.struts2.login.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.smile.struts2.front.model.User;

public class LoginAction1 extends ActionSupport{
	
	private Map request;
	private Map session;
	private Map application;
	private User user;
	
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public LoginAction1(){
		request = (Map)ActionContext.getContext().get("request");
		session = ActionContext.getContext().getSession();
		application = ActionContext.getContext().getApplication();
	}
	
	public String execute(){
		System.out.println(user.getName()+" "+user.getPassword());
		request.put("r1", "帅气");
		session.put("s1", "帅气");
		application.put("a1", "帅气");
		return SUCCESS;
	}
	
}
LoginAction2:

package com.smile.struts2.login.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.ActionSupport;

public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
    
    private Map request;
    private Map session;
    private Map application;
    
    
    
    public String execute(){
        request.put("r1", "帅气");
        session.put("s1", "帅气");
        application.put("a1", "帅气");
        return SUCCESS;
    }



    public void setRequest(Map request) {
        this.request = request;
    }



    public void setSession(Map session) {
        this.session = session;
    }



    public void setApplication(Map application) {
        this.application = application;
    }
    
}


LoginAction3:

package com.smile.struts2.login.action;

import java.util.Map;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
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", "帅气");
		session.setAttribute("s1", "帅气");
		application.setAttribute("a1", "帅气");
		return SUCCESS;
	}
	
}
LoginAction4:

package com.smile.struts2.login.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", "帅气");
		session.setAttribute("s1", "帅气");
		application.setAttribute("a1", "帅气");
		return SUCCESS;
	}


	public void setServletRequest(HttpServletRequest servletRequest) {
		this.request = servletRequest;
		this.session = servletRequest.getSession();
		this.application = servletRequest.getSession().getServletContext();
	}
	
}
一般我们使用第二种。

index.jsp文件如下:


<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>

<% String context = request.getContextPath(); %>





Insert title here


取得Map类型request,session,application,真实类型 HttpServletRequest, HttpSession, ServletContext的引用:
  1. 前三者:依赖于容器
  2. 前三者:IOC
  3. (只用这种)
  4. 后三者:依赖于容器
  5. 后三者:IOC

用户名: 密码:
这样我们在返回的结果jsp文件中可以这样接收:


<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>




Insert title here


	User Login Success!
	
| <%=request.getAttribute("r1") %>
| <%=session.getAttribute("s1") %>
| <%=application.getAttribute("a1") %>




如果为了防止配置文件的混淆,可以使用include的方式:

struts.xml:





  	
  	
  	
  	
  	
  	
	

login.xml:





  	
        
             /user_login_success.jsp  
        
    



与上面的效果也是一样的。

设置默认的action可以使用:



你可能感兴趣的:(struts2)