ssh学习:struts2的获取web资源模式
struts2中获取web资源模式
在前面已经讲到怎么使用struts2的数据封装,这是在一定的条件下是可行的的也是很简单的。但是呢,远远还没有达到web开发的需求。下面讲一下怎么在action类中获取web资源模式。
获取web的资源模式有两种方法:使用拦截器动态获取,另外一中就是使用静态对象获取web资源模式。
使用拦截器动态获取web资源模式:
方法一:
实现各个web模式的拦截器对应的拦截器接口
ServletRequestAware, ServletResponseAware, ServletContextAware
在请求到达之后,先被拦截器拦截,在触发action的相关方法
示例代码:
public class FirstAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware { private ServletRequest request; private ServletResponse response; private ServletContext context; @Override public String execute() throws Exception { String name = request.getParameter("name"); System.out.println("name:" + name); response.getWriter().write("hello gaosi!"); return SUCCESS; } @Override public void setServletRequest(HttpServletRequest arg0) { request = arg0; } @Override public void setServletContext(ServletContext arg0) { context = arg0; } @Override public void setServletResponse(HttpServletResponse arg0) { response = arg0; } }
这样我们就能轻松的获取到web资源模式了
方法二:是实现拦截器RequestAware接口,在serRequest方法中的map对象当中就可以全部的获取到Web资源模式对象了,只要从map中提取出来就可以直接的使用了
示例代码:
package com.usc.geowind.lilin.action; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.ws.spi.http.HttpContext; import org.apache.struts2.StrutsStatics; import org.apache.struts2.interceptor.RequestAware; import com.opensymphony.xwork2.ActionSupport; public class SecAction extends ActionSupport implements RequestAware { private HttpServletRequest request; private HttpServletResponse response; private ServletContext context; @Override public String execute() throws Exception { System.out.println("secAction name:" + request.getParameter("name")); return SUCCESS; } @Override public void setRequest(Map<String, Object> arg0) { request = (HttpServletRequest) arg0.get(StrutsStatics.HTTP_REQUEST); response = (HttpServletResponse) arg0.get(StrutsStatics.HTTP_RESPONSE); context = (ServletContext) arg0.get(StrutsStatics.SERVLET_CONTEXT); } }
通过拦截器获取web资源模式就基本介绍完了,下面看些静态对象获取web资源模式。
静态对象获取web资源模式:
使用静态的方式获取web资源模式不需要实现任何借口也不需要使用拦截器。具体的方法如下:
方法一:使用ActionContext.getContext()方式;
public String execute() throws Exception { //ActionContext相当去Map一样,存储着web全部的资源模式 ActionContext context = ActionContext.getContext(); ServletRequest resqust = (ServletRequest) context.get(ServletActionContext.HTTP_REQUEST); ServletResponse response = (ServletResponse) context.get(ServletActionContext.HTTP_RESPONSE); ServletContext sContext = (ServletContext) context.get(ServletActionContext.SERVLET_CONTEXT); String name = resqust.getParameter("name"); System.out.println("name:" + name); return SUCCESS; }
方法二:
使用ServletActionContext获取web资源模式
示例代码:
public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); ServletContext servletContext = ServletActionContext.getServletContext(); String name = request.getParameter("name"); System.out.println("name:" + name); return SUCCESS; }
相关推荐
projava 2020-11-14
WanKaShing 2020-11-12
airfish000 2020-09-11
tryfind 2020-09-14
yegen00 2020-09-10
kkaazz 2020-09-03
风语者 2020-09-02
BraveWangDev 2020-08-19
lichuanlong00 2020-08-15
gsl 2020-08-15
pandaphinex 2020-08-09
yhuihon 2020-08-09
CheNorton 2020-08-02
xiangqiao 2020-07-28
hpujsj 2020-07-26
hpujsj 2020-07-26
sshong 2020-07-19
BraveWangDev 2020-07-19
annan 2020-07-18