struts2筆記1

                            Struts 2

概述
1. struts 2 框架應用於java ee三層結構的web層框架
2. struts 2 框架在struts 1 和 webwork基礎上發展起來的全新框架
3. struts 2解決的問題
如果功能很多,就會創建很多的servlet,造成維護很不方便
這裏寫圖片描述
Servlet在struts2中稱爲action

這裏寫圖片描述
4.web層常用框架有struts 2 spring MVC
##入門 ##
1.建立web項目,導入jar包
在壓縮包中的實例程序中找,lib目錄中有jar包,不能全部導入,到apps目錄中找到一個示例程序,從示例程序中的lib目錄下複製jar包
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
下面的圖纔是要導入的
這個是要導入的
2.創建action,類似於創建servlet,即創建普通類
訪問action默認執行的方法是execute方法
3.配置action的訪問路徑
第一步:創建struts2的核心配置文件,名稱和位置都是固定的
必須在src下面,名稱struts.xml
第二步:引入dtd約束,在示例src中找struts.xml中複製

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

第三步:配置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>
    <package name = "test1" extends = "struts-default" namespace = "/">
        <action name = "hello" class = "hjj.action.Hello" method = "execute">
            <result name = "ok">/hello.jsp</result>
        </action>
    </package>
</struts>

第四步:配置過濾器,在示例程序的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>

放入自己項目的web.xml中去
這裏寫圖片描述

開始寫程序:
action的代碼:

public class HelloAction  {

    public String execute() {
        return "success";
    }
}

隨便寫個jsp頁面用來跳轉
還要在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>
    <package name = "test1" extends = "struts-default" namespace = "/">
        <action name = "hello" class = "hjj.action.Hello" method = "execute">
            <result name = "success">/hello.jsp</result>
        </action>
    </package>
</struts>

然後在瀏覽器輸入http://localhost:8080/struts2_day01/hello即可訪問action,會跳轉到hello.jsp頁面

注意:此處沒有配置過濾器或者action中的返回值沒有對應的result都會出現404錯誤

分析struts2執行過程

這裏寫圖片描述
下面還有
這裏寫圖片描述
過濾器分析:
這裏寫圖片描述
過濾器中的init()方法,主要加載配置文件,在服務器啓動時執行init方法,加載struts.xml和web.xml文件
doFilter()方法過濾

struts.xml分析

1.名稱和位置都固定:在src目錄下,名字是struts.xml
2.在配置文件中需要先引入約束,然後在<struts></struts>中配置
主要有三個標籤:package,action,result

package標籤

:類似於代碼包,區別不同的action,必須首先寫package標籤才能配置action
屬性:
name:只是爲了區分不同的action,取名任意,名字不重複即可
extends:屬性值必須是”struts-default”,有了這個屬性,類纔有了action的功能
namespace:默認是”/”,namespace和action標籤裏的name屬性值構成了訪問路徑

action標籤

主要作用配置action的訪問路徑
屬性:
name:package標籤裏面可以有多個action標籤,但是值不能一樣
class:action類的全路徑
method:反射要調用的方法,默認不寫是execute方法

result標籤

根據action方法的返回值,跳轉到不同的action,jsp或者頁面
name:用來匹配返回的值
type:用來配置如何到路徑中轉發或者重定向(轉發地址欄不變)
dispatcher地址不變(轉發) redirect地址改變重定向
struct2的默認常量值struts.i18n.encoding=”utf-8”,struts2在post方式提交時自己爲我們解決了亂碼問題,所以我們儘量使用post提交表單數據
***************************分模塊開發***************************************************
在action類的包下面建立一個hello.xml文件,寫法和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>
    <package name = "test1" extends = "struts-default" namespace = "/">
        <action name = "hello" class = "hjj.action.HelloAction" method = "execute">
            <result name = "success" type = "redirect">/hello.jsp</result>
        </action>
    </package>
</struts>

只要在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>
    <!-- 加載文件 -->
    <!-- 注意此處的xml文件的url前面要有包名 -->
    <include file = "hjj/action/helloAction.xml"></include>
</struts>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章