【struts2 配置文件】

1.包配置:

在struts.xml文件中package元素用于定义包配置,每个package元素定义了一个包配置。它的常用属性有:
name:必填属性,用来指定包的名字。
extends:可选属性,用来指定该包继承其他包。继承其它包,可以继承其它包中的Action定义、拦截器定义等。
namespace:可选属性,用来指定该包的命名空间。
 1 <!DOCTYPE struts PUBLIC
 2         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 3         "http://struts.apache.org/dtds/struts-2.0.dtd">
 4 <struts>
 5     <!-- struts2的action必须放在一个指定的包空间下定义 -->
 6     <package name="default" extends="struts-default">
 7     <!-- 定义处理请求URL为login.action的Action -->
 8         <action name="login" class="org.caijieru.web.struts.action.LoginAction">
 9         <!-- 定义处理结果字符串和资源之间的映射关系 -->
10             <result name="success">/success.jsp</result>
11             <result name="error">/error.jsp</result>
12         </action>
13     </package>
14 </struts>

-->如果没有为action中指定class,默认值是ActionSupport
-->如果没有为action中指定method,默认执行action中的excute()方法
-->如果没有指定result的name属性,默认值是success

2.命名空间配置 :

考虑到同一个Web应用中需要同名的Action,Struts2以命名空间的方式来管理Action,同一个命名空间不能有同名的Action。

Struts2通过为包指定namespace属性来为包下面的所有Action指定共同的命名空间。

 1 <!DOCTYPE struts PUBLIC
 2         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 3         "http://struts.apache.org/dtds/struts-2.0.dtd">
 4 <struts>
 5     <!-- struts2的action必须放在一个指定的包空间下定义 -->
 6     <package name="struts2" extends="struts-default">
 7     <!-- 定义处理请求URL为login.action的Action -->
 8         <action name="login" class="org.caijieru.web.struts2.action.LoginAction">
 9         <!-- 定义处理结果字符串和资源之间的映射关系 -->
10             <result name="success">/success.jsp</result>
11             <result name="error">/error.jsp</result>
12         </action>
13     </package>
14    
15     <package name="it" extends="struts-default" namespace="/manage">
16     <!-- 定义处理请求URL为login.action的Action -->
17         <action name="backLogin" class="org.caijieru.web.struts2.action.LoginAction">
18         <!-- 定义处理结果字符串和资源之间的映射关系 -->
19             <result name="success">/success.jsp</result>
20             <result name="error">/error.jsp</result>
21         </action>
22     </package></struts>

以上配置陪了两个包,struts2和it包,配置it包时指定了该包的命名空间为/manage。

对于包struts2:没有指定namespace属性。如果某个包没有指定namespace属性,即该包使用默认的命名空间,默认为null。
对于包it:指定了命名空间/manage,则该包下所有的Action处理的URL应该是“命名空间/Action名”。如上名为backLogin的Action,
它处理的URL为:http://localhost:8080/userlogin_struts2/manage/backLogin.action

3.包含配置

1 <struts>
2     <include file="struts-default.xml"/>
3     <include file="struts-user.xml"/>
4     <include file="struts-book.xml"/>
5     <include file="struts-shoppingCart.xml"/>
6    
7     ......
8    </struts>

 

 

在Struts2中可以将一个配置文件分解成多个配置文件,那么我们必须在struts.xml中包含其他配置文件。

 

 

 

 

其他:Result的各种转发类型

 

浏览器重定向:就是引导客户的浏览器到指定的路径文件,是不能访问到WEB-INF目录下的文件的。

1 <action name="helloword" class="com.cxy.HelloWordAction" method="execute">
2 <result name="success">/jsp路径</result> ////内部请求转发
3 </action>
4 
5 
6 <action name="redirect">
7 <result type="redirect">/xx.jsp</result> //浏览器重定向
8 </action>

 

 posted on 2012-05-22 12:32  蔡傑儒  阅读(294)  评论(0编辑  收藏  举报