使用Struts 2 开发,需要大量使用Action类,并在struts.xml文件中配置Action。
Struts 2 通常直接使用Action来封装HTTP请求参数,因此,Action类还要有与请求参数对应的属性,并生成setter和getter方法。
public class LoginAction {
//提供两个属性来封装HTTP请求参数
private String username;
private String password;
//生成的getter和setter方法
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;
}
//Action类默认处理用户请求的方法:execute方法
public String execute(){
return "SUCCESS";
}
}
在Action中不严格区分哪个属性是用于封装请求参数,哪个属性是用于封装处理结果。
Struts 2 中提供了一个Action接口和一个Action接口的实现类:ActionSupport。自己写Action时,可以实现Action接口或继承ActionSupport来重写execute方法。
Action访问Servlet API的方法:
1、Struts 2提供了一个ActionContext类,Action可以通过该类来访问Servlet API。
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class LoginAction implements Action {
//提供两个属性来封装HTTP请求参数
private String username;
private String password;
//生成的getter和setter方法
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;
}
//Action类默认处理用户请求的方法:execute方法
public String execute(){
ActionContext actionContext = ActionContext.getContext();
//通过ActionContext访问application范围的属性值
Integer counter = (Integer)actionContext.getApplication().get("counter");
if(counter == null){
counter = 1;
} else {
counter++;
}
//通过ActionContext设置application范围的属性值
actionContext.getApplication().put("counter", counter);
//通过ActionContext设置session范围的属性
actionContext.getSession().put("username", username);
if(getUsername().equals("admin") && getPassword().equals("123")){
//通过ActionContext设置request范围的属性
actionContext.put("tip", "登录成功");
return SUCCESS;
} else {
//通过ActionContext设置request范围的属性
actionContext.put("tip", "登录失败");
return ERROR;
}
}
}
2、Struts 2提供了ServletContextAware、ServletRequestAware、ServletResponseAware,Action可以通过实现接口来访问Servlet API。
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class LoginAction implements Action,ServletResponseAware{
//提供两个属性来封装HTTP请求参数
private String username;
private String password;
private HttpServletResponse response;
//生成的getter和setter方法
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;
}
//Action类默认处理用户请求的方法:execute方法
@Override
public String execute(){
ActionContext actionContext = ActionContext.getContext();
//通过ActionContext访问application范围的属性值
Integer counter = (Integer)actionContext.getApplication().get("counter");
if(counter == null){
counter = 1;
} else {
counter++;
}
//通过ActionContext设置application范围的属性值
actionContext.getApplication().put("counter", counter);
//通过ActionContext设置session范围的属性
actionContext.getSession().put("username", username);
if(getUsername().equals("admin") && getPassword().equals("123")){
//通过response添加cookie
Cookie cookie = new Cookie("username", getUsername());
cookie.setMaxAge(60 * 60);
response.addCookie(cookie);
//通过ActionContext设置request范围的属性
actionContext.put("tip", "登录成功");
return SUCCESS;
} else {
//通过ActionContext设置request范围的属性
actionContext.put("tip", "登录失败");
return ERROR;
}
}
}
import javax.servlet.http.Cookie;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class LoginAction implements Action{
//提供两个属性来封装HTTP请求参数
private String username;
private String password;
//生成的getter和setter方法
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;
}
//Action类默认处理用户请求的方法:execute方法
@Override
public String execute(){
ActionContext actionContext = ActionContext.getContext();
//通过ActionContext访问application范围的属性值
Integer counter = (Integer)actionContext.getApplication().get("counter");
if(counter == null){
counter = 1;
} else {
counter++;
}
//通过ActionContext设置application范围的属性值
actionContext.getApplication().put("counter", counter);
//通过ActionContext设置session范围的属性
actionContext.getSession().put("username", username);
if(getUsername().equals("admin") && getPassword().equals("123")){
//通过response添加cookie
Cookie cookie = new Cookie("username", getUsername());
cookie.setMaxAge(60 * 60);
ServletActionContext.getResponse().addCookie(cookie);
//通过ActionContext设置request范围的属性
actionContext.put("tip", "登录成功");
return SUCCESS;
} else {
//通过ActionContext设置request范围的属性
actionContext.put("tip", "登录失败");
return ERROR;
}
}
}