分享

Struts 2笔记:与Servlet耦合及动态方法调用

 xue_dong5437 2011-04-29

一、与Servlet耦合

在Struts 2的Action类里,有三种方式与Servlet进行耦合。

* ActionContext与Servlet耦合,但是它无法获得HttpServletResponse
  在Action类中,ActionContext.getContext().put("hello","hello");
  在页面上可以接收,${requestScope.hello}
* Aware方式(包括ServletRequestAware,ServletResponseAware)也可以与Servlet耦合
  建议使用ActionContext
* ServletActionContext方式也可以与Servlet耦合,这个类继承自ActionContext

 

1.LoginAction.java

package action;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {

 
 private static final long serialVersionUID = 1L;

 private String username;
 
 private String password;
 
 private HttpServletRequest httpServletRequest;
 
 private HttpServletResponse httpServletResponse;

 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;
 }
 
 @SuppressWarnings("unchecked")
 public String execute() throws Exception {
  if(!"admin".equals(username) || !"admin".equals(password)) {
   addFieldError("username", "用户名或密码错误!");
   return "failure";
  }
  else {
   
   Map map = ActionContext.getContext().getSession();
   ActionContext.getContext().put("ActionContext", "ActionContext方式耦合");
   map.put("user", "invalid");
   
   
   
   httpServletRequest.setAttribute("Aware", "Aware方式耦合");
   
   Cookie cookie = new Cookie("username", this.getUsername());
   //设置cookie最大存活时间
   //如果设置为正数,就是相应的存活时间;如果设置为负数,当浏览器被关闭的是,cookie就会被删除
   cookie.setMaxAge(1000);
   //第一次执行,cookie不会显示
   httpServletResponse.addCookie(cookie);
   
   
   
   HttpServletRequest request = ServletActionContext.getRequest();
   request.setAttribute("ServletActionContext", "ServletActionContext方式耦合");
   
   return "success";
  }
 }

 public void validate() {
  if(null == username || "".equals(username)) {
   addFieldError("username", "请填写用户名!");
  }
  if(null == password || "".equals(password)) {
   addFieldError("password", "请填写密码!");
  }
 }
 
 @SuppressWarnings("unchecked")
 public String login() throws Exception {
  if(!"admin".equals(username) || !"admin".equals(password)) {
   addFieldError("username", "用户名或密码错误!");
   return "failure";
  }
  else {
   
   Map map = ActionContext.getContext().getSession();
   ActionContext.getContext().put("ActionContext", "ActionContext方式耦合");
   map.put("user", "invalid");
   
   
   
   httpServletRequest.setAttribute("Aware", "Aware方式耦合");
   
   Cookie cookie = new Cookie("username", this.getUsername());
   //设置cookie最大存活时间
   //如果设置为正数,就是相应的存活时间;如果设置为负数,当浏览器被关闭的是,cookie就会被删除
   cookie.setMaxAge(1000);
   //第一次执行,cookie不会显示
   httpServletResponse.addCookie(cookie);
   
   
   
   HttpServletRequest request = ServletActionContext.getRequest();
   request.setAttribute("ServletActionContext", "ServletActionContext方式耦合");
   
   return "success";
  }
 }

 //这个方法被自动执行,是依赖注入
 //这是使用servlet的方法
 @Override
 public void setServletRequest(HttpServletRequest httpServletRequest) {
  // TODO Auto-generated method stub
  this.httpServletRequest = httpServletRequest;
 }

 @Override
 public void setServletResponse(HttpServletResponse httpServletResponse) {
  // TODO Auto-generated method stub
  this.httpServletResponse = httpServletResponse;
 }
 
}

 

这里,三种方式同时使用。在实际开发中,建议使用ActionContext和ServletActionContext

 

2.result.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
 用户名:${requestScope.username }<br>
 密  码:${requestScope.password }<br>
 ActionContext方式耦合:${requestScope.ActionContext }<br>
 Aware方式耦合:${requestScope.Aware }<br>
 Cookie(Aware方式耦合):${cookie.username.value }<br>
 ServletActionContext方式耦合:${requestScope.ServletActionContext }
</body>
</html>

 

 

运行结果:

Struts <wbr>2笔记:与Servlet耦合及动态方法调用

Struts <wbr>2笔记:与Servlet耦合及动态方法调用

二、动态方法调用 dynamic method invocation

有三种方式:

* 第一种,在struts.xml的action标签中,添加method属性;在action类里编写与之对应的方法,这个方法除

  了名字与execute方法不同,其他的返回值、抛出异常等都相同。这个类似Struts 1的DispatchAction


* 第二种,页面配置
  <s:form action="login!login.action" method="post">
  “!”之前的login是struts.xml的action里配置的名字,之后的login是对应于所执行的Action类的方法名,

   这个方法与execute除了名字不同外完全相同


* 第三种,使用通配符方式,不建议使用
  <action name="*Login" class="action.LoginAction" method="{1}">
  这里的通配符从1开始
  页面上,<s:form action="loginLogin" method="post">

 

三、结果返回类型

在struts-default.xml文件中定义了所有的结果返回类型,下面举一个例子,构造一个状态,返回一个页面。

<result name="success">
    <param name="status">404</param>
</result>
这样页面就会转到404

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多