Struts2 --接口注入
1.原生方式ServletActionContext对象
public class RequestDemo01 extends ActionSupport { public String login(){ System.out.println("login-------"); //1.获取请求路径 String requestURI = ServletActionContext.getRequest().getRequestURI(); System.out.println(requestURI); System.out.println("=========="); //2.获取Struts中的ActionContext对象 ActionContext context = ActionContext.getContext(); //ActionContext中的方法可以 HttpParameters parameters = context.getParameters(); for (String s : parameters.keySet()) { System.out.println(s+":"+parameters.get(s)); } System.out.println("=========="); //3.request取值 HttpServletRequest request= ServletActionContext.getRequest(); Map<String,String[]> map = request.getParameterMap(); for (String s : map.keySet()) { System.out.println(s.toString()+" "+ Arrays.toString( map.get(s))); } //4.在域对象中存值 request.getSession().setAttribute("sessions","sessionValue"); request.setAttribute("requests","requestValue"); ServletActionContext.getServletContext().setAttribute("appliactions","applicationValue"); return "yuduixiang"; } }
2.实现ServletRequestAware接口,获取HttpServletRequest对象
public class RequestDemo02 extends ActionSupport implements ServletRequestAware { private HttpServletRequest request; @Override public String execute() throws Exception { System.out.println("进入execute+++++++"); Map<String,String[]> map = request.getParameterMap(); for (String key : map.keySet()) { System.out.println(key+"---"+ Arrays.toString(map.get(key))); } request.getSession().setAttribute("sessions","sessionValue"); request.setAttribute("requests","requestValue"); ServletActionContext.getServletContext().setAttribute("appliactions","applicationValue"); return "yuduixiang"; } @Override public void setServletRequest(HttpServletRequest request) { this.request=request; } }
3.实现ServletContextAware,接口获取ServletContext对象
import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.util.ServletContextAware; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.Map; public class RequestDemo02 extends ActionSupport implements ServletRequestAware, ServletContextAware { private HttpServletRequest request; private ServletContext context; @Override public String execute() throws Exception { System.out.println("进入execute+++++++"); Map<String,String[]> map = request.getParameterMap(); for (String key : map.keySet()) { System.out.println(key+"---"+ Arrays.toString(map.get(key))); } request.getSession().setAttribute("sessions","sessionValue"); request.setAttribute("requests","requestValue"); ServletActionContext.getServletContext().setAttribute("appliactions","applicationValue"); context.setAttribute("appliactions2","applicationValue2"); return "yuduixiang"; } @Override public void setServletRequest(HttpServletRequest request) { this.request=request; } @Override public void setServletContext(ServletContext context) { this.context=context; } }
*Servlet是单例的,多个程序访问servlet指挥创建一个servlet对象,线程不安全
*Action是多例的,一次请求会创建一个Action对象,线程安全
结果页面
1.全局结果页面
指在包中配置一次,其它在这个包中的所有action只要返回了这个值,都可以跳转到这个页面。
针对这个报下所有的action都有效。
2.局部结果页面
指只能对当前的action有效。
针对当前的action都有效。
<package name="demo01" extends="struts-default" namespace="/"> <!--全局结果页面,包下所有的方法返回值userlist都可以到/jsp/p02.jsp页面--> <global-results> <result name="userlist">/jsp/p02.jsp</result> </global-results> <!--通配符配置的方式--> <action name="user_*" class="com.action.RequestDemo01" method="{1}"> <result name="yuduixiang">/jsp/p01.jsp </result> <allowed-methods>login</allowed-methods> </action> <action name="user_*" class="com.action.RequestDemo02" method="{1}"> <result name="yuduixiang">/jsp/p01.jsp </result> <allowed-methods>execute</allowed-methods> </action> </package>
3.Result标签配置
用于配置页面的跳转
属性:
name:逻辑视图属性 ,默认值success
type:页面跳转类型。
dispatcher 默认值,请求转发到jsp;
redirect 重定向到jsp;
chian 转发到Action;
redirectAction 重定向到Action;
stream 提供文件下载的功能;