Struts配置文件解析:
1、首先使用Struts進行安裝配置,在當前應用下創建當前應用的目錄結構,,把Struts安裝文件中lib目錄下的JAR文件放到當前應用的lib目錄下。
2、在WEB-INF目錄下必須配置文件:
web.xml、struts-config.xml。
3、web.xml
? 以例子加注釋解析吧:
<?xml version="1.0" encoding="UTF-8"?><!--?固定格式 -->
<!DOCTYPE web-app
? <!-- Standard Action Servlet Mapping? servlet類映射到對應的URL--> ? ? <!-- Struts Tag Library Descriptors Struts自帶的標簽庫--> ? <taglib> ? <taglib>
? PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
? "
<!--?WEB應用配置 -->
<web-app>
? <display-name>HelloApp Struts Application</display-name>
?
? <!-- Standard Action Servlet Configuration? -->
? <servlet>
????<--?struts action類的servlet配置?-->
??? <servlet-name>action</servlet-name>
??? <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
???
??? <!--?配置Action類的初始化參數名為config,值為 /WEB-INF/struts-config.xml-->
??? <init-param>
????? <param-name>config</param-name>
????? <param-value>/WEB-INF/struts-config.xml</param-value>
??? </init-param>
???? <load-on-startup>2</load-on-startup>
? </servlet>
? <servlet-mapping>
??? <servlet-name>action</servlet-name>
??? <url-pattern>*.do</url-pattern>
? </servlet-mapping>
???
?<!-- The Usual Welcome File List 應用的默認顯示頁面-->
? <welcome-file-list>
??? <welcome-file>hello.jsp</welcome-file>
? </welcome-file-list>
? <!--?還有struts-nested.tld和struts-tiles.tld -->
? <taglib>
??? <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
??? <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
? </taglib>
??? <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
??? <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
? </taglib>
??? <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
??? <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
? </taglib>
</web-app>
4、servlet-config.xml
???? 用struts來寫bean和action類對應關系都在servlet-config.xml配置
???? 下面以配置文件加注釋加以說明:
<!--?配置文件固定格式 -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "
<!--?struts配置標簽 -->
<struts-config>
??<!--?bean的配置,指定bean名字為hello2Form,type對應的是bean類 -->
? <form-beans>
????? <form-bean name="hello2Form" type="hello2.Hello2Form" />
? </form-beans>
??<!--?action-mapping配置, 可以有多個action標簽-->
? <action-mappings>
????<!--
??????? input----action對應的請求頁面
??????? name----和bean標簽的name對應,是這個action對應的哪一個bean
??????? path ----action類對應的URL,是請求頁面路徑,使用時以path.do的形式。
??????? score----請求范圍,有兩種:request,session
??????? type ----指定對應的action類
??????? validate----指定是否通過FormBean驗證。
??? -->
??? <action input="/Hello2.jsp" name="hello2Form" path="/hello2" scope="request"? type="hello2.Hello2Action" validate="true">
???????<--???????????
??????????? name----轉發請求名
??????????? path? ----用于指定請求轉發的url
????????-->
????????<forward name="hello2" path="/Hello2.jsp" />
??? </action>
??? <!--
??????? 此action可以不通bean和action關聯,可以在當前應用下通過
??????? http://IP:port/applicationpath/hello.do,就可以轉發到logon.jsp頁面。
??????? path----和前一個action的path一樣,使用方式path.do
??????? forward----請求path.do對應的頁面
??? -->
??? <action path="/hello" forward="/logon.jsp" />
? </action-mappings>
? <!--?指定配置文件Resources的位置 -->
? <message-resources parameter="hello2.Resources" />
??
? <!--?用于bean的驗證 -->
? <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
??? <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
? </plug-in>
</struts-config>