当我们在struts.xml中配置action的时候,设置了package的namepace,但浏览器打开的路径与其不相同也能运行action。
比如:我们的创建一个struts2项目,项目名为:struts2。struts.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" namespace="/test" extends="struts-default">
<action name="helloworld" class="action.HelloWorldAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
理论上,我们在浏览器中输入的地址为:
http://localhost:8080/struts2/test/helloworld.action
才能运行helloworld.action。但是我们把地址改为
http://localhost:8080/struts2/aaaa/helloworld.action
也能访问。
Struts2中Action的搜素顺序:
当我们访问http://localhost:8080/strutsProject/path1/path2/path3/Test.action
的时候:
首先,判断package是否存在,如path1/path2/path3
- ①如果存在:判断Action是否存在,比如:Test.action。如果不存在则跳转到默认的namespace(在struts.xml中指定)中去找Action;如果不存在,则直接报错
- ② 如果不存在:依次检查上一级package是否存在(直到默认namespace),执行 ①。
虽然理论上我们只要正确输入Action就能运行,但推荐还是要正确输入package哦