溫故知新:struts2_01整體流程感受

          使用了一陣子struts2,但是一直沒能好好整理一下,所以將平時學習工作中的一些心得記錄一下,溫故知新,時刻保持空杯心態。

          環境如下
          系統:64位win7


          測試環境:



          先從一個helloworld的簡單案例開始吧
          新建一個maven項目,選擇


          其實不論使用哪個框架技術,都無非三步走,import,config,run,首先,我們import
          因為使用maven來管理項目,所以直接添加struts2的依賴到pom.xml
              <dependency>
                  
          <groupId>org.apache.struts</groupId>
                  
          <artifactId>struts2-core</artifactId>
                  
          <version>2.3.7</version>
              </dependency>
          之后,我們開始config,具體配置哪些呢,首先要讓struts2去過濾請求,那么肯定要在web.xml中配置struts2的過濾器,然后還需要讓struts2知道請求和控制器之間的關系,那么肯定還需要再給struts2單獨進行配置。
          首先添加struts2的過濾器到web.xml中
              <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>

          其次添加struts2的配置struts.xml,特別說一下constant的name值是配置在org.apache.struts.default.properties中的,還有其他常用的常量,比如struts.action.extension=action,,,請求后綴等
          配置頭部的dtd在jar包中會有struts-2.X.dtd的配置,也不用到處搜索了。
          <?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.devMode" value="true" /><!-- 開發模式開啟,能夠顯示更詳細的異常信息-->  
             <package name="helloworld" extends="struts-default" namespace="/"><!-- struts-default這個包是一定要繼承的,否則struts很多重要功能會失效-->
                
          <action name="hello" <!-- action的名稱,namespace的值連接/hello表示請求該action  -->
                      class
          ="demo.action.HelloWorld"><!-- class表示該action的位置,在action的配置中,如果不指定調用哪個方法,則默認調用execute方法  -->
                      
          <result name="success">/helloWorld.jsp</result><!-- action的返回結果,以及相應的視圖  -->
                
          </action>
             
          </package>

          </struts>

          action的內容
          package demo.action;

          public class HelloWorld {
              
          public String execute() {
                  System.out.println(
          "Hello Struts2");
                  
          return "success";
              }
          }

          整個配置的過程就是這樣了,非常簡易。

          posted on 2014-10-29 15:52 都較瘦 閱讀(108) 評論(0)  編輯  收藏 所屬分類: MVCFramework

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

          博客定位:囿于目前的水平,博客定位在記錄自己的學習心得和隨手的練習

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 绵阳市| 灌南县| 连山| 刚察县| 长岭县| 原平市| 通辽市| 通海县| 噶尔县| 武强县| 壶关县| 南昌市| 连州市| 连云港市| 江孜县| 榆林市| 玛曲县| 铜川市| 广元市| 贡觉县| 新邵县| 弋阳县| 普兰店市| 永丰县| 鞍山市| 泾源县| 东平县| 房山区| 西峡县| 白水县| 射阳县| 万山特区| 丹阳市| 华容县| 永仁县| 金昌市| 女性| 牡丹江市| 建瓯市| 宁陵县| 盐边县|