Action访问ServletAPI
struts2的一个重大改良之处就是与ServletAPI的解耦。不过,对于Web应用而言,不访问ServletAPI几乎是不可能的。例如跟踪HTTPSession的状态。Struts2框架提供了一种轻松的方式来访问ServletAPI。通常需要访问的对象是HttpServletRequest,HttpServletSession,ServletContext,这三类也代表了JSP的内置对象中的request,session,application.

方法有:
1 Object get(Object key):类似于条用HttpServletRequest的getAttribute(String name)
2 Map getApplication:返回对象为map,模拟了ServletContext。
3 Static ActionContext().getContext(),获取ActionContext实例。
4 Map getParameters()
5 void setApplication(Map application)
6 void setSession(Map session)
用一个2方法,获取ActionContext实例,通过该对象的getApplication()和getSession()的put(key,value)方法,实现访问ServletAPI。

一个用ActionContext().getContext()获取ServletContext的例子
index.jsp

<%@page language="java" contentType=" text/html; charset=GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    
<head>
        
<title>struts2.demo1</title>
    
</head>

    
<body>
        
<s:form action="login">
            
<s:textfield name="username"/>
            
<s:password name="password"/>
            
<s:submit />
        
</s:form>
    
</body>
</html>

welcome.jsp

<%@page language="java" contentType=" text/html; charset=GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head><title>struts2.demo1</title></head>
  
  
<body>
  本站访问次数:${applicationScope.counter }
<br>
  ${sessionScope.user },你已经登陆
<br>
  ${requestScope.tip }
  
</body>
</html>

LoginAction类的execute()
public String execute() throws Exception {
        ActionContext ctx
=ActionContext.getContext();
        Integer counter
=(Integer) ctx.getApplication().get("counter");
        
if(counter==null){
            counter
=1;
        }
else{
            counter
=counter+1;
        }

        ctx.getApplication().put(
"counter", counter);
        ctx.getSession().put(
"user", getUsername());
        
if(getUsername().equals("ming")&&getPassword().equals("123456")){
            setTip(
"欢迎您,"+getUsername());
            
return SUCCESS;
        }
else{
            
return ERROR;
        }


另外,

虽然Struts2提供了ActionContext来访问ServletAPI,但是并不能直接获得ServletAPI的实例。但是Struts2提供了一下接口,
1 ServletContextAware:实现该接口的Action可以直接访问ServletContext实例。
2 ServletRequestAware:实现该接口的Action可以直接访问HttpServletRequest实例。
3 ServletResponseAware
例如
Action类
public class LoginAciton implements Action,ServletResponseAwre{
 private HttpServletResponse response;
 private String username;
 private String password;
 ....//setter getter
 public void serServletResponse(HttpServletResponse response){
  this.response=response;
 }
 public String execute() throws Exception{
  Cookie c = new Cookie("user",getUsername);
  c.setMaxAge(60*60);
  response.addCookie(c);
  return SUCCESS;
 }
}
//通过HttpServletResponse为系统添加Cookies对象。
jsp页面
<body>
从系统中读出Cooki值:${cookies.user.value}<br>
</body>

=======虽然可以在Action中获得HttpServleResponse对象,但是希望通过它来生成服务器相应是不可能的。即使在Struts2中获得了HttpServletResponse对象,也不要尝试直接在Action中对客户端生成相应。没有任何实际意义。

posted on 2009-11-05 01:03 南朝 阅读(220) 评论(0)  编辑  收藏 所属分类: Struts2

只有注册用户登录后才能发表评论。


网站导航:
 

统计