struts2框架快速入門小案例


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框架可以運行。 

<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類

//要求,在HelloAction類中創建一個返回值是String類型的方法,注意,無參數。
public String say(){
  return "good";
}

  5.在struts.xml文件中配置HelloAction

<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.

運行流程:

 用filter模擬Struts2工作原理:

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);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM