<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 下載 [http://struts.apache.org/downloads.html](http://struts.apache.org/downloads.html) 可能需要VPN。 我們下載2.3.31版本。 ![](https://box.kancloud.cn/afea5ba14fecc6a0017a0203742acc23_374x272.png) 同時,我們將下載后壓縮包放在了tower中的『夢云智』項目中,這意味著你可以直接登陸tower來下載教程使用的struts。 ## 引入 在JAVA開發的世界中,我們可以使用很多的第三方包來擴充我們的應用。這就像是在搭積木。我們想要什么的時候,直接去下載個積木包(一個包中包括了很多種積木),然后使用從這個積木包中找到一些我們需要的積木,引入進來就可以了。 struts下載后,默認給我們了所有的積木,但按項目需求的不同,我們可能只需要部分積木就能達到我們學習的目的。當然了,你也可以把所有的積木都拿過來使用,這僅僅只會增加項目的占用空間而已。在此,我們抱著學習的目的,只引struts中最核心的包(積木)。 > 查看struts的基本jar包,請參考:[http://www.imooc.com/video/9022](http://www.imooc.com/video/9022) 在這,我們已經為大家分解好,并放在團隊的tower中 ![](https://box.kancloud.cn/783a60463a422960e1aaf38eba19ee76_364x195.png) 我們下載解壓,并將其放置到如下位置: ![](https://box.kancloud.cn/ea96cc9cb599ec23a7969c5af6ba3907_512x792.png) ### 使用Struts JAR包 將在eclipse中,通過Java Build Path中的Libraries進行第三方庫(jar包)的引入 在項目文件夾上點右鍵 -&gt; Build path -&gt; configure Build Path -&gt; Libraries -&gt; add JARS 然后選擇上面lib文件夾中的所有jar文件,最后點確定。此時struts已經被我們成功引用到項目中,并可以在項目中使用他們了。我們此時打開Java Resources -&gt; Libraries 能夠查看到我們剛剛引入的包,視為正常。 ![](https://box.kancloud.cn/71f2a0029b99902d10cbe6f1e007f580_526x430.png) # 配置web.xml 下面,我們共同將struts配置至項目中: ``` <display-name>Java EE study</display-name> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> ``` 我們新配置了兩項信息: * 定義了一個過濾器,名字叫做:struts2, 它位于:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter * 指定了過濾規則(路由)。 * 在此規則下的所有請求,請交給指定的過濾器來處理。 * 即:所有的請求都會交給我們在上面定義好的struts2過濾器(類). ## 測試: 重啟tomcat,控制臺無報錯.即成功加了struts。 如果未成功引入的話,將報找不到 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter的錯誤。此時請檢查: * 是否將struts基本jar包復制到了項目的指定位置。 * tomcat在啟動時,將復制這些jar包。 * 是否在Libraries中,由上面的位置添加了這些jar包。 * 添加后,我們將在左側的 src中的Libraries中找到它們。 ![https://box.kancloud.cn/3454d3b2d9c29bc5e699a22fd6cc7263_536x356.png](https://box.kancloud.cn/3454d3b2d9c29bc5e699a22fd6cc7263_536x356.png) 在其包上點右鍵,點擊屬性,文件的位置應該如下: ![https://box.kancloud.cn/40fcdf062f1df4f56cd61835124cd641_1114x264.png](https://box.kancloud.cn/40fcdf062f1df4f56cd61835124cd641_1114x264.png) 加入注釋后如下: ``` <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>java ee study!</display-name> <!-- 指定過濾器 --> <filter> <!-- 定義過濾器的名字:struts2 --> <filter-name>struts2</filter-name> <!-- 過濾器對應的類 該類位于Java Resources -> Libraries -> struts2-core-2.3.31.jar ->org.apache.struts2.dispatcher.ng.filter -> StrutsPrepareAndExecuteFilter --> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 為過濾器指定路由 --> <filter-mapping> <!-- 指定過濾器名稱:struts2 --> <filter-name>struts2</filter-name> <!-- 路由規則 /* 代表全部 --> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> ``` 配置信息的關系如下: # ![](https://box.kancloud.cn/28f05466af76ab6e2f525ebaaf5c3515_1198x465.png) ## V:helloStruts.jsp struts能夠找到WebContent下的jsp文件渲染。但人們更習慣將一些模板文件建立在WebContent下的WEB-INFO目錄下。沒錯,web.xml也在這個目錄下,這更多的是出于安全角度來考慮的,因為tomcat的策略是:用戶無法直接訪問WEB-INFO目錄下的任何文件,所以做為V層的模板文件放在此,當然最合適不過了。 在WebContent/WEB-INFO中新建jsp/helloStruts.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> <h2>hello struts!</h2> </body> </html> ``` 有了jsp文件,下面,我們通過配置struts,來達到展示該V層文件的目的。 # 初始化struts.xml 在web.xml配置好struts后,它會自動加載src文件夾中的struts.xml文檔,以達到配置struts的目的。 在src中,我們新建struts.xml文檔,并寫入標準的文件頭,來告知此文件的編碼及遵循的dtd格式。 > 我們只需要知道:不同的dtd格式,解析的方式會不同 同時,寫入基本的struts標簽。 > 在前期,我們其實可以完全的不去關心這到底是什么,又是為什么要這樣寫,僅管這樣寫就好了。 ``` <?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> </struts> ``` 保存后,注意看eclipse的提示,在文件名上是否有紅色的X產生,如果有的話,檢查拼寫。 重啟Tomcat,以使得我們剛剛建立的配置文件生效。注意看控制臺信息,發生拼寫錯誤時,會報錯。 # 新建hellostruts路由 ``` <?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> <!-- 聲明包名,并指定命名空間/hello,同時繼承于struts-default。 這與我們定義一個class相類似 --> <package name="default" namespace="/hello" extends="struts-default"> <!-- 設置hellostruts Action --> <action name="struts"> <!-- 設置V層,相對于WebContent的絕對路徑 --> <result>/WEB-INF/jsp/helloStruts.jsp</result> </action> </package> </struts> ``` # 測試 重啟Tomcat 訪問以下url [http://localhost:8080/javaee/hello/struts](http://localhost:8080/javaee/hello/struts) ![](https://box.kancloud.cn/61aaa2c78ff10ce9fae6d824bf72154e_412x155.png) ## 代碼分析: ![](https://box.kancloud.cn/cc44440b34c8703a89f5c0da8d5da4be_1456x457.png) ## 未找到路由: ![](https://box.kancloud.cn/55ea1ee409beabced9620107dc82927a_475x252.png) 系統未找到路由時,會報如上錯誤。現在,我們啟用開發模式,來查看錯誤的詳細信息: ## 開發模式 ``` <struts> <!-- 開發模式 - ture --> <constant name="struts.devMode" value="true" /> ``` 重啟tomcat后,我們再測試 ![](https://box.kancloud.cn/e6b898d5132a636260001610d7a2bcb3_885x220.png) 此時,我們發送前臺為我們自動打印了命名空間及action名等信息。有了以上信息,我們去配置struts的xml文件就會變得輕松很多。 > 不僅如此,在開發模式下,當我們再次更改struts.xml時,會被時時的重新加載,從而不再需要重新啟動tomcat了。 ## 作業 使用以下URL來顯示hellostruts [http://localhost:8080/javaee/hellostruts](http://localhost:8080/javaee/hellostruts) 使用如下信息定義XML,文件如何存放,前臺如何訪問: ``` <!-- 聲明包名,并指定命名空間\hello,同時繼承于struts-default。 這與我們定義一個class相類似 --> <package name="default" namespace="/mengyunzhi" extends="struts-default"> <!-- 設置hellostruts Action --> <action name="welcome"> <!-- 設置V層,相對于WebContent的絕對路徑 --> <result>/jsp/helloStruts.jsp</result> </action> </package> ``` > 當使用struts做為過濾器后,WebContent下的所有文件,用戶都無法直接訪問到了,這無疑提升了系統的安全性。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看