2.1 结果跳转方式
2.1.1 转发
Demo1Action.java
import com.opensymphony.xwork2.ActionSupport;
public class Demo1Action extends ActionSupport {
public String execute() throws Exception {
return SUCCESS;
}
}
struts.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="result" namespace="/" extends="struts-default">
<action name="Demo1Action" class="com.yyh.struts2.a_result.Demo1Action" method="execute">
<!-- type默认值是dispatcher 转发 -->
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
</package>
</struts>
访问结果:
2.1.2 重定向
Demo2Action.java
import com.opensymphony.xwork2.ActionSupport;
public class Demo2Action extends ActionSupport {
public String execute() throws Exception {
return SUCCESS;
}
}
struts.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="result" namespace="/" extends="struts-default">
<action name="Demo2Action" class="com.yyh.struts2.a_result.Demo2Action" method="execute">
<!-- type:redirect 重定向到hello.jsp -->
<result name="success" type="redirect">/hello.jsp</result>
</action>
</package>
</struts>
访问结果:
2.1.3 转发到Action
Demo3Action.java
import com.opensymphony.xwork2.ActionSupport;
public class Demo3Action extends ActionSupport {
public String execute() throws Exception {
System.out.println("Demo3Action");
return SUCCESS;
}
}
struts.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="result" namespace="/" extends="struts-default">
<action name="Demo1Action" class="com.yyh.struts2.a_result.Demo1Action" method="execute">
<!-- type默认值dispatcher 转发到hello.jsp -->
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
<action name="Demo2Action" class="com.yyh.struts2.a_result.Demo2Action" method="execute">
<!-- type:redirect 重定向到hello.jsp -->
<result name="success" type="redirect">/hello.jsp</result>
</action>
<action name="Demo3Action" class="com.yyh.struts2.a_result.Demo3Action" method="execute">
<!-- 转发到Action -->
<result name="success" type="chain">
<param name="actionName">Demo1Action</param>
<param name="namespace">/</param>
</result>
</action>
</package>
</struts>
访问结果:
2.1.4 重定向到Action
Demo4Action.java
import com.opensymphony.xwork2.ActionSupport;
public class Demo4Action extends ActionSupport {
public String execute() throws Exception {
System.out.println("Demo4Action");
return SUCCESS;
}
}
struts.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="result" namespace="/" extends="struts-default">
<action name="Demo1Action" class="com.yyh.struts2.a_result.Demo1Action" method="execute">
<!-- type默认值dispatcher 转发到hello.jsp -->
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
<action name="Demo2Action" class="com.yyh.struts2.a_result.Demo2Action" method="execute">
<!-- type:redirect 重定向到hello.jsp -->
<result name="success" type="redirect">/hello.jsp</result>
</action>
<action name="Demo3Action" class="com.yyh.struts2.a_result.Demo3Action" method="execute">
<!-- 转发到Action -->
<result name="success" type="chain">
<param name="actionName">Demo1Action</param>
<param name="namespace">/</param>
</result>
</action>
<action name="Demo4Action" class="com.yyh.struts2.a_result.Demo4Action">
<!-- 重定向到Action -->
<result name="success" type="redirectAction">
<param name="actionName">Demo1Action</param>
<param name="namespace">/</param>
</result>
</action>
</package>
</struts>
访问结果:
2.2 访问ServletAPI的方式
2.2.1 ActionContext(常用)
生命周期:每个请求是都会创建一个与请求对应的ActionContext.请求处理完ActionContext销毁
如何获得ActoinContext?
Struts2设计的是,ActionContext对象创建之后,将ActionContext与当前线程绑定,我们要获得ActionContext,只需要从ThreadLocal中获得。
// 如何在action中获得原生ServletAPI
public class Demo5Action extends ActionSupport {
public String execute() throws Exception {
// 获得Sesson域对象 -> Map
Map<String, Object> sessionScope = ActionContext.getContext().getSession();
sessionScope.put("session", "session");
// 获得Application域
Map<String, Object> applicationScope = ActionContext.getContext().getApplication();
applicationScope.put("application", "application");
// 获得request域(Struts2并不推荐使用原生request域)
Map<String, Object> requestScope = (Map<String, Object>)ActionContext.getContext().get("request");
ActionContext.getContext().put("request", "requestTom");
return SUCCESS;
}
}
request: ${requestScope.request }<br>
session: ${sessionScope.session }<br>
application: ${applicationScope.application }<br>
2.2.2 通过ServletActionContext(其本质还是ActionContex)
// 并不推荐
public String execute() throws Exception {
// 原生request
HttpServletRequest request = ServletActionContext.getRequest();
// 原生session
HttpSession session = request.getSession();
// 原生response
HttpServletResponse response = ServletActionContext.getResponse();
// 原生servletContext
ServletContext servletContext = ServletActionContext.getServletContext();
return SUCCESS;
}
2.2.3 实现接口方式(其本质也是ActionContext)
public class Demo7Action extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
public String execute() throws Exception {
System.out.println("原生request:" + request);
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
}
}
2.3 如何获得参数
2.3.1 Action生命周期
1. 每次请求到来时,都会创建一个新的Action实例
2. Action是线程安全的,可以使用成员变量来接受参数
2.3.2 属性驱动获得参数
public class Demo8Action extends ActionSupport {
// 准备与参数相同的属性就可以
private String name;
// 可以自动完成类型转换,只能转换8大基本数据类型以及对应包装类
private Integer age;
// 支持特定类型字符串转换为Date 例如yyyy-MM-dd
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() throws Exception {
System.out.println("name参数值:" + name + ",age参数值" + age + ",生日" + birthday);
return SUCCESS;
}
}
<form action="${pageContext.request.contextPath }/Demo8Action">
用户名:<input type="text" name="name" /> <br>
年龄:<input type="text" name="age" /> <br>
生日: <input type="text" name="birthday" /> <br>
<input type="submit" value="提交" />
</form>
2.3.3 对象驱动获得参数 (封装到多个对象)
public class Demo10Action extends ActionSupport {
// 准备user对象
private User user;
// 必须提供对象的get方法
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
}
<form action="${pageContext.request.contextPath }/Demo10Action">
用户名: <input type="text" name="user.name" /> <br/>
年龄: <input type="text" name="user.age" /> <br/>
生日: <input type="text" name="user.birthday" ><br/>
<input type="submit" value="提交" />
</form>
2.3.4 模型驱动 (封装单个对象优先使用)
通过实现ModelDriven接口来接受请求参数,重写getModel()方法,返回Action使用的数据模型对象
public class Demo11Action extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
@Override
public User getModel() {
return user;
}
}
<form action="${pageContext.request.contextPath }/Demo11Action">
用户名:<input type="text" name="name" /> <br>
年龄:<input type="text" name="age" /> <br>
生日: <input type="text" name="birthday" /> <br>
<input type="submit" value="提交" />
</form>
2.4 集合类型参数封装
2.4.1 封装数据到list集合
<form action="${pageContext.request.contextPath }/Demo12Action">
用户名:<input type="text" name="list[0].name" /> <br>
年龄:<input type="text" name="list[0].age" /> <br>
生日: <input type="text" name="list[0]birthday" /> <br>
用户名:<input type="text" name="list[1].name" /> <br>
年龄:<input type="text" name="list[1].age" /> <br>
生日: <input type="text" name="list[1].birthday" /> <br>
<input type="submit" value="提交" />
</form>
public class Demo12Action extends ActionSupport {
private List<User> list;
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
public String execute() throws Exception {
for (User user : list) {
System.out.println(user);
}
return SUCCESS;
}
}
2.4.2 封装数据到 Map集合
<form action="${pageContext.request.contextPath }/Demo13Action" >
map1:<input type="text" name="map['1']"/><br>
map2: <input type="text" name="map[2]" /><br>
<input type="submit" value="提交" />
</form>
public class Demo13Action extends ActionSupport {
private Map<String, String> map;
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public String execute() throws Exception {
for (String key : map.keySet()) {
System.out.println(map.get(key));
}
return SUCCESS;
}
}