Struts(2) struts.xml

通常struts.xml都是如下形式:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

      <!--在此进行相应的配置-->


</struts>

 struts元素下有五个元素,只讲解package、constant、include;

package:    

在Struts2框架中是通过包来管理action、result、interceptor、interceptor-stack等配置信息的。

属性:

name 给包取名,必填,作为其它包应用本包的标记;

extends 通常每个包都应该继承struts-default包, struts-default包是由struts内置的,它定义了struts2内部的众多拦截器和Result类型。

当一个包通过配置extends属性继承了另一个包的时候,该包将会继承父包中所有的配置,包括action、result、interceptor等。
由于包信息的获取是按照配置文件的先后顺序进行的,所以父包必须在子包之前被定义。

namespace  主要是针对大型项目中Action的管理,更重要的是解决Action重名问题,因为不在同一个命名空间的Action可以使用相同的Action名。

在浏览器中的URL为  http://localhost:端口/内容路径/namespace名/action名;例如下面配置中URL:http://localhost:8080/strutsProject/test/hello.action;

<package name="mypk" extends="struts-default" namespace="test">
       <action name="hello" class="com.po.Hello">
         <result name="success">index.jsp</result>
       </action>
   </package>

 abstract=“true”定义为抽象包,抽象包中不能包含action

package元素下有个action元素:

action  属性:

name 请求的action的名称;

class action处理类对应的具体路径;

Method 指定action中的方法名;

converter 指定action中类型转换器;

action下有个result元素:

result属性:

name 对应Action返回逻辑视图名称,默认为success;

type 返回结果类型,默认为dispatcher

在开发时,我们经常会把配置文件放到一个文件夹里,而不会就放在src下;

这样就要在web.xml写入相应的配置:

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  <init-param>
    <param-name>config</param-name>
    <param-value>struts-default.xml,struts-plugin.xml,config/struts/struts.xml</param-value>
  </init-param>
</filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

原文地址:https://www.cnblogs.com/yzdqxing/p/3800134.html