Struts2中Action的搜素顺序

来源:互联网 发布:软件健壮性 软件可靠性 编辑:程序博客网 时间:2024/06/05 06:27

当我们在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哦

原创粉丝点击