馬士兵struts2視頻筆記--第一天

1类缤、開發(fā)準備  
2芜抒、第一個案例—Hello World 
      2.1.新建web project項目   
      2.2.配置項目  
            2.2.1 將開發(fā)庫導入lib文件夾下 
            2.2.2 在src包下新建struts.xml配置文件    
            2.2.3 修改struts.xml  
            2.2.4 修改web.xml文件為  
            2.2.5 訪問    
      2.3.原理    
3槐沼、namespace 
4、action    
5痛阻、path  
      5.1 path.jsp  
      5.2 index.jsp 
6毯炮、ActionMethod_DMI_動態(tài)方法調用   
      6.1 index.jsp頁面   
      6.2 struts.xml文件  
      6.3 UserAction.java   

1份殿、開發(fā)準備

jdk(http://www.oracle.com/technetwork/java/javase/downloads/index.html)
myeclipse
tomcat(http://tomcat.apache.org/)
struts2(http://struts.apache.org/download.cgi)
Struts2開發(fā)文檔(http://struts.apache.org/docs/getting-started.html)

2、第一個案例—Hello World

2.1.新建web project項目

File-new-web project枝秤,輸入項目名醋拧,如sturts2_001

2-1 新建項目.png

2.2.配置項目

2.2.1 將開發(fā)庫導入lib文件夾下

下載鏈接

2.2.2 在src包下新建struts.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <include file="example.xml"/>
    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>
     -->
    <!-- Add packages here -->
</struts>

2.2.3 修改struts.xml

在 下面添加:

<constant name="struts.devMode" value="true" />
     <package name="default" namespace="/" extends="struts-default">
        <action name="hello">
            <result>
                /Hello.jsp
            </result>
        </action>
 </package>

說明:

①<constant name="struts.devMode" value="true" />開發(fā)模式
②action name:訪問的名稱
③package:類似于java中的包淀弹,解決重名問題

2.2.4 修改web.xml文件為

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
     <welcome-file-list>
    <welcome-file>Hello.jsp</welcome-file>
  </welcome-file-list>
  
  <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>

2.2.5 訪問

新建Hello.jsp文件丹壕,然后部署項目,訪問localhost:8080/struts2/hello即可訪問到Hello.jsp頁面

圖2-2 項目結構.png

2.3.原理

圖3-1 struts2 核心原理.png

3垦页、namespace

<package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>

namespace決定了action的訪問路徑雀费,默認為"",可以接收所有路徑的action
namespace可以寫為’/’痊焊,或者’/xxx’盏袄,或者’/xxx/yyy’忿峻,對應的action訪問路徑為’/index.action’‘/xxx/index.action’,或者’/xxx/yyy/index.action’辕羽。
namespace最好也用模塊來進行命名

4逛尚、action

圖4-1 執(zhí)行過程.png

action不一定是servlet,可以是一個普通類刁愿。

圖4-2 程序結構.png

<action name="path" class="com.bjsxt.struts2.path.action.PathAction">
“class”是類的路徑绰寞。當class屬性沒有配置時,默認訪問ActionSupport類铣口。
action的三種實現(xiàn)方法

①普通類滤钱,手寫execute方法。缺點:容易出錯脑题。

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

②實現(xiàn)Action類件缸,重載execute方法。

public class IndexAction2 implements Action {
    @Override
    public String execute() {
        return "success";
    }
}

③繼承已經實現(xiàn)好的類叔遂,有很多可以直接用方法他炊。推薦使用

public class IndexAction3 extends ActionSupport {
    @Override
    public String execute() {
        return "success";
    }
}

5、path

struts2中的路徑問題是根據(jù)action的路徑而不是jsp路徑來確定已艰,所以盡量不要使用相對路徑痊末。
雖然可以用redirect方式解決,但redirect方式并非必要哩掺。
解決辦法非常簡單凿叠,統(tǒng)一使用絕對路徑。(在jsp中用request.getContextRoot方式來拿到webapp的路徑)
或者使用myeclipse經常用的嚼吞,指定basePath幔嫂。

圖5-1 程序結構.png

5.1 path.jsp

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
struts2中的路徑問題是根據(jù)action的路徑而不是jsp路徑來確定,所以盡量不要使用相對路徑誊薄。<br />
<a href="index.jsp">index.jsp</a>
<br />
雖然可以用redirect方式解決履恩,但redirect方式并非必要。
<br />
解決辦法非常簡單呢蔫,統(tǒng)一使用絕對路徑切心。(在jsp中用request.getContextRoot方式來拿到webapp的路徑)
<br />
或者使用myeclipse經常用的,指定basePath
</body>
</html>

注:前面設置好了<base>標簽片吊,就可以使用絕對路徑了绽昏。

5.2 index.jsp

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>

<%--
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//在head中<base href>指定basePath
--%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
    <a href="path/path">路徑問題說明</a>
</body>
</html>

6、ActionMethod_DMI_動態(tài)方法調用.

Action執(zhí)行的時候并不一定要執(zhí)行execute方法俏脊,也可以訪問其他方法全谤。
訪問方法有兩種:
①在配置文件中配置Action的時候用method=“”來指定執(zhí)行指定方法
②url地址中動態(tài)指定(動態(tài)方法調用DMI)(推薦)

6.1 index.jsp頁面

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>

<% String context = request.getContextPath(); %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
Action執(zhí)行的時候并不一定要執(zhí)行execute方法<br />
可以在配置文件中配置Action的時候用method=來指定執(zhí)行哪個方法
也可以在url地址中動態(tài)指定(動態(tài)方法調用DMI)(推薦)<br />
    <a href="<%=context %>/user/userAdd">添加用戶</a>
    <br />
    <a href="<%=context %>/user/user!add">添加用戶</a>
    <br />
前者會產生太多的action,所以不推薦使用
    
</body>
</html>

說明:第一個超鏈接是普通方法爷贫,對應struts.xml中第一個action认然,需要配置method屬性补憾。
第二個超鏈接是動態(tài)方法調用,struts.xml文件不需要改動卷员,推薦使用盈匾。

6.2 struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" extends="struts-default" namespace="/user">
        <action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">
            <result>/user_add_success.jsp</result>
        </action>
        
        <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
            <result>/user_add_success.jsp</result>
        </action>
    </package>
</struts>

6.3 UserAction.java

package com.bjsxt.struts2.user.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
    public String add() {
        return SUCCESS;
    }
}

馬士兵struts2視頻筆記--第一天
馬士兵struts2視頻筆記--第二天
馬士兵struts2視頻筆記--第三天

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市毕骡,隨后出現(xiàn)的幾起案子削饵,更是在濱河造成了極大的恐慌,老刑警劉巖未巫,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件窿撬,死亡現(xiàn)場離奇詭異,居然都是意外死亡叙凡,警方通過查閱死者的電腦和手機尤仍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來狭姨,“玉大人,你說我怎么就攤上這事苏遥”模” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵田炭,是天一觀的道長师抄。 經常有香客問我,道長教硫,這世上最難降的妖魔是什么叨吮? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮瞬矩,結果婚禮上茶鉴,老公的妹妹穿的比我還像新娘钻哩。我一直安慰自己澈侠,他們只是感情好,可當我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布熙含。 她就那樣靜靜地躺著伞插,像睡著了一般割粮。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上媚污,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天舀瓢,我揣著相機與錄音,去河邊找鬼耗美。 笑死京髓,一個胖子當著我的面吹牛航缀,可吹牛的內容都是我干的。 我是一名探鬼主播朵锣,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼谬盐,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了诚些?” 一聲冷哼從身側響起飞傀,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎诬烹,沒想到半個月后砸烦,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡绞吁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年幢痘,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片家破。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡颜说,死狀恐怖,靈堂內的尸體忽然破棺而出汰聋,到底是詐尸還是另有隱情门粪,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布烹困,位于F島的核電站玄妈,受9級特大地震影響,放射性物質發(fā)生泄漏髓梅。R本人自食惡果不足惜拟蜻,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望枯饿。 院中可真熱鬧酝锅,春花似錦、人聲如沸奢方。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽袱巨。三九已至阁谆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間愉老,已是汗流浹背场绿。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留嫉入,地道東北人焰盗。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓璧尸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親熬拒。 傳聞我的和親對象是個殘疾皇子爷光,可洞房花燭夜當晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內容