ActionType.javaphp
package com; public class ActionType_1 { public String execute() { System.out.println("我是Action類型1"); return null; } }
C、繼承ActionSupport類html
package com; import com.opensymphony.xwork2.ActionSupport; public class ActionType_3 extends ActionSupport { @Override public String execute() throws Exception { System.out.println("這是繼承ActionSupport的類"); return super.execute(); } }
示例:java
ActionMethod_1.java:web
package method; import com.opensymphony.xwork2.ActionSupport; public class ActionMethod_1 extends ActionSupport { @Override public String execute() throws Exception { System.out.println("執行默認execute方法"); return super.execute(); } public String add() { System.out.println("執行add方法"); return NONE; } public String del() { System.out.println("執行del方法"); return NONE; } public String list() { System.out.println("執行list方法"); return NONE; } public String uptdate() { System.out.println("執行uptdate方法"); return NONE; } }
假設Actionmethod_1這個Action中有4個方法(除execute外),若是咱們要用method屬性調用這些方法,則須要:apache
struts.xml設計模式
<package name="default" namespace="/" extends="struts-default"> <action name="method_1" class="method.ActionMethod_1" method="add"> </action> <action name="method_2" class="method.ActionMethod_1" method="del"> </action> <action name="method_3" class="method.ActionMethod_1" method="uptdate"> </action> <action name="method_4" class="method.ActionMethod_1" method="list"> </action> </package>
如配置,當地址欄URL=「工程名/method_1」時,執行method.ActionMethod_1類中的add方法; 當地址欄URL=「工程名/method_2」時,執行method.ActionMethod_1類中的adel方法,以此類推。session
ActionMethod_1.java:
package method;
import com.opensymphony.xwork2.ActionSupport;
public class ActionMethod_1 extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("執行默認execute方法");
return super.execute();
}
public String add()
{
System.out.println("執行add方法2");
return NONE;
}
public String del()
{
System.out.println("執行del方法2");
return NONE;
}
public String list()
{
System.out.println("執行list方法2");
return NONE;
}
public String uptdate()
{
System.out.println("執行uptdate方法2");
return NONE;
}
}
struts.xmlapp
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="method" class="method.ActionMethod_1"> </action> </package> </struts>
index.jspeclipse
<%@ 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> <% String path=request.getContextPath(); %> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="<%=path%>/method!add">執行add方法</a> <a href="<%=path%>/method!del">執行del方法</a> <a href="<%=path%>/method!uptdate">執行uptdate方法</a> <a href="<%=path%>/method!list">執行list方法</a> </body> </html>
解析: 當點擊a標籤時候,請求url中的!後跟的是方法名,<a href="<%=path%>/method!add">執行add方法</a> 即當點擊這個標籤時,執行action名爲「method」中的add方法。其餘的依次類推。 這種方法就避免了在struts.xml文件中配置過多,致使代碼臃腫。 webapp
注意的是:使用動態方法調用,struts.enable.DynamicMethodInvocation 這個常量必須是true ;
ActionMethod_1.java
package method; import com.opensymphony.xwork2.ActionSupport; public class ActionMethod_1 extends ActionSupport { @Override public String execute() throws Exception { System.out.println("執行默認execute方法"); return super.execute(); } public String add() { System.out.println("執行add方法3"); return NONE; } public String del() { System.out.println("執行del方法3"); return NONE; } public String list() { System.out.println("執行list方法3"); return NONE; } public String uptdate() { System.out.println("執行uptdate方法3"); return NONE; } }
idnex.jsp
<%@ 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> <% String path=request.getContextPath(); %> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="<%=path%>/method_add">執行add方法</a> <a href="<%=path%>/method_del">執行del方法</a> <a href="<%=path%>/method_uptdate">執行uptdate方法</a> <a href="<%=path%>/method_list">執行list方法</a> </body> </html>
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="method_*" class="method.ActionMethod_1" method="{1}"> </action> </package> </struts>
「method_*」的Action,* 號表示任意字符(任意長度),即調用method.ActionMethod_1這個類,並調用method=「{1}」 這個方法,{1} 中的表示*表示的字符,在這個標籤中*表明add,因此就調用了ActionMethod_1類中的add方法。
注意: 若是有多個通配符,
<action name="method_*_*" class="method.ActionMethod_1" method="{1}"> 而標籤爲<a href="<%=path%>/method_exam_add">執行add方法</a>,則調用的是第一個通配符匹配的字符即exam方法。
A、 實現相應的接口,注入servlet對象
示例:
index.jsp
<%@ 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> <% String path=request.getContextPath(); %> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="<%=path%>/servlet/obj">獲取並測試servlet原生對象</a> </body> </html>
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/servlet" extends="struts-default"> <action name="obj" class="com.ServletObject"></action> </package> </struts>
ServletObject.java
package com; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class ServletObject extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{ //定義servlet原生對象爲成員變量,不然若是是局部變量生命週期過短。 private ServletContext servletContext; private HttpServletResponse response; private HttpServletRequest request; private PrintWriter out; private HttpSession session; /** * 設置ServletContext */ public void setServletContext(ServletContext servletContext) { this.servletContext=servletContext; } /** * 設置ServletResponse,獲得response以後就可由response獲得PrintWriter對象 */ public void setServletResponse(HttpServletResponse response) { this.response=response; if(response!=null) { try { this.out=response.getWriter(); } catch (IOException e) { e.printStackTrace(); } } } /** * 設置ServletRequest,獲得request以後就可由request獲得HttpSession */ public void setServletRequest(HttpServletRequest request) { this.request=request; if(request!=null) { this.session=request.getSession(); } } public String execute() { /** * 測試servlet原生對線是否真正獲取可使用 */ System.out.println(this.request.getContextPath()); //獲取工程路徑 System.out.println(this.session.getId()); //獲取session的id System.out.println(this.servletContext.getRealPath("/index.jsp")); //獲取/index.jsp的真實路徑 this.out.print("我是PrintWriter類的對象out"); return NONE; } }
B、 使用struts提供的靜態類來獲取
package com; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class ServletObject extends ActionSupport { public String execute() { try{ HttpServletRequest request=ServletActionContext.getRequest(); HttpServletResponse response=ServletActionContext.getResponse(); ServletContext servletContext=ServletActionContext.getServletContext(); PrintWriter out=response.getWriter(); HttpSession session=request.getSession(); /** * 測試servlet原生對線是否真正獲取可使用 */ System.out.println(request.getContextPath()); //獲取工程路徑 System.out.println(session.getId()); //獲取session的id System.out.println(servletContext.getRealPath("/index.jsp")); //獲取/index.jsp的真實路徑 out.print("我是PrintWriter類的對象out"); } catch(Exception e) { e.printStackTrace(); } return NONE; } }
ServletActionContext 這個靜態類提供了Servlet原生對象的建立等。
通常來講用第一種方法來獲取,可是這種方法每一個Action都要寫一大堆代碼很麻煩,因此這裏能夠把這些servlet原生對象封裝爲一個抽象基類,其餘Action要使用servlet原生對象的話就繼承這個抽象類並實現抽象execute方法便可。
示例:
BaseAction.java
package com; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.util.ServletContextAware; public abstract class BaseAction implements ServletRequestAware,ServletResponseAware,ServletContextAware { HttpServletRequest request; HttpServletResponse response; ServletContext servletContext; HttpSession session; PrintWriter out; public void setServletContext(ServletContext servletContext) { this.servletContext=servletContext; } public void setServletResponse(HttpServletResponse response) { this.response=response; if(response!=null) { try { this.out=response.getWriter(); } catch (IOException e) { e.printStackTrace(); } } } public void setServletRequest(HttpServletRequest request) { this.request=request; if(request!=null) { this.session=request.getSession(); } } abstract public String execute(); }
get把數據放在網址中,例如:http://www.abc.com/index.php?a=1&b=2 其中?a=1&b=2就是get數據,而且連http://www.abc.com/index.php長度限制在1024個字。
示例:
index.jsp:
<%@ 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> <% String path=request.getContextPath(); %> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="<%=path%>/request/getpost!get?userId=001&userName=用戶名">用request原生對象來獲取GET請求中的參數</a> </body> </html>
把參數userId=001和userName=用戶名做爲參數傳過去。
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> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="request" namespace="/request" extends="struts-default" > <action name="getpost" class="com.ActionRequest"></action> </package> </struts>
ActionRequest.java
package com; import java.io.UnsupportedEncodingException; public class ActionRequest extends BaseAction{ @Override public String execute() { return null; } public String get() throws UnsupportedEncodingException { /** * 模擬GET請求 */ String userId=request.getParameter("userId"); String userName=request.getParameter("userName"); userName=new String(userName.getBytes("ISO-8859-1"),"UTF-8"); System.out.println("userId="+userId); System.out.println("userName="+userName); return null; } public String post() { /** * 模擬POST請求 */ //TODO return null; } }
post則是把數據放到http請求中,例如仍是傳輸a=1&b=2,但是網址仍是http://www.abc.com/index.php,好比表單的post提交,在網址上咱們是看不到用戶輸入的數據的。
index.jsp
<%@ 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="request/shuxing!post_2" method="post" > 用戶名:<input type="text" name="userName" /> <br/> 密 碼:<input type="password" name="passWd"/><br/> 愛 好:<input type="checkbox" name="like" value="籃球"/>籃球 <input type="checkbox" name="like" value="足球"/>足球 <input type="submit" /> </form> </body> </html>
界面:
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/request" extends="struts-default"> <action name="shuxing" class="action.AttrrbuteDriver"></action> </package> </struts>
package action; import java.util.List; public class AttrrbuteDriver extends BaseAction{ /** * 以屬性驅動的方式來獲取表單中的值 * 前提是: 1. 控件名(即表單元素名)與Action中的成員變量名要一致 * 2. Action中的成員變量要有getset方法 */ private String userName; //獲取表單元素名爲userName的值, private String passWd; //獲取表單元素名爲passWd的值 private List<String> like; //也能夠用String[]來獲取表單元素名爲checkbox的值 public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWd() { return passWd; } public void setPassWd(String passWd) { this.passWd = passWd; } public List<String> getLike() { return like; } public void setLike(List<String> like) { this.like = like; } @Override public String execute() { System.out.println("execute方法"); return null; } public String post_2(){ System.out.println(userName); System.out.println(passWd); for(String likes:like){ System.out.println(likes); } return null; } }
若是一個表單元素有不少的時候,那麼Action中定義的成員變量就會不少,代碼會冗長,因此咱們能夠把其中一些成員變量封裝到JavaBean裏,具體以下:
index.jsp
<%@ 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> <h3>以屬性驅動來獲取表單值</h3> <form action="request/shuxing!post_2" method="post" > 用戶名:<input type="text" name="userName" /> <br/> 密 碼:<input type="password" name="passWd"/><br/> 愛 好:<input type="checkbox" name="like" value="籃球"/>籃球 <input type="checkbox" name="like" value="足球"/>足球 <h3>註冊狗的信息</h3> dogId:<input type="text" name="dog.dogId" /> <br/> dogName:<input type="text" name="dog.dogName" /> <br/> dogColor:<input type="text" name="dog.dogColor" /> <br/> <input type="submit" /> </form> </body> </html>
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/request" extends="struts-default"> <action name="shuxing" class="action.AttrrbuteDriver"></action> </package> </struts>
AttrrbuteDriver.java
package action; import java.util.List; import bean.DogBean; public class AttrrbuteDriver extends BaseAction{ /** * 以屬性驅動的方式來獲取表單中的值 * 前提是: 1. 控件名(即表單元素名)與Action中的成員變量名要一致 * 2. Action中的成員變量要有getset方法 */ private String userName; //獲取表單元素名爲userName的值, private String passWd; //獲取表單元素名爲passWd的值 private List<String> like; //也能夠用String[]來獲取表單元素名爲checkbox的值 private DogBean dog; //DogBean是JavaBean public DogBean getDog() { return dog; } public void setDog(DogBean dog) { this.dog = dog; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWd() { return passWd; } public void setPassWd(String passWd) { this.passWd = passWd; } public List<String> getLike() { return like; } public void setLike(List<String> like) { this.like = like; } @Override public String execute() { System.out.println("execute方法"); return null; } public String post_2(){ System.out.println(userName); System.out.println(passWd); for(String likes:like){ System.out.println(likes); } System.out.println(dog.getDogName()); System.out.println(dog.getDogId()); System.out.println(dog.getDogColor()); return null; } }
package bean; public class DogBean { private String dogId; private String dogName; private String dogColor; public String getDogId() { return dogId; } public void setDogId(String dogId) { this.dogId = dogId; } public String getDogName() { return dogName; } public void setDogName(String dogName) { this.dogName = dogName; } public String getDogColor() { return dogColor; } public void setDogColor(String dogColor) { this.dogColor = dogColor; } }
結果:
解析: dogId:<input type="text" name="dog.dogId" /> 中的「dog.dogId」中的dog爲Action類(本例中爲AttrrbuteDriver類)的成員變量名即private DogBean dog; 並且dog成員變量也要有getset方法。「dog.dogId」中的dogId爲JavaBean(本例中爲DogBean)的成員變量dogId.
index.jsp
<%@ 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> <h3>以模型驅動來獲取表單值</h3> <form action="request/shuxing!post_2" method="post" > 用戶名:<input type="text" name="userName" /> <br/> 密 碼:<input type="password" name="passWd"/><br/> 愛 好:<input type="checkbox" name="like" value="籃球"/>籃球 <input type="checkbox" name="like" value="足球"/>足球 <h3>註冊狗的信息</h3> dogId:<input type="text" name="dogId" /> <br/> dogName:<input type="text" name="dogName" /> <br/> dogColor:<input type="text" name="dogColor" /> <br/> <input type="submit" /> </form> </body> </html>
界面:
struts.xml和DogBean.java與前一個例子同樣,
package action; import java.util.List; import com.opensymphony.xwork2.ModelDriven; import bean.DogBean; public class AttrrbuteDriver extends BaseAction implements ModelDriven<DogBean>{ /** * 模型驅動方法來獲取表單元素值 * 1. 實現ModelDriven<類型>,並重寫其中的getModel()方法 * 2. 本例中AttrrbuteDriver接受Dog這個模型的數據,且實現一個接口,即這個類只能接受一個數據模型, * 若是再加implements ModelDriven<DogBean>,ModelDriven<UserBean> * 這是錯的,雖然日常來講語法是正確的。 */ private String userName; //獲取表單元素名爲userName的值, private String passWd; //獲取表單元素名爲passWd的值 private List<String> like; //也能夠用String[]來獲取表單元素名爲checkbooks的值 private DogBean dog; public DogBean getModel() { this.dog=new DogBean(); return dog; } public DogBean getDog() { return dog; } public void setDog(DogBean dog) { this.dog = dog; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWd() { return passWd; } public void setPassWd(String passWd) { this.passWd = passWd; } public List<String> getLike() { return like; } public void setLike(List<String> like) { this.like = like; } @Override public String execute() { System.out.println("execute方法"); return null; } public String post_2(){ System.out.println(userName); System.out.println(passWd); for(String likes:like){ System.out.println(likes); } System.out.println(dog.getDogName()); System.out.println(dog.getDogId()); System.out.println(dog.getDogColor()); return null; } }
結果: