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>
既然是 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,即进入首页。
这一点可是不要忘记哦~~