3 Struts2的常见配置解析

1 package的相关配置

package标签:包,与Java中的包概念不一致。旨在更好的管理action
package标签的属性:

  •         name :  包的名称
  •         extends: 继承哪个包,通常值为Struts-default
  •         namespace: 名称空间,与<action>标签中的name属性共同决定访问路径

 名称空间有三种写法:

  •       带名称的名称空间:/aaa
  •       根名称空间:namespace="/"
  •       默认名称空间:namespace=""

                   先执行有名称的执行空间

     abstrcts:  抽象的,abstrcts="true"可以被继承

2 action相关配置

主要配置Action类:

  •    name属性: 与namespace共同决定访问路径
  •    class属性:类的访问路径
  •    method属性:默认执行方法为execute(),如为save,需要在类里面变更方法名为save
  •  (了解)converter属性:用于设置类型转换

3 常量配置

修改常量的值:   后修改的覆盖前修改的,习惯在struts.xml中修改

访问Action: <action name="">组成名字,<constant name="">组成后缀

Struts.xml中进行修改

  <!-- 配置struts常量的值 -->
   <constant name="struts.action.extension" value="abc"></constant> 

此时访问:http://localhost:8888//Struts_day01/hello.abc

strut.peoperties进行修改


web.xml进行修改  

 <!-- 配置Struts2的过滤器 -->
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  <!-- 修改常量 -->
  <init-param>
  <param-name>struts.action.extension</param-name>
  <param-value>xyz</param-value>
  </init-param>
  </filter>

4 include配置,分模块开发

在src目录下创建struts.xml,通过include标签引入配置文件,示例如下:

<?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常量的值 -->
  <constant name="struts.action.extension" value="action"></constant>
  <!-- 引入其他路径的配置文件,适合团队开发 -->
  <include file="com/itheima/Structs/demo1/struts_demo1.xml"></include>
  <include file="com/itheima/Structs/demo2/struts_demo2.xml"></include>
   <include file="com/itheima/Structs/demo3/struts_demo3.xml"></include>
</struts>

猜你喜欢

转载自www.cnblogs.com/ltfxy/p/9818456.html
今日推荐