赤壁国色战魂四维:Struts 2笔记:与Servlet耦合及动态方法调用

来源:百度文库 编辑:九乡新闻网 时间:2024/05/01 03:38:49
一、与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"%>




Insert title here


用户名:${requestScope.username }

密  码:${requestScope.password }

ActionContext方式耦合:${requestScope.ActionContext }

Aware方式耦合:${requestScope.Aware }

Cookie(Aware方式耦合):${cookie.username.value }

ServletActionContext方式耦合:${requestScope.ServletActionContext }


运行结果:


二、动态方法调用 dynamic method invocation
有三种方式:
* 第一种,在struts.xml的action标签中,添加method属性;在action类里编写与之对应的方法,这个方法除
了名字与execute方法不同,其他的返回值、抛出异常等都相同。这个类似Struts 1的DispatchAction
* 第二种,页面配置

“!”之前的login是struts.xml的action里配置的名字,之后的login是对应于所执行的Action类的方法名,
这个方法与execute除了名字不同外完全相同
* 第三种,使用通配符方式,不建议使用

这里的通配符从1开始
页面上,
三、结果返回类型
在struts-default.xml文件中定义了所有的结果返回类型,下面举一个例子,构造一个状态,返回一个页面。

404

这样页面就会转到404