<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國際加速解決方案。 廣告
                記錄一下配置struts的基本步驟,大神請繞道。 ## 第一步:導入jar包 首先到[struts](https://struts.apache.org/)官網下載jar包,下載后解壓縮。? ![](https://box.kancloud.cn/2016-02-26_56cfbddbc2476.jpg)? 選擇struts-2.3.24.1-all.zip,下載全部文件。 在struts-2.3.24-all/struts-2.3.24/app目錄下找到struts2-blank.rar,解壓縮。在eclipse中新建dynamic web project,拷貝struts2-blank/WEB-INF/lib目錄下的所有jar包到項目的WEB-INF/lib下。 ## 第二步:加入struts.xml 拷貝/Volumes/My struts-2.3.24/apps/struts2-blank/WEB-INF/src/java/struts.xml到工程的src目錄下。? 如果在struts.xml中不能自動提示或者在斷網狀態下也能自動提示,需要手動導入dtd文件。? ![](https://box.kancloud.cn/2016-02-26_56cfbddc007e4.jpg) ## 第三步:在web.xml中加入過濾器代碼 在web.xml增filter配置,filter-name可以自定義,比如struts2,filter-class在struts2-core-2.3.24.jar/org.apache.struts2.dispatcher.ng.filter/StrutsPrepareAndExecuteFilter.class,復制名稱即可。? ![](https://box.kancloud.cn/2016-02-26_56cfbddc2f093.jpg) ~~~ <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> ~~~ ## 四、配置文件詳解 在struts.xml中,主要有四種標簽 1. **bean標簽**? bean標簽用于創建一個javabean 2. **package標簽**? 用來管理不同的請求,比如前臺請求和后臺請求,分別放到不同的package里面。 | property | value | function | | --- | --- | --- | | name | 自定義包名,如test | 用戶被別的包調用后繼承 | | extends | 包名,默認為strut-default | 繼承別的包 | | namespace | 自定義,如/new | action訪問路徑 | 3.?**constant標簽**? struts默認行為標簽,[官網文檔](https://struts.apache.org/docs/strutsproperties.html) | property | value | function | | --- | --- | --- | | struts.i18n.encoding | UTF-8 | 配置web默認編碼集 | | struts.action.extension | 默認action,可選do或者其他 | struts默認請求為.action,配置后do和action都可 | | truts.enable.DynamicMethodInvocation | true或false | 開啟后支持動態方法 | | struts.configuration.xml.reload | true或false | 開啟后struts.xml修改后可以自動重新加載 | | struts.serve.static.browserCache | true或false | 開啟后瀏覽器自動緩存靜態內容,開發階段可以關閉便于測試 | | struts.ui.theme | xhtml、simple、css_xhtml | 視圖主題 | 4.?**include標簽**? 引入其它的web.xml,便于管理和維護. ## 五、登錄實例 工程目錄:? ![](https://box.kancloud.cn/2016-02-26_56cfbddc48e73.jpg)? 在src目錄下新建包cn.ac.ucas.action,添加UserAction.java,新增username、password屬性和響應的set和get方法、execute方法。execute方法中驗證用戶名和密碼,如果正確返回success、錯誤返回fail。 ~~~ package cn.ac.ucas.action; public class UserAction { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() { if (username.equals("admin") && password.equals("123456")) { return "success"; }else{ return "fail"; } } } ~~~ 在struts.xml中配置action: ~~~ <constant name="struts.action.extension" value="action,do"></constant> <constant name="devModel" value="true"></constant> <package name="test" namespace="/user" extends="struts-default"> <action name="login" class="cn.ac.ucas.action.UserAction"> <result name="success">/success.jsp</result> <result name="fail">/fail.jsp</result> </action> </package> ~~~ execute方法返回success則跳轉至success.jsp,返回fail則跳轉至fail.jsp頁面.struts.action.extension屬性是的action后綴為.action和.do均可。? 在webcontent目錄下新增index.jsp、success.jsp、fail.jsp。? index.jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path=request.getContextPath(); %> <!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="user/login.action"> 用戶名:<input type="text" name="username"> 密碼:<input type="text" name="password"> <input type="submit" value="提交"> </form> </body> </html> ~~~ success.jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>success</title> </head> <body> <h1>登錄成功!</h1> </body> </html> ~~~ fail.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>fail</title> </head> <body> <h1>登錄失敗!</h1> </body> </html> ~~~ 測試:? 用戶名和密碼與action里設置的一樣時登錄成功,不一樣時登錄失敗:? 登錄頁:? ![](https://box.kancloud.cn/2016-02-26_56cfbddc62f24.jpg)? 結果頁:? ![](https://box.kancloud.cn/2016-02-26_56cfbddc86053.jpg) ## **ps** 如果把struts.action.extension的值自定義,會有很好玩的事情:? 比如: ~~~ <constant name="struts.action.extension" value="action,do,html"></constant> ~~~ 這樣我們可以訪問一.html為后綴的action.? ![](https://box.kancloud.cn/2016-02-26_56cfbddca62c0.jpg) 總結:? 1.按照步驟一步一步來,知其然也知其所以然。? 2.配置namespace的時候前端訪問action 時候注意路徑.? 錯誤的寫法: ~~~ <form action="/user/login.html" method="post"> ~~~ 正確的寫法: ~~~ <form action="user/login.html" method="post"> ~~~
                  <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>

                              哎呀哎呀视频在线观看