struts1.2 配置文件详解

本文深入探讨了Struts框架的web.xml配置文件与struts-config.xml配置文件的关键要素,包括servlet、初始化参数、欢迎文件、错误处理、异常处理、标签库配置等,详细解释了各元素的作用与配置方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >








struts


配置文件


 


web.xml





web


应用的发布描述文件。








 


struts-config.xml





struts


应用专有的配置文件


,


可以根据需要给这个配置文件起其它的文件名。


 


一、


web


配置文件


web.xml 


<servlet>


元素必须出现在


<servlet-mapping>


元素前面,


<servlet-mapping>


元素必须出现在


<taglib>


元素的


前面。


 


<init-param> 


<param-name>config</param-name> 


<param-value>/WEB-INF/struts-config.xml</param-value> 


</init-param> 


<init-param>


元素用来声明


servlet


初始化参数,


config


参数是配置


struts





ActionServlet


专有的,用来设





struts


配置文件的相对路径。


 


<welcome-file-list>











不能直接把


struts





Action


作为欢迎文件,


以下是在欢迎文件中调用


struts action





 


<global-forwards> 


<forward name="welcome" path="HelloWorld.do"/> 


</global-forwards>


 


<body> 


<logic:forward name="welcome"/> 


</body>


 


<welcome-file-list> 


<welcome-file>welcome.jsp</welcome-file> 


</welcome-file-list>


 


配置错误处理


 


<error-page> 


<error-code>404</error-code> 


<location>/common/404.jsp</location> 


</error-page> 


<error-page> 


<error-code>505</error-code> 


<location>/common/505.jsp</location> 


</error-page>


 


























 


配置异常处理


 


<error-page> 


<exception-type>javax.servle.ServletException</exception-type> 


<location>/common/system_error.jsp</location> 


</error-page>


 


配置标签库


 


<taglib> 


<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> 


<taglib-location>WEB-INF/struts-html.tld</taglib-location> 


</taglib>


 


<taglib> 


<taglib-uri>/WEB-INF/mytaglib.tld</taglib-uri> 


<taglib-location>WEB-INF/mytaglib.tld</taglib-location> 


</taglib> 


web


版本在


2.4


版本中允许不在


web.xml


中配置


taglib


描述文件而直接在页面上引用。


 


 


二、


struts


配置文件


struts-config.xml 


<struts-config>

















Struts





















































org.apache.struts.config.ModuleConfig








<struts-config>


元素有


8


个子元


素。他们的


DTD











data-sources?form-bean? 


global-exception?global-forwards?action-mapping?controller?message-resources*plug-in* 


<data-sources>


元素:配置应用数据源,一般没用。








 


<form-beans>


元素


:用来配置多个


ActionForm


,包含一个或者


N





<form-bean>


子元素。


name


指定该


ActionForm


的唯一标识符,这个属性是必须的以后作为引用使用;


type


指定


ActionForm


类的完整类名,


这个属性也是必须的。


 


该元素主要用来配置表单验证的类。它包含如下属性:


 


 


 


1. 


className
































form-bean






































org.apache.struts.config.FormBeanConfig


,如果自定义,则必须扩展


FormBeanConfig


类,可有可无。


 


 


 


2. 


name





ActionForm Bean


的惟一标识。必须。


 


 


 


3. 


type





ActionForm


的完整类名。必须。


 


如下所示:


 


























<form-beans> 


 


 


 


<form-bean 


 


 


 


 


 


 


 


name="Loign" 


 


 


 


 


 


 


 


type="com.ha.login"> 


 


 


 


</form-bean> 


 


</form-beans> 


若配置动态


ActionForm Bean,


还必须配置


form-bean


元素的


form-property


子元素。


 


<global-exceptions>


元素


:用来配置异常处理,一般没用。








 


<global-forwards>


元素


:用来声明全局的转发关系。元素可以有一个或者


N





<forward>


元素组成,用于


把一个逻辑名映射到特定的


URL


,通过这种方法


Action


类或者


JSP


页面无需指定


URL


,只要指定逻辑名


称就可以实现请求转发或者重定向。


 


该元素主要用来声明全局的转发关系,它具有以下四个属性:


 


 


 


1. 


className


:和


forward


元素对应的配置类,默认为:


org.apache.struts.action.ActionForward


。可


有可无。


 


 


 


2. 


contextRelative


:此项为


true


时,表时


path


属性以"


/


"开头,相对于当前上下文的


URL


,默认为


false


.可有可无。


 


 


 


 


3. 


name


:转发路径的逻辑名.必填。


 


 


 


4. 


path





转发或重定向的


URL








contextRelative=false


时,


URL


路径相对于当前应用





application








当为


ture


时,表示


URL


路径相对于当前上下文(


context








 


 


 


5. 


redirect


:当此项为


ture


时,表示执行重定向操作。当此项为


false


时表示转向操作。默认为


false





 


如下所示:


 


<global-forwards> 


 


 


 


<forward name="forms1





 path="/a.do"/> 


 


 


 


<forward name="forms2





 path="/nb.jsp"/> 


 


<global-forwards>


 


<action-mapping>


元素:包含一个或者


N





<action>


元素,描述了从特定的请求路径到响应的


Action


的映


射。


 


上述从特定的请求路径到相应的


Action


类的映射。它具有以下几个属性:


 


 


 


1. 


attribute





设置和


Action


关联的


ActionForm Bean





request





session


范围内的


key





如:


Form Bean


存在于


request


范围内,此项设为





myBenas"


,则在


request.getAttribute("myBenas")


就可以返回该


Bean


的实例。


 


 


 


2. 


classsName


:和


action


元素对应的配置元素,默认为:


org.apache.struts.action.ActionMapping. 


 


 


3. 


forward


:转发的


URL


路径。


 


























 


 


4. 


include


:指定包含的


URL


路径。


 


 


 


5. 


input


:输入表单的


URL


路径,当表单验证失败时,将把请求转发到该


URL





 


 


 


6. 


name


:指定和


Action


关联的


Action FormBean


的名字,该名字必须在


Form-Bean


定义过。


 


 


 


7. 


path


:指定访问


Action


的路径,以





/"


开头,无扩展名。


 


 


 


8. 


parameter


:指定


Actgion


的配置参数,在


Action


类的


execute()


方法中,可以调用


ActionMapping


对象的


getParameter()


方法来读取该配置参数。


 


 


 


9. 


roles





指定允许调用该


Action


的安全角色,


多个角色之间用,


隔开,


在处理请求时,


RequestProcessor


会根据该配置项来决定用户是否有权限调用


Action


权限。


 


 


 


10. 


scope


:指定


ActionForm Bean


的存在范围,可选取为


request





session


,默认为


session





 


 


 


11. 


type


:指定


Action


类的完整类名。


 


 


 


12. 


unknown


:如果此项为


true


,表示可以处理用户发出的所有无效的


Action URL


,默认为


false





 


 


 


13. 


validate





指定是否要调用


Action FormBean





validate


方法,


默认值为


ture.


注:


forward





include





type


属性只能选中其中一项。


 


<action path="/search" 


 


 


 


 


 


type="zxj.okBean" 


 


 


 


 


 


name="a1" 


 


 


 


 


 


scope="request" 


 


 


 


 


 


validate="true" 


 


 


 


 


 


input="/b.jsp"> 


 


 


 


<forward name="tig" path="/aa.jsp"/> 


 


</action>


 


注:此中的


forward


是指局部的转发路径。


global-forwards


表示全局的转发路径。


 


<


controller


>


元素:用于配置


ActionServlet.buffreSize


指定上载文件的输入缓冲大小,一般


 


不用。








 


<message-resources>


元素:用来配置


Resource Bundle





 


 


 


 


 


1. 


className








message-resources






































org.apache.struts.config.MessageResourcesConfig





 


 


 


2. 


factory





指定消息资源的工厂类,


默认为:


org.apache.struts.util.PropertyMessageResourcesFactory


类。


 


 


 


3. 


key











Resource 


Bundle











ServletContext





























Key














Globals.MESSAGES_KEY


定义的字符串常量,只允许一个


Resource Bundle


采用默认的属性


Key





 


 


 


4. 


null


:指定


MessageSources


类如何处理未知消息的


key


,如果为


true


,则返回空字符串,如果为


false


,则返回相关字串,默认为


false





 


























 


 


5. 


prameter


:指定


MessageSources


的资源文件名,如果为:


a.b.ApplicationResources


,则实际对


应的文件路径为:


WEB-INF/classes/a/b/ApplicationResources.properties





 


如下:


 


<message-resources null="false" parameter="defaultResource"/> 


 


<message-resources key="num1" null="false" parameter="test"/>


 


访问为:


 


<bean:message key="zxj"/> 


 


<bean:message key="zxj" bundle="num1"/>


 


其中,


zxj


表法,


messagesource


资源文件中的一个字符串。


 


<plug-in>


元素:用于配置


Struts


插件,配置多应用模块,一般不用。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值