安裝
下載是從 http://struts.apache.org/download.cgi 進(jìn)行下載的股毫,struts和struts2區(qū)別還是很大的孝扛,所以注意版本不能下錯(cuò)圾笨;還有下載時(shí)會(huì)有兩個(gè)版本乔遮,一種是完全版然遏,就是struts2的所有的包贫途,另一種是迷你包,web開發(fā)用這個(gè)迷你包就可以待侵,通常名字都是 struts-xxx-min 這個(gè)樣子丢早,xxx就是版本號(hào)。
下載后正常解壓就可以秧倾,需要記得解壓到哪里了怨酝,因?yàn)楹竺孢€要再往項(xiàng)目里添加外部包。
配置
需要配置的文件一般就是兩個(gè):web.xml那先,struts.xml
web.xml 文件
web.xml 配置文件是一個(gè) J2EE 的配置文件农猬,它決定如何用 servlet 容器來(lái)處理 HTTP 請(qǐng)求的元素。它不是嚴(yán)格意義上的一個(gè) Struts 2 的配置文件售淡,但它是一個(gè) Struts 2 工作時(shí)需要被配置的文件斤葱。
如前所述,這個(gè)文件為任何 web 應(yīng)用程序提供了一個(gè)入口點(diǎn)揖闸。Struts 2 應(yīng)用程序的入口點(diǎn)是一個(gè)在部署描述符(web.xml)中已定義的過(guò)濾器揍堕。因此,我們將在 web.xml 中定義 FilterDispatcher 類的入口楔壤。web.xml 文件需要在 WebContent/WEB-INF 文件夾下創(chuàng)建鹤啡。
本人就曾經(jīng)遇到過(guò)有關(guān)web.xml的神奇錯(cuò)誤,因?yàn)橹苯訌膭e的項(xiàng)目賦值戰(zhàn)踢過(guò)來(lái)的蹲嚣,所以有些東西都沒有改递瑰,導(dǎo)致到后來(lái)的項(xiàng)目一直是404報(bào)錯(cuò)祟牲,結(jié)果就是文件過(guò)濾的問(wèn)題,漲知識(shí)了抖部。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
這里倒數(shù)第三行就是關(guān)于文件過(guò)濾的問(wèn)題说贝。不要輕易改動(dòng)。
struts.xml 文件
struts.xml 文件包含配置信息慎颗,隨著動(dòng)作的開發(fā)乡恕,你將會(huì)修改這些配置信息。這個(gè)文件可以用來(lái)重寫應(yīng)用程序的默認(rèn)設(shè)置俯萎,例如 struts.devMode = false傲宜,還有定義在屬性文件中的其他設(shè)置。這個(gè)文件可以在文件夾** src** 下創(chuàng)建夫啊。
<?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="項(xiàng)目包名" extends="struts-default">
<action name="隨便一個(gè)名字"
class="調(diào)用的方法所在文件"
method="對(duì)應(yīng)方法名">
<result name="success">/跳轉(zhuǎn)到的jsp界面.jsp</result>
</action>
<-- more actions can be listed here -->
</package>
<-- more packages can be listed here -->
</struts>