struts2与Servlet解耦
方式一:
action中关键代码:
HttpServletRequest request =ServletActionContext.getRequest();
request.setAttribute("username", "username_request");
Map sessionMap =ServletActionContext.getContext().getSession();
sessionMap.put("username", "username_session");
ServletContext sc = ServletActionContext.getServletContext();
sc.setAttribute("username", "username_application");
在项目开发可以先定义一个BaseAction:
public class BaseAction extends ActionSupport
{
private static final long serialVersionUID = 3437750672575106983L;
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
Map sessionMap=ServletActionContext.getContext().getSession();
ServletContext sc=ServletActionContext.getServletContext();
}
再通过继承BaseAction进行复用:
public class ContextAction extends BaseAction{
private static final long serialVersionUID = -6803448020752037119L;
public String test(){
System.out.println("context1");
request.setAttribute("username", "username_request");//request
sessionMap.put("username", "username_session");//session
sc.setAttribute("username", "username_application");//servlet
return "attr";
}
方式二:实现接口的方式
public class ContextAction extends ActionSupport
implements ServletRequestAware, ServletResponseAware, ServletContextAware, SessionAware{
HttpServletRequest request;
HttpServletResponse response;
Map sessionMap;
ServletContext sc;
public String test(){
System.out.println("ContextAction *********** test()");
request.setAttribute("username", "username_request");//request
sessionMap.put("username", "username_session");//sessopmn
sc.setAttribute("username", "username_application");//servlet
return "attr";
}
public void setSession(Map<String, Object> session) {
this.sessionMap = session;
}
public void setServletContext(ServletContext context) {
this.sc = context;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
在实际开发中先定义一个BaseAction2(基本action)
public class BaseAction2 extends ActionSupport
implements ServletRequestAware, ServletResponseAware, ServletContextAware, SessionAware{
private static final long serialVersionUID = 2131084805823257294L;
protected HttpServletRequest request;
protected HttpServletResponse response;
protected Map sessionMap;
protected ServletContext sc;
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.sessionMap=session;
}
public void setServletContext(ServletContext context) {
// TODO Auto-generated method stub
this.sc=context;
}
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
}
其他使用servlet对象的action通过继承BaseAction2
public class ContextAction extends BaseAction2{
private static final long serialVersionUID = -6803448020752037119L;
public String test(){
System.out.println("context1");
request.setAttribute("username", "username_request");//request
sessionMap.put("username", "username_session");//session
sc.setAttribute("username", "username_application");//servlet
return "attr";
}
显示页面:
使用struts标签需要引入:<%@ taglib uri="/struts-tags" prefix="s"%>
<%@pagelanguage="java"pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="description"content="This is my page">
</head>
<body>
<!--获取request -->
jstl取request值:${requestScope.username}<br>
<!-- struts2标签访问: -->
struts取request值:<s:property value="%{#request.username}" /><br>
<!--获取session -->
jstl取session值:${sessionScope.username}<br>
struts取session值:<s:property value="%{#session.username}" /><br>
<!-- 获取appliaction -->
jstl取application值:${applicationScope.username}<br>
struts取appliaction值:<s:property value="%{#application.username}"/>
</body>
</html>
使用jstl标签需引入:<%@tagliburi="http://java.sun.com/jstl/core_rt" prefix="c"%>
使用用:request对象:
action中:
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("username", "username_request");
request.setAttribute("listAll", DAOFactory.getPersonDAOInstance().queryAll());
jsp中:
${requestScope.username}<!--一个对象-->
使用struts自定义标签获取:
<!--集合对象-->
使用jstl获取:
<c:forEach items="${requestScope.listAll}"var="list">
<tr>
<td align="center">${list.id}</td><!-- 输出id -->
<td>${list.name}</td><!-- 输出name -->
<td>${list.password}</td><!-- 输出password -->
<td align="center"><a href="test!queryById?id=${list.id}">修改</a></td>
<td align="center"><a href="test!delete?id=${list.id}">删除</a></td>
</tr>
</c:forEach>
使用session对象:
action中:
HttpSession session_user = ServletActionContext.getRequest().getSession();
//把登录的用户名保存到session中
session_user.setAttribute("name",name);
jsp中:
${sessionScope.name}
使用context对象
action中:
ServletContextsc = ServletActionContext.getServletContext();
sc.setAttribute("username", "username_application");
jsp中:
<!-- 获取appliaction -->
${applicationScope.username}