一、struts2获取ServletApi
struts2获取ServletApi总共有4中方法获取,获取ServletApi主要就是获取servlet那几个对象,主要以HttpServletRequest对象为主进行总结。
1.1 通过ActionContext对象获取
话不多说,直接上代码:
public String method1(){
Map<String, Object> session = ActionContext.getContext().getSession();
//session存个值,在jsp页面获取
session.put("test", "testValue");
//这个方法相当于获取的是HttpServletRequest对象的attributes
Map<String,Object> request=
(Map<String, Object>) ActionContext.getContext().get("request");
//获取application
Map<String, Object> application = ActionContext.getContext().getApplication();
//获取, parameters 也是一个Map,HttpParameters实现了Map<String, Parameter>, Cloneable接口
HttpParameters parameters = ActionContext.getContext().getParameters();
Map<String, Parameter> paraMap=(Map<String, Parameter>)parameters;
//获取url传过来的以key为key的参数,并输出到控制台
String value = paraMap.get("key").getValue();
System.out.println("value====="+value);
return Action.SUCCESS;
}
struts配置:
<action name="method1" class="com.david.struts2.servletApi.GetServletApiAction" method="method1">
<result>/jsp/4servletApi/success.jsp</result>
</action>
success.jsp
<%@ 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>Insert title here</title>
</head>
<body>
<!-- 将后台设置到session中的值取出来 -->
${sessionScope.test}
</body>
</html>
访问结果:
1.2 通过ActionContext.getContext()直接获取request对象,当然其它对象也可以获取(session对象除外)
public String method2(){
//通过ActionContext.getContext()直接获取request对象,当然其它对象也可以获取(session对象除外)
HttpServletRequest req = (HttpServletRequest) ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
HttpServletResponse resp = (HttpServletResponse) ActionContext.getContext().get(StrutsStatics.HTTP_RESPONSE);
//操作一下seesion
req.getSession().setAttribute("test", "method2");
resp.addCookie(new Cookie("mycookie", "method2"));
return Action.SUCCESS;
}
补充:StrutsStatics静态常量的取值部分源码如下:
struts配置都返回sucess.jsp页面,访问结果:
1.3 通过实现接口的方式
单独的一个action类,此种方式侵入性强,耦合度也高,不建议使用
package com.david.struts2.servletApi;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.Action;
public class ServletApiAction implements ServletRequestAware, ServletResponseAware {
private HttpServletRequest req;
private HttpServletResponse resp;
@Override
public void setServletResponse(HttpServletResponse response) {
this.resp=response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.req=request;
}
public String method3(){
//验证一下这2个对象是否有效
req.getSession().setAttribute("test", "method3");
resp.addCookie(new Cookie("mycookie", "method3"));
return Action.SUCCESS;
}
}
访问结果:
1.4直接使用ServletActionContext获取
这种方式简单快捷,推荐使用。
public String method4(){
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse resp = ServletActionContext.getResponse();
//验证一下这2个对象是否有效
req.getSession().setAttribute("test", "method4");
resp.addCookie(new Cookie("mycookie", "method4"));
return Action.SUCCESS;
}
访问结果:
哎,今天有点事耽搁了,开始比较晚,只总结了这么一点,要加油了。