本系列是本人在学习《Struts2实战》一书时的学习笔记,并结合当前Struts2的最新版本(2.3.16.2)做的一些更新和修正。如果有错漏之处,还请各位大侠多多指正。
一、web.xml (注意filter-class改用新的类)
位置:/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_1" version="3.0">
<display-name>hello-annotated</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
</web-app>
二、struts.xml
位置:/WEB-INF/classes/struts.xml(在eclipse中可以放在java的源(src)目录下)
1. 配置文件统一配置Action
2. 零配置,约定大于配置,Java注解大于配置
(0)一个Action类,如果使用了注解进行Action配置,则xml文件中关于此Action的配置将被全部忽略。
(1)加入依赖包:struts2-convention-plugin-2.3.16.2.jar
(2)关于Action搜索根包的约定:struts, struts2, action , actions
(3)关于Action类的约定:任何在搜索根包及其子包之内的,符合以下条件之一的类:
a) 类名以Action结尾的类
b) 实现com.opensymphony.xwork2.Action接口的类,或继承com.opensymphony.xwork2.ActionSupport的类
(4)URL命名规则(包括Action,result和命名空间):所有大写字母前加"-",并全部转为小写,例如:MyCar -> my-car
(5)关于命名空间的约定:从搜索根包开始(不含根包)到包结束
(6)关于Action的URL命名规则:除执行“URL命名规则”之外,(如果有)去掉未尾的Action,例如:MyBookAction -> my-book
(7)关于结果页面的约定:默认的结果页面根目录:/WEB-INF/content ,默认的result映射路径是:结果页面根目录/命名空间/Action名-结果字符串.扩展名。例如:Action类:notebook.action.a.MyBookAction的success结果的默认映射路径是:/WEB-INF/content/a/my-book-success.jsp
(8)可以通过配置常量的方式改变约定(详见附表):
(struts.convention.action.packages),指定Action搜索根包,多个以逗号分隔:
<?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>
<constant name="struts.devMode" value="true" />
<constant name="struts.convention.action.packages" value="manning" />
</struts>
三、配置查询(浏览)插件:
1. 加入依赖包:struts2-config-browser-plugin-2.3.16.2.jar
2. 打开http://localhost:8080/CONTEXT/config-browser/index.action,即可查看Struts的action配置情况。
附表:convention插件的常量:
Name | Default Value | Description |
---|---|---|
struts.convention. action.alwaysMapExecute | true | 如果设置为false,可以防止当在类中存在其他方法被标注为action时,创建默认映射到“execute” |
struts.convention. action.includeJars |
| 要扫描的jar的URL的正则表达式列表,用逗号分隔。例如:".myJar-0\.2.,.thirdparty-0\.1." |
struts.convention. action.packages |
| An optional list of action packages that this should create configuration for (they don't need to match a locator pattern) 将要创建配置的action包的可选列表,(他们并不需要匹配一个定位嚣模式) 可选的搜索根包列表,用逗号分隔。 |
struts.convention. result.path | /WEB-INF/content/ | 存放页面模板(*.jsp等)的根目录 |
struts.convention. result.flatLayout | true | 如果设置为false,结果可以被放在它自己的目录:结果根目录/命名空间/action名/结果页面.扩展名 |
struts.convention. action.suffix | Action | 用于查找action类的类名后缀 |
struts.convention. action.disableScanning | false | 禁止扫描action类 |
struts.convention. action.mapAllMatches | false | 创建action映射,即使没有发现@Action |
struts.convention. action.checkImplementsAction | true | 检查实现com.opensymphony.xwork2.Action的action类是否创建一个action映射 |
struts.convention. default.parent.package | convention-default | action映射的默认父包 |
struts.convention. action.name.lowercase | true | 将action名转换为小写 |
struts.convention. action.name.separator | - | 分隔符,用于构建动作名称,MyAction - >my-action。该字符也用作模板的action名和结果之间的分隔符,例如:action-result.jsp |
struts.convention. package.locators | action,actions, struts,struts2 | 以这些字符串之一结尾的包将被扫描是否存在action |
struts.convention. package.locators.disable | false | 禁止扫描基于locators的包 |
struts.convention. exclude.packages | org.apache.struts.*, web.struts.*, web.struts2.*, | 在action扫描时,被排除的包。如果一个包被排除,则不可能以任何方式包含到扫描列表中。 |
struts.convention. package.locators.basePackage |
| 如果设置,只有此值开始的包才会被扫描 |
struts.convention. relative.result.types | dispatcher, velocity, freemarker | The list of result types that can have locations that are relative and the result location (which is the resultPath plus the namespace) prepended to them 结果类型的列表中,可以有一个是相对位置和结果的位置(这是resultPath加上命名空间)预先考虑到它们 |
struts.convention. redirect.to.slash | true | 布尔型参数,控制是否应该以Apache, Tomcat 和其他web服务器同样的方式来处理未知的action。这个处理会发回一个重定向的URL,如 /foo -> /foo/,如果不存在一个action响应/foo |
struts.convention. classLoader.excludeParent | true | 排除由父类加载器从扫描以查找action的URL列表中找到的URL(JBoss5需要设置为false) |
struts.convention. action.eagerLoading | false | 如果设置,发现的action类将通过的ObjectFactory实例化,以加速未来使用,设置它可能与Spring管理的bean发生冲突 |