一砰碴、創(chuàng)建一個(gè)動(dòng)態(tài)web項(xiàng)目
二、導(dǎo)包:
先下載Struts2環(huán)境搭建所需文件
密碼:htcs
我們可以直接把文件夾“jar包”下的jar包拷貝到項(xiàng)目中/WebContent/WEB-INF/lib
中趁尼。jar包
這個(gè)文件夾是我單獨(dú)從struts-2.3.24-all.zip
中抽取出來的猖辫。
抽取方式:先解壓struts-2.3.24-all.zip
,然后在struts-2.3.24-all\struts-2.3.24\apps
下找到struts2-blank.war
啃憎,如下圖:
繼續(xù)解壓struts2-blank.war
,然后打開這個(gè)解壓后的文件夾悯姊,在struts2-blank\WEB-INF\lib
下的所有jar即為環(huán)境搭建所需要的包贩毕。
三、導(dǎo)入約束:
在dtd
文件夾下岸晦,此處參考hibernate環(huán)境搭建中的第四大點(diǎn)的第一小點(diǎn)睛藻。
四、編寫一個(gè)Action:
1)在src
下創(chuàng)建一個(gè)包(包名任意)店印,在包中創(chuàng)建一個(gè)類HelloAction
:
public class HelloAction {
public String hello(){
System.out.println("hello world!");
return "success";
}
}
五、編寫Struts2的配置文件:
在src
根目錄下創(chuàng)建一個(gè)xml文件包券,命名為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>
<package name="hello" namespace="/hello" extends="struts-default">
<action name="HelloAction" class="com.zl.a_hello.HelloAction" method="hello">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
其中:
package:將Action配置封裝.就是可以在Package中配置很多action.
屬性:
name屬性: 給包起個(gè)名字,起到標(biāo)識(shí)作用.隨便起.不能其他包名重復(fù).
namespace屬性:給action的訪問路徑中定義一個(gè)命名空間
extends屬性: 繼承一個(gè) 指定包
.
action元素:配置action類
屬性:
name屬性: 決定了Action訪問資源名.
class屬性: action的完整類名
method屬性: 指定調(diào)用Action中的哪個(gè)方法來處理請(qǐng)求
.
result元素:結(jié)果配置
屬性:
name屬性: 標(biāo)識(shí)結(jié)果處理的名稱.與action方法的返回值對(duì)應(yīng).
標(biāo)簽體:填寫頁面的相對(duì)路徑侍郭,此處轉(zhuǎn)發(fā)到hello.jsp
六、配置核心過濾器:
在WebContent/WEB-INF/web.xml
中配置
<!-- struts2核心過濾器 -->
<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>
此時(shí)猛计,框架已經(jīng)搭建完成爆捞,我們先把項(xiàng)目發(fā)布到tomcat
中,然后在瀏覽器中輸入http://localhost:8080/struts1/hello/HelloAction
地址煮甥,就能成功訪問搭建的項(xiàng)目。