您的位置:首页 > 其它

default-action-ref 和 welcome-file-list 标签的区别

2013-01-17 20:00 567 查看
default-action-ref 标签是 struts.xml 中的标签,意思是默认的动作引用,可以理解为当没有相应的 action 对应是,调用这个 action 引用,如下:

1:  <default-action-ref name="index" />


上面这段代码意味着如果在地址栏中输入的 action 不存在,即调用 index 这个 action。

但是,如果我们在地址栏中不指明 action ,即日常的访问主页的操作,通过 default-action-ref 标签是不能达到效果的,这个标签仅仅作用于地址栏存在 action 但是找不到相应 action 的情况下。

那么如何达到像平常的那样访问主页的操作呢?我们就需要在 web.xml 中配置了。

welcome-file 是 web.xml 中的标签,是 welcome-file-list 标签的子标签,顾名思义,是对欢迎页面的指定:

1:  <welcome-file-list>

2:      <welcome-file>index</welcome-file>

3:  </welcome-file-list>


当我们在 web.xml 中如是配置的话,我们就可以像访问主页一样直接定位到 index action 了。

既然是 list ,当然就可以在内部定义多个 标签,当出现多个 标签的时候,又是如何定位的呢?

1:  <welcome-file-list>

2:      <welcome-file>index1</welcome-file>

3:      <welcome-file>index2</welcome-file>

4:      <welcome-file>index3</welcome-file>

5:      <welcome-file>index4</welcome-file>

6:  </welcome-file-list>


我们可以在 中定义多个 标签,当浏览器发来请求的时候,服务器会根据 welcome-file-list 标签下的 标签进行遍历,一旦得到存在的 就立即 forward 到这个 。

================================================================================================

如果:

<welcome-file-list>

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

</welcome-file-list>

并且设置了:

<package name="default" namespace="/" extends="struts-default">

<default-action-ref name="index"></default-action-ref>

<action name="index">

<result>/default.jsp</result>

</action>

</package>

现在地址栏敲入:http://localhost:8080/项目名称

那么此时关键是看,index.jsp 页面是否存在。

如果不存在index.jsp 则执行默认action操作,如果存在index.jsp(即正常情况),则必然是转入index.jsp,即进入首页。

这一点可是不要忘记哦~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: