一:servlet API
1)完全解耦接口:
使用ActionContext类进行相关操作:
1 package jd.com.actioncontex; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 import java.lang.reflect.Array; 6 import java.util.Map; 7 import java.util.Set; 8 9 public class actioncon extends ActionSupport{ 10 11 12 public String getInfo(){ 13 //获取actioncontex对象通过静态方法getcontext 14 ActionContext actionContext=ActionContext.getContext(); 15 // 获取requet参数 16 Map<String, Object> map=actionContext.getParameters(); 17 Set<String> k=map.keySet(); 18 for(String i:k){ 19 System.out.println(i); 20 Object[] objects= (Object[]) map.get(i); 21 System.out.println(Array.get(objects,0)); 22 } 23 //给servletcontex设置值 24 Map<String,Object> con=actionContext.getApplication(); 25 con.put("oop",567); 26 27 // 给session设置值 28 Map<String,Object> session=actionContext.getSession(); 29 session.put("oop",9000); 30 //给request设置值 31 actionContext.put("oop",900); 32 return "ok"; 33 } 34 }
jsp:
1 <%-- 2 Created by IntelliJ IDEA. 3 User: liumeide 4 Date: 2018/4/11 5 Time: 14:27 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>Title</title> 12 </head> 13 <body> 14 15 ${oop} 16 17 ${sessionScope.oop}<br> 18 ${applicationScope.oop}<br> 19 ${requestScope.get("oop")} 20 </body> 21 </html>
原生servletAPI这个比较常用(直接获取相应的域对象)
通过ServletActionContext对象获取相应的域对象。
1 package jd.com.or; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 import org.apache.struts2.ServletActionContext; 6 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import javax.servlet.http.HttpSession; 10 11 public class or extends ActionSupport { 12 /** 13 * 原生servletAPI 14 * @return 15 * @throws Exception 16 */ 17 @Override 18 public String execute() throws Exception { 19 // 获取HttpServletResponse 20 HttpServletResponse httpServletResponse= ServletActionContext.getResponse(); 21 httpServletResponse.getWriter(); 22 // 获取HttpServletRequest对象 23 HttpServletRequest httpServletRequest=ServletActionContext.getRequest(); 24 httpServletRequest.setAttribute("oop","121313131"); 25 // 获取session 26 HttpSession httpSession=httpServletRequest.getSession(); 27 httpSession.setAttribute("oop","oopsdadad"); 28 return SUCCESS; 29 } 30 }