struts2 servlet为什么执行execute方法
2016-08-26 · 知道合伙人软件行家
关注
展开全部
一、IoC方式
在struts2框架中,可以通过IoC方式将servlet对象注入到Action中,通常需要Action实现以下接口:
a. ServletRequestAware:
实现该接口的Action可以直接访问Request对象,该接口中提供void setServletRequest(HttpServletRequest request)
方法,实现此接口的Action控制类通过setServletRequestHttpServlet(HttpServlet request)方法将request对象注入到Action中。
b. ServletResponseAware:
Action类通过实现ServletResponseAware接口可以直接访问Response对象,与ServletRequestAware接口相似该接口通过提供void setServletResponse(HttpServlet resonse)将response对象注入到Action中,以便于可以直接使用。
c. SessionAware:
实现该接口的Action可以直接访问session对象,该接口中提供void setSession(Map map)方法,注入原理与request和response原理相同,都是通过实现对应接口的set方法将session对象注入到Action中。
实例:
[java] view plain copy print?
/**
* @author zx
*/
package com.zhouxiang.action;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author zx
*
*/
public class LoginAction1 extends ActionSupport implements ServletRequestAware{
private String userName;
private String password;
private HttpServletRequest request;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
this.request=arg0;
}
public String validateUser()throws Exception
{
if(getUserName().endsWith("QQ")&&getPassword().equals("123"))
{
//调用ServletActionContext的getRequest()方法获取request对象
HttpServletRequest request=ServletActionContext.getRequest();
//调用request对象的getSession()方法获取session对象
HttpSession session=request.getSession();
//调用session对象的方法设置数据
session.setAttribute("username",this.userName);
return SUCCESS;
}
else
{
return INPUT;
}
}
}
二、非IoC方式
在非IoC方式中,Struts2提供ServletActionContext类帮助获得Servlet。
访问request对象
通过ServletActionContext中的静态方法 getRequest()获取web应用程序的request对象。
访问response对象
通过ServletActionContext中的静态方法getResponse()获取web应用程序中的response对象。
实例:
[java] view plain copy print?
/**
* @author zx
*/
package com.zhouxiang.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author zx
*
*/
public class LoginAction1 extends ActionSupport {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String validateUser()throws Exception
{
if(getUserName().endsWith("QQ")&&getPassword().equals("123"))
{
//调用ServletActionContext的getRequest()方法获取request对象
HttpServletRequest request=ServletActionContext.getRequest();
//调用request对象的getSession()方法获取session对象
HttpSession session=request.getSession();
//调用session对象的方法设置数据
session.setAttribute("username",this.userName);
return SUCCESS;
}
else
{
return INPUT;
}
}
}
在struts2框架中,可以通过IoC方式将servlet对象注入到Action中,通常需要Action实现以下接口:
a. ServletRequestAware:
实现该接口的Action可以直接访问Request对象,该接口中提供void setServletRequest(HttpServletRequest request)
方法,实现此接口的Action控制类通过setServletRequestHttpServlet(HttpServlet request)方法将request对象注入到Action中。
b. ServletResponseAware:
Action类通过实现ServletResponseAware接口可以直接访问Response对象,与ServletRequestAware接口相似该接口通过提供void setServletResponse(HttpServlet resonse)将response对象注入到Action中,以便于可以直接使用。
c. SessionAware:
实现该接口的Action可以直接访问session对象,该接口中提供void setSession(Map map)方法,注入原理与request和response原理相同,都是通过实现对应接口的set方法将session对象注入到Action中。
实例:
[java] view plain copy print?
/**
* @author zx
*/
package com.zhouxiang.action;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author zx
*
*/
public class LoginAction1 extends ActionSupport implements ServletRequestAware{
private String userName;
private String password;
private HttpServletRequest request;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
this.request=arg0;
}
public String validateUser()throws Exception
{
if(getUserName().endsWith("QQ")&&getPassword().equals("123"))
{
//调用ServletActionContext的getRequest()方法获取request对象
HttpServletRequest request=ServletActionContext.getRequest();
//调用request对象的getSession()方法获取session对象
HttpSession session=request.getSession();
//调用session对象的方法设置数据
session.setAttribute("username",this.userName);
return SUCCESS;
}
else
{
return INPUT;
}
}
}
二、非IoC方式
在非IoC方式中,Struts2提供ServletActionContext类帮助获得Servlet。
访问request对象
通过ServletActionContext中的静态方法 getRequest()获取web应用程序的request对象。
访问response对象
通过ServletActionContext中的静态方法getResponse()获取web应用程序中的response对象。
实例:
[java] view plain copy print?
/**
* @author zx
*/
package com.zhouxiang.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author zx
*
*/
public class LoginAction1 extends ActionSupport {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String validateUser()throws Exception
{
if(getUserName().endsWith("QQ")&&getPassword().equals("123"))
{
//调用ServletActionContext的getRequest()方法获取request对象
HttpServletRequest request=ServletActionContext.getRequest();
//调用request对象的getSession()方法获取session对象
HttpSession session=request.getSession();
//调用session对象的方法设置数据
session.setAttribute("username",this.userName);
return SUCCESS;
}
else
{
return INPUT;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询