struts學(xué)習(xí)筆記

傳統(tǒng)的mvc模式

Struts開源架構(gòu)很好的實(shí)現(xiàn)了MVC模式,MVC即Model-View-Controller的縮寫,是一種常用的設(shè)計(jì)模式。MVC 減弱了業(yè)務(wù)邏輯接口和數(shù)據(jù)接口之間的耦合,以及讓視圖層更富于變化。

實(shí)現(xiàn)一個(gè)簡單的struts案例

實(shí)現(xiàn)步驟

  • 創(chuàng)建一個(gè)javaweb工程,并選中struts2,
  • 創(chuàng)建jsp頁面(創(chuàng)建視圖)
  • 創(chuàng)建action類(Action 類是 Struts 2 應(yīng)用程序的關(guān)鍵,并且我們在 action 類中實(shí)現(xiàn)大部分的業(yè)務(wù)邏輯)
  • 配置文件

創(chuàng)建一個(gè)javaweb工程,并選中struts2,

在idea中創(chuàng)建好之后,會自動導(dǎo)入struts相關(guān)的核心包,并在web.xml中初始化配置一些相關(guān)的文件

完整項(xiàng)目路徑

創(chuàng)建index.jsp頁面

<%--
  Created by IntelliJ IDEA.
  User: pc
  Date: 17-5-22
  Time: 上午8:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
hello!
  </body>
</html>

成功時(shí),會在jsp中顯示一句話hello!

創(chuàng)建action類 UserAction

package action;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by pc on 17-5-22.
 */
public class UserAction extends ActionSupport {

    //action中業(yè)務(wù)處理方法
    public String login() {
        System.out.println("User.action");
        return "success";
    }

    public String register() {
        System.out.println("User.action User.action");
        return "success";
    }
}

1.Action映射:
action映射是Struts2框架中的基本” 工作單元”,action映射就是將一個(gè)請求URL(即action的名字)映射到一個(gè)action類,當(dāng)一個(gè)請求匹配某個(gè)action的名字時(shí),框架就使用這個(gè)映射來確定如何處理請求。


屬性   是否必須    說明
name      是      action的名字,用于匹配URL
class     否      Action實(shí)現(xiàn)類的完整類名
method    否     執(zhí)行Action類時(shí)調(diào)用的方法
convert   否     應(yīng)用于action的類型轉(zhuǎn)換的完整類名

2.使用method屬性
在配置action時(shí),我們可以通過action元素的method屬性來指定action調(diào)用的 方法,所指定的方法,必須遵循與execute方法相同的格式。

配置文件

  • 在web.xml中需要如下配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <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>
</web-app>
  • 在action包下需要再次創(chuàng)建一個(gè)struts.xml
<?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-default,因?yàn)樵摪心J(rèn)的攔截器等東西 -->
    <!--namespace:命名空間,考慮到可能出現(xiàn)相同的action,如果有命名空間就可以解決  -->
    <package name="config" namespace="/user" extends="struts-default" abstract="false">
         <!-- 定義一個(gè)action -->
         <!--對應(yīng)UserAction中l(wèi)ogin方法-->
        <action name="login" class="action.UserAction" method="login">
             <!--  返回一個(gè)String常量,轉(zhuǎn)發(fā)至對應(yīng)的JSP頁面--> 
            <result name="success">/index.jsp</result>
        </action>
        <!--對應(yīng)UserAction中register方法-->
        <action name="register" class="action.UserAction" method="register">
            <result name="success">/index.jsp</result>
        </action>
    </package>

</struts>

常量配置:用到 constant 標(biāo)簽 ,name 和 value 兩屬性

命名空間: namespace=”/user”,http://localhost:8080/user/login.action 查找條件是優(yōu)先到指定命名空間尋找對應(yīng)的action,如果找到就用不再繼續(xù)找,如果找不到,則到默認(rèn)的命名空間找,否則報(bào)錯(cuò)。

在大部分應(yīng)用里,隨著應(yīng)用規(guī)模的增加,系統(tǒng)中Action的數(shù)量也會大量增加,導(dǎo)致struts.xml配置文件變得非常臃腫。為了避免struts.xml文件過于龐大、臃腫,提高struts.xml文件的可讀性,我們可以將一個(gè)struts.xml配置文件分解成多個(gè)配置文件,然后在struts.xml文件中包含其他配置文件。

  • 在src根目錄下面找到struts.xml,這個(gè)文件稱為總配置文件,作用是引入其他所有配置文件
<?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>

    <!--全局配置-->
    <!--1.修改struts默認(rèn)的訪問后綴-->
    <constant name="struts.action.extension" value="action,do,"/>
    <!--總配置文件,引入其他所有配置文件-->
    <include file="action/struts.xml"></include>
</struts>

struts2的工作流程

  1. 客戶端提起一個(gè)(HttpServletRequest)請求,在瀏覽器中輸入http://localhost:8080/user/login就是提起一個(gè)(HttpServletRequest)請求。其中user就是namespace="/user",login<action name="login" class="action.UserAction" method="login">
  2. 在web.xml文件中定義核心Filter攔截用戶請求。
  3. 通過post或get提交請求。
  4. 定義處理用戶請求的Action類。在MVC框架中,控制器C實(shí)際上是由攔截所有用戶請求,處理請求的通用代碼兩個(gè)部分共同組成的,實(shí)際的業(yè)務(wù)邏輯則由Action來處理。
成功時(shí)的截圖
UserAction中打印的字符串

struts.xml中常用到的標(biāo)簽

1、<include>
利用include標(biāo)簽,可以將一個(gè)struts.xml配置文件分割成多個(gè)配置文件,然后在struts.xml中使用<include>標(biāo)簽引入其他配置文件。
3、<package>
1、包

在Struts2框架中是通過包來管理action、result、interceptor、interceptor-stack等配置信息的。包屬性如下:

屬性          是否必需       描述

name            是        包名,作為其它包應(yīng)用本包的標(biāo)記
extends      否      設(shè)置本包繼承其它包
namespace      否        設(shè)置包的命名空間
abstact      否      設(shè)置為抽象包
<package name="config" namespace="/user" extends="struts-default" abstract="false">
        <action name="login" class="action.UserAction" method="login">
            <result name="success">/index.jsp</result>
        </action>

        <action name="register" class="action.UserAction" method="register">
            <result name="success">/index.jsp</result>
        </action>
    </package>

2、extends

當(dāng)一個(gè)包通過配置extends屬性繼承了另一個(gè)包的時(shí)候,該包將會繼承父包中所有的配置,包括action、result、interceptor等。
由于包信息的獲取是按照配置文件的先后順序進(jìn)行的,所以父包必須在子包之前被定義。
通常我們配置struts.xml的時(shí)候,都繼承一個(gè)名為“struts-default.xml”的包,這是struts2中內(nèi)置的包。

<package name="config" namespace="/user" extends="struts-default" abstract="false">

3、namespace

namespace主要是針對大型項(xiàng)目中Action的管理,更重要的是解決Action重名問題,因?yàn)椴辉谕粋€(gè)命名空間的Action可以使用相同的Action名的。

namespace="/user"

4、<action>與<result>

1、<action>屬性介紹

屬性名稱  是否必須  功能描述

name        是   請求的Action名稱
class      否    Action處理類對應(yīng)具體路徑
method    否 指定Action中的方法名
converter   否   指定Action使用的類型轉(zhuǎn)換器

如果沒有指定method則默認(rèn)執(zhí)行Action中的execute方法。

2、<result>屬性介紹

屬性名稱 是否必須 功能描述

name    否   對應(yīng)Action返回邏輯視圖名稱,默認(rèn)為success
type    否   返回結(jié)果類型,默認(rèn)為dispatcher

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容