Struts2基礎學習2
阿新 • • 發佈:2018-11-20
Struts2基礎學習2
專案結構,測試頁面與實體類
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> request: ${requestScope.name}api<br> session:${sessionScope.name}<br> application:${applicationScope.name}<br> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/Demo3Action"> 使用者名稱:form1<input type="text" name="name" /><br> 年齡:<input type="text" name="age" /><br> 生日:<input type="text" name="birthday" /><br> <input type="submit" value="提交" /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncodingform2="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/Demo4Action"> 使用者名稱:<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> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/Demo5Action"> 使用者名稱:<input type="text" name="name" /><br> 年齡:<input type="text" name="age" /><br> 生日:<input type="text" name="birthday" /><br> <input type="submit" value="提交" /> </form> </body> </html>form3
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/Demo6Action" method="post" > list:<input type="text" name="list" /><br> list:<input type="text" name="list[3]" /><br> map:<input type="text" name="map['key']" /><br> <input type="submit" value="提交" /> </form> </body> </html>form4
package com.struts2.pojo; import java.util.Date; /** * @author: 肖德子裕 * @date: 2018/11/20 18:39 * @description: */ public class User { private String name; private Integer age; private Date birthday; 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; } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", birthday=" + birthday + "]"; } }User
測試Struts2的重定向與轉發
package com.struts2.action; import com.opensymphony.xwork2.ActionSupport; /** * @author: 肖德子裕 * @date: 2018/11/20 10:25 * @description: 測試重定向與轉發 * 之所以繼承ActionSupport類,是因為該類實現了很多介面,我們可以直接使用 */ public class Demo1Action extends ActionSupport { public String index() throws Exception{ System.out.println("hello1"); return SUCCESS; } public String index2() throws Exception{ System.out.println("hello2"); return SUCCESS; } public String index3() throws Exception{ System.out.println("hello3"); return SUCCESS; } public String index4() throws Exception{ System.out.println("hello4"); return SUCCESS; } }Demo1Action
測試Struts2獲取servlet api
package com.struts2.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import java.util.Map; /** * @author: 肖德子裕 * @date: 2018/11/20 14:05 * @description: 測試struts2獲取原生servlet api * 每次請求都會建立一個ActionContext物件(資料中心),請求結束時銷燬 * 可以通過該物件獲取servlet api * 也可以通過ServletActionContext物件獲取api(不推薦) * 也可以通過實現相應的介面獲取(不推薦) * 3種方式都是從ActionContext中獲取相應api */ public class Demo2Action extends ActionSupport { /** * 在本地執行緒ThreadLocal上綁定了建立的ActionContext物件 */ public String test() throws Exception{ //獲取request(不推薦使用) Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request"); //可以直接儲存值到request(推薦) ActionContext.getContext().put("name","request"); //獲取session Map<String, Object> sessionScope = ActionContext.getContext().getSession(); sessionScope.put("name","session"); //獲取application Map<String, Object> applicationScope = ActionContext.getContext().getApplication(); applicationScope.put("name","application"); return SUCCESS; } }Demo2Action
測試Struts2獲取引數的方式
package com.struts2.action; import com.opensymphony.xwork2.ActionSupport; import java.util.Date; /** * @author: 肖德子裕 * @date: 2018/11/20 14:39 * @description: 測試struts2屬性獲取引數 * 每次請求都會建立一個新的action例項物件 * servlet是執行緒不安全的;action是執行緒安全的,可以使用成員變數接收引數 * 引數支援自動轉換,如字串轉日期,字串轉int */ public class Demo3Action extends ActionSupport { private String name; private Integer age; private Date birthday; 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; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getParam(){ System.out.println(name+" "+age+" "+birthday); return SUCCESS; } }Demo3Action
package com.struts2.action; import com.opensymphony.xwork2.ActionSupport; import com.struts2.pojo.User; /** * @author: 肖德子裕 * @date: 2018/11/20 18:40 * @description: 測試struts2物件獲取引數 */ public class Demo4Action extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getParam(){ System.out.println(user); return SUCCESS; } }Demo4Action
package com.struts2.action; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.struts2.pojo.User; /** * @author: 肖德子裕 * @date: 2018/11/20 18:47 * @description: 測試struts2模型驅動獲取引數 */ public class Demo5Action extends ActionSupport implements ModelDriven<User> { private User user=new User(); public String getParam(){ System.out.println(user); return SUCCESS; } @Override public User getModel() { return user; } }Demo5Action
package com.struts2.action; import com.opensymphony.xwork2.ActionSupport; import java.util.List; import java.util.Map; /** * @author: 肖德子裕 * @date: 2018/11/20 19:05 * @description: 封裝集合型別引數 */ public class Demo6Action extends ActionSupport { private List<String> list; private Map<String,String> map; public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public String getParams(){ System.out.println(list); System.out.println(map); return SUCCESS; } }Demo6Action
核心配置檔案(注意web.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="demo1" namespace="/" extends="struts-default" > <!-- 測試轉發(預設) --> <action name="Demo1Action" class="com.struts2.action.Demo1Action" method="index" > <result name="success" type="dispatcher" >/index.jsp</result> </action> <!-- 測試重定向 --> <action name="Demo1Action2" class="com.struts2.action.Demo1Action" method="index2" > <result name="success" type="redirect" >/index.jsp</result> </action> <!-- 測試鏈式轉發跳轉 --> <action name="Demo1Action3" class="com.struts2.action.Demo1Action" method="index3" > <result name="success" type="chain" > <param name="namspace">/</param> <param name="actionName">Demo1Action</param> </result> </action> <!-- 測試鏈式重定向 --> <action name="Demo1Action4" class="com.struts2.action.Demo1Action" method="index4" > <result name="success" type="redirectAction" > <param name="namspace">/</param> <param name="actionName">Demo1Action</param> </result> </action> </package> <package name="demo2" namespace="/" extends="struts-default" > <!-- 測試獲取servlet api --> <action name="Demo2Action" class="com.struts2.action.Demo2Action" method="test" > <result name="success" type="dispatcher" >/api.jsp</result> </action> </package> <package name="demo3" namespace="/" extends="struts-default" > <!-- 測試屬性獲取引數 --> <action name="Demo3Action" class="com.struts2.action.Demo3Action" method="getParam" > <result name="success" type="dispatcher" >/form1.jsp</result> </action> </package> <package name="demo4" namespace="/" extends="struts-default" > <!-- 測試物件獲取引數 --> <action name="Demo4Action" class="com.struts2.action.Demo4Action" method="getParam" > <result name="success" type="dispatcher" >/form2.jsp</result> </action> </package> <package name="demo5" namespace="/" extends="struts-default" > <!-- 測試模型驅動獲取引數 --> <action name="Demo5Action" class="com.struts2.action.Demo5Action" method="getParam" > <result name="success" type="dispatcher" >/form3.jsp</result> </action> </package> <package name="demo6" namespace="/" extends="struts-default" > <!-- 測試集合獲取引數 --> <action name="Demo6Action" class="com.struts2.action.Demo6Action" method="getParams" > <result name="success" type="dispatcher" >/form4.jsp</result> </action> </package> </struts>struts2.xml