struts.xml中的标签及属性详解

Package:

1.用意:用来管理action。

<struts>
	<package name="helloworld" namespace="/" extends="struts-default">
		<action name="helloWorldAction" class="com.itheima09.struts2.action.HelloWorldAction">
			<result name="index">index.jsp</result>
		</action>
	</package>
</struts>

2.name属性:为包的名字,是唯一的。

3.namespace:为命名空间,是针对访问时的url的。

当namespace="/hello"时,访问的时候需要访问  localhost:8080/项目名/hello/helloWorldAction.action,才能执行action中的execute方法。

而如果浏览器提交一个url为 localhost:8080/项目名/helloWorldAction.action,则url直接从项目到根目录查找,所以找不到。

而如果浏览器提交的是 localhost:8080/项目名/hello/a/helloWorldAction.action,那么会先找hello/a下的action,如果找不到,则查找上一层hello中的action,再找不到则再找上一层,直到找到,如果最上层也找不到,则报错。

当namespace="/"时,上述的命名空间针对的是: localhost:8080/项目名/helloWorldAction.action,只要命名空间后面加一层,最后跳转到相应的jsp的时候也会加上相应的路径去查找,如果找不到则页面会报404错误。

比如:

当namespace="/"时,会直接去WebRoot下查找result标签里面写的路径。

当namespace=“/hello”时,会去WebRoot/hello下查找result标签里面写的路径。

4.extends:

(1)在tomcat启动的时候,不仅加载了struts.xml文件,还加载了struts-default.xml文件而这个文件在classpath下。

该xml文件在struts2-core-2.3.1.2.jar这个包中,在struts-default.xml文件中有如下配置:

  <package name="struts-default" abstract="true">
        <result-types>
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
        </result-types>

        <interceptors>
		 ...
       </interceptors>

        <default-interceptor-ref name="defaultStack"/>

        <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
    </package>
可以看出,在包中继承了name="struts-default"包下的所有内容,而该包中默认的class为com.opensymphony.xwork2.ActionSupport,所以当包中不写class属性时,会默认跳转到ActionSupport类中执行该类中的execute方法,并且返回一个"success"字符串.

注意:

在action标签中,默认的执行方法就是execute()。

<package name="struts-default">是支持struts2底层运行的最核心的包。

5.Result:

代表一种结果集,type为结果集类型。

name属性的值和action中某一个方法的返回值一致(method属性所指定的那个方法,默认为execute方法)。name属性也可以不写,默认为"success"。 

type属性不写,则默认和struts-default中的结果集中的default="true"的结果集保持一致,为dispatcher,转发。并且result标签中的内容就是要转发到的页面。




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值