struts2-登陆(2)
通常在我们的WEB应用中需要访问request,session和application(即ServletContext)。下面我们在前面登陆应用的基础上访问这些Servlet API,访问方式主要分为两类:与Servlet API解耦的访问方式和与Servlet API耦合的访问方式。 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
一.与Servlet API解耦的访问方式
1.通过Struts2提供的Map对象来访问
package org.leno.struts2.action;
import java.util.Map;
import org.leno.struts2.model.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction4 implements Action, ModelDriven<User> {
private User user = new User();
public String execute() throws Exception {
if ("leno".equals(user.getUsername())
&& "123".equals(user.getPassword())) {
ActionContext context = ActionContext.getContext();
Map request = (Map) context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
request.put("greeting", "欢迎访问老许的博客(http://www.blog.csdn.net/lenotang)");
session.put("USER_INFO", user);
Integer count = (Integer) application.get("count");
if (null == count) {
count = 1;
} else {
count++;
}
application.put("count", count);
return SUCCESS;
} else {
return ERROR;
}
}
public User getModel() {
return user;
}
}
编写success2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />3C//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>
<h1>${sessionScope.USER_INFO.username},${requestScope.greeting}.<br>
本站的访问量为${applicationScope.count}!</h1>
</body>
</html>
2.通过接口注入来访问
package org.leno.struts2.action;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.leno.struts2.model.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction5 implements Action, ModelDriven<User>, RequestAware,SessionAware, ApplicationAware {
private Map request;
private Map session;
private Map application;
private User user = new User();
public String execute() throws Exception {
if ("leno".equals(user.getUsername())
&& "123".equals(user.getPassword())) {
request.put("greeting", "欢迎访问老许的博客(http://www.blog.csdn.net/lenotang)");
session.put("USER_INFO", user);
Integer count = (Integer) application.get("count");
if (null == count) {
count = 1;
} else {
count++;
}
application.put("count", count);
return SUCCESS;
} else {
return ERROR;
}
}
public User getModel() {
return user;
}
public void setRequest(Map request) {
this.request = request;
}
public void setSession(Map session) {
this.session = session;
}
public void setApplication(Map application) {
this.application = application;
}
}
二: 与Servlet API耦合的访问方式
1.通过ServletActionContext访问
package org.leno.struts2.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.leno.struts2.model.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction6 implements Action, ModelDriven<User> {
private User user = new User();
public String execute() throws Exception {
if ("leno".equals(user.getUsername())
&& "123".equals(user.getPassword())) {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
ServletContext context = ServletActionContext.getServletContext();
request.setAttribute("greeting", "欢迎访问老许的博客(http://www.blog.csdn.net/lenotang)");
session.setAttribute("USER_INFO", user);
Integer count = (Integer) context.getAttribute("count");
if (null == count) {
count = 1;
} else {
count++;
}
context.setAttribute("count", count);
return SUCCESS;
} else {
return ERROR;
}
}
public User getModel() {
return user;
}
}
2.通过接口注入来访问
package org.leno.struts2.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import org.leno.struts2.model.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction7 implements Action, ModelDriven<User>,
ServletRequestAware, ServletContextAware {
private User user = new User();
private HttpServletRequest request;
private ServletContext context;
public String execute() throws Exception {
if ("leno".equals(user.getUsername())
&& "123".equals(user.getPassword())) {
HttpSession session = request.getSession();
request.setAttribute("greeting",
"欢迎访问老许的博客(http://www.blog.csdn.net/lenotang)");
session.setAttribute("USER_INFO", user);
Integer count = (Integer) context.getAttribute("count");
if (null == count) {
count = 1;
} else {
count++;
}
context.setAttribute("count", count);
return SUCCESS;
} else {
return ERROR;
}
}
public User getModel() {
return user;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletContext(ServletContext context) {
this.context = context;
}
}
最后别忘了导入struts2的jar包和在web.xml上配置struts2的过滤器。