Struts2实现与Servlet API的交互

struts2中提供了Map类型的request,session,与application,可以从ActionContext对象中获得.

(1)实例ActionContext,通常通过ActionContext对象提供的getContext()方法获取

public static ActionContext getContext(),该方法是静态的,可以直接调用

ActionContext=ActionContext.getContext()

(2)获取Map类型的request

Map request=ActionContext.getContext().get("request");

(3)获取Map类型的session

Map request=ActionContext.getContext().getSession();

(4)获取Map类型的application

Map request=ActionContext.getContext().getApplication();

public class LoginAction {
	
	private String username;
	private String password;
	
	public String login(){
		return "login";
	}
	
	public String logined(){
		
		if(StringUtil.isNotEmpty(username)&&StringUtil.isNotEmpty(password)){
			ActionContext.getContext().getSession().put("username",username);
			return "loginSuccess";
		}else{
			return "error";
		}
	}
	
	//生成setter和getter
	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;
	}

	
}

 虽然在struts2中使用ActionContext来访问 Servlet API,但这种方法不能直接获得ServletAPI实例,为了再action中直接访问 Servlet API可以通过以下方法直接访问

(1)ServletContextAware:实现该接口的Action可以直接访问Web应用的ServletContext实例

(2)ServletRequestAware:实现该接口的Action可以直接访问Web应用的HttpServletRequest实例

(3)ServletResponseAware:实现该接口的Action可以直接访问Web应用的HttpServletResponse实例

public class LoginAction implements ServletResponseAware{
	
	private String username;
	private String password;
	private HttpServletResponse response;
	public String login(){
		return "login";
	}
	
	public String logined(){
		//记录一个网站访问次数
		ActionContext context=ActionContext.getContext();
		Integer count=(Integer)context.getApplication().get("count");
		if(count==null){
			count=1;
		}else{
			count+=1;
		}
		context.getApplication().put("count", count);
		if(StringUtil.isNotEmpty(username)&&StringUtil.isNotEmpty(password)){
			Cookie cookie=new Cookie("username", username);
			cookie.setMaxAge(60*60);
			response.addCookie(cookie);
			context.getSession().put("username",username);
			return "loginSuccess";
		}else{
			return "error";
		}
	}
	
	//生成setter和getter
	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;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response=response;
	}

}

 除上述两种方法外,还可以通过ServletActionContext访问Servlet API

public class LoginAction{
	
	private String username;
	private String password;
	private HttpServletResponse response;
	public String login(){
		return "login";
	}
	
	public String logined(){
		//记录一个网站访问次数
		ActionContext context=ActionContext.getContext();
		Integer count=(Integer)context.getApplication().get("count");
		if(count==null){
			count=1;
		}else{
			count+=1;
		}
		context.getApplication().put("count", count);
		if(StringUtil.isNotEmpty(username)&&StringUtil.isNotEmpty(password)){
			Cookie cookie=new Cookie("username", username);
			cookie.setMaxAge(60*60);
			ServletActionContext.getResponse().addCookie(cookie);
			context.getSession().put("username",username);
			return "loginSuccess";
		}else{
			return "error";
		}
	}
	
	//生成setter和getter
	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;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值