struts2快速入門:
index.jsp------>HelloAction--------->hello.jsp struts2流程
1.導入jar包
struts2的目錄結構:
apps: 例子程序
docs:文檔
lib:struts2框架所應用的jar以及插件包
src:源代碼
core 它是struts2的源代碼
xwork-core struts2底層使用了xwork,xwork的源代碼
注意:在struts2開發,通常狀況下最少導入的jar包,去apps下的struts2-blank示例程序中copy
2.建立index.jsp頁面前端
3.對struts2框架進行配置
1.web.xml文件中配置前端控制器(核心控制器)-----就是一個Filter(目的:是爲了讓struts2框架能夠運行。) web
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.在src下(對應Tomcat的classes下)建立一個struts.xml配置文件 ,這個是struts2框架配置文件(目的:是爲了struts2框架流程能夠執行。)
4.建立一個HelloAction類apache
//要求,在HelloAction類中建立一個返回值是String類型的方法,注意,無參數。 public String say(){ return "good"; }
5.在struts.xml文件中配置HelloAction
app
<package name="default" namespace="/" extends="struts-default"> <action name="hello" class="cn.yzu.action.HelloAction" method="say"> <result name="good">/hello.jsp</result> </action> </package>
6.在index.jsp中添加鏈接,測試框架
<a href="${pageContext.request.contextPath}/hello">第一次使用struts2</a>
在地址欄中輸入:http://localhost/struts2_day01/index.jsp 訪問鏈接,就能夠看到HelloAction類中的say方法執行了,也跳轉到了hello.jsp.jsp
運行流程:測試
用filter模擬Struts2工做原理:this
public class StrutsFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException {} public void destroy() {} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { // 1.強轉 HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; // 2.操做 // 2.1 獲得請求資源路徑 String uri = request.getRequestURI(); String contextPath = request.getContextPath(); String path = uri.substring(contextPath.length() + 1); // 2.2 使用path去struts.xml文件中查找某一個<action name=path>這個標籤 SAXReader reader = new SAXReader(); try { // 獲得struts.xml文件的document對象。 Document document = reader.read(new File(this.getClass().getResource("/struts.xml").getPath())); // 查找<action name='hello'>這樣的標籤 Element actionElement = (Element) document.selectSingleNode("//action[@name='" + path + "']"); if (actionElement != null) { // 獲得<action>標籤上的class屬性以及method屬性 String className = actionElement.attributeValue("class"); // 獲得了action類的名稱 String methodName = actionElement.attributeValue("method");// 獲得action類中的方法名稱。 // 2.3經過反射,獲得Class對象,獲得Method對象 Class actionClass = Class.forName(className); Method method = actionClass.getDeclaredMethod(methodName); // 2.4 讓method執行. String returnValue = (String) method.invoke(actionClass.newInstance()); // 是讓action類中的方法執行,並獲取方法的返回值。 // 2.5 使用returnValue去action下查找其子元素result的name屬性值,與returnValue作對比。 Element resultElement = actionElement.element("result"); String nameValue = resultElement.attributeValue("name"); if (returnValue.equals(nameValue)) { // 2.6獲得了要跳轉的路徑。 String skipPath = resultElement.getText(); request.getRequestDispatcher(skipPath).forward(request,response); return; } } } catch (Exception e) { e.printStackTrace(); } // 3.放行 chain.doFilter(request, response); } }