结果跳转方式
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="result" namespace="/" extends="struts-default">
<!--转发-->
<action name="Demo" class="com.company.jump.Demo" method="execute">
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
<!--重定向-->
<action name="Demo2" class="com.company.jump.Demo2" method="execute">
<result name="success" type="redirect">/hello.jsp</result>
</action>
<!--转发到Action-->
<action name="Demo3" class="com.company.jump.Demo3" method="execute">
<result name="success" type="chain">
<param name="actionName">Demo</param>
<param name="namespace">/</param>
</result>
</action>
<!--重定向Action-->
<action name="Demo4" class="com.company.jump.Demo4" method="execute">
<result name="success" type="redirectAction">
<param name="actionName">Demo</param>
<param name="namespace">/</param>
</result>
</action>
</package>
</struts>
获得servletAPI
ActionContext
//session域
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("name", "session");
//application域
Map<String, Object> application = ActionContext.getContext().getApplication();
application.put("name", "application");
//request域
//Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
ActionContext.getContext().put("name", "request");
ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest();
request.getSession();
ServletActionContext.getResponse();
ServletActionContext.getServletContext();
实现接口方式
package com.company.api;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class APIDemo3 extends ActionSupport implements ServletRequestAware, ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse httpServletResponse;
@Override
public String execute() throws Exception {
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest httpServletRequest) {
this.request = request;
}
@Override
public void setServletResponse(HttpServletResponse httpServletResponse) {
this.httpServletResponse = httpServletResponse;
}
}
参数封装
属性驱动
Parm
package com.company.param;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class Param extends ActionSupport {
//与参数键名称相同的属性
private String name;
private Date birthday;
private Integer age;
@Override
public String execute() throws Exception {
System.out.println("name:" + name);
System.out.println("birthday:" + birthday);
System.out.println("age:" + age);
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
struts.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="Param" namespace="/" extends="struts-default">
<action name="Param" class="com.company.param.Param" method="execute">
<result name="success" type="dispatcher">/form.jsp</result>
</action>
</package>
</struts>
form.jsp
<%--
Created by IntelliJ IDEA.
User: mac
Date: 2019/3/9
Time: 5:27 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Param.do">
<input type="text" name="name" ><br>
<input type="text" name="age" ><br>
<input type="text" name="birthday" ><br>
<input type="submit" name="提交">
</form>
</body>
</html>
对象驱动
Param2
package com.company.param;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class Param2 extends ActionSupport {
private User user;
@Override
public String execute() throws Exception {
System.out.printf(user.toString());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
struts.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="Param" namespace="/" extends="struts-default">
<action name="Param2" class="com.company.param.Param2" method="execute">
<result name="success" type="dispatcher">/form2.jsp</result>
</action>
</package>
</struts>
form2.jsp
<%--
Created by IntelliJ IDEA.
User: mac
Date: 2019/3/9
Time: 5:27 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Param2.do">
<input type="text" name="user.name" ><br>
<input type="text" name="user.age" ><br>
<input type="text" name="user.birthday" ><br>
<input type="submit" name="提交">
</form>
</body>
</html>
模型驱动
Param3
package com.company.param;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class Param3 extends ActionSupport implements ModelDriven<User> {
private User user = new User();
@Override
public String execute() throws Exception {
System.out.printf(user.toString());
return SUCCESS;
}
@Override
public User getModel() {
return user;
}
}
struts.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="Param" namespace="/" extends="struts-default">
<action name="Param3" class="com.company.param.Param3" method="execute">
<result name="success" type="dispatcher">/form3.jsp</result>
</action>
</package>
</struts>
form3.jsp
<%--
Created by IntelliJ IDEA.
User: mac
Date: 2019/3/9
Time: 5:27 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Param3.do">
<input type="text" name="name" ><br>
<input type="text" name="age" ><br>
<input type="text" name="birthday" ><br>
<input type="submit" name="提交">
</form>
</body>
</html>
集合类型封装
Param4
package com.company.param;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class Param4 extends ActionSupport {
private List list;
private Map<String, String> map;
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Override
public String execute() throws Exception {
System.out.println("list:" + list);
System.out.println("map:" + map);
return SUCCESS;
}
}
struts.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="Param" namespace="/" extends="struts-default">
<action name="Param4" class="com.company.param.Param4" method="execute">
<result name="success" type="dispatcher">/form4.jsp</result>
</action>
</package>
</struts>
form4.jsp
<%--
Created by IntelliJ IDEA.
User: mac
Date: 2019/3/9
Time: 5:27 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Param4.do" method="post">
list:<input type="text" name="list" ><br>
list:<input type="text" name="list[3]" ><br>
list:<input type="text" name="list[8]" ><br>
map:<input type="text" name="map['name']" ><br>
<input type="submit" name="提交">
</form>
</body>
</html>