Struts2 配置说明及后缀与路径设置

Struts2 作为传统 WebWork 的继承者,融合 Struts1, 使得它成为高度成熟的框架。POJOOGNL 两大特性也使得它成为简单易用的框架,成为现在初学者的入手框架。其中 Struts 的配置文件 struts.xml 是领悟 Struts2 的核心。本文首先介绍 Struts2 的基本配置以免初学者感觉突兀,第二部分的路径部分为主要要讲解的内容 ( 本人实践多次的经验 )。

一、如何学习Struts2框架

1、入手

官网下载

所有我们所要学习的目标,我们都要找到其最原始的地方,通常都有其自己的官网。Struts2 官网地址:http://struts.apache.org/, 下载 zip 包到本地,解压后目录如下

目录介绍

  • apps为利用 Struts2 框架写的项目,后缀都是 war, 所以可以把他们放到 Tomcatwebapps 目录下直接运行。
  • docsStruts2 框架的文档教程。
  • libStruts2 框架的所有用到的 jar 包,有很多,但是我们的项目用不到那么多的。只需要核心的几个。
  • srcStruts2 框架的源码。

2、使用 Struts2 框架

使用框架实际上就是利用他们提供的依赖,使用他们的接口。所以我们只需将所用的框架的依赖添加到自己项目里,然后配置使用。

手动添加 jar 包

对于 Struts2 的依赖包都在 lib 文件夹里,但是 lib 下有很多包,当然不是每一个都加入项目。我们可以打开 apps 目录下的 struts2-blank.war 文件,直接解压缩得到文件夹,打开 WEB-INF\lib 目录,下面的包就是核心的几个需要包:

把他们复制到自己的 web 项目 WEB-INF/lib 目录下面就可以用了。

Maven 管理 jar 包

Maven 管理则很简单,只需要在 dependencies 里加入

1
2
3
4
5
<dependency>  
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.1.2</version>
</dependency>

3、Struts2 配置

web.xml 里配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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>
</web-app>

web 配置里加入了全局 action 拦截器。

struts.xml 配置

下面为我学习记录的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?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-login.xml,在所包括的配置里所有的struts的配置都字符都不能少的-->
<include file="struts-login.xml"/>
<!-- 默认包 没有命名空间 默认为-->
<!-- action名是url访问名-->
<!-- 访问地址http://127.0.0.1:8080/ProjectName/hello.action-->
<package name="default" extends="struts-default" namespace="/">
<action name="hello">
<result>/hello.jsp</result>
</action>
</package>
<!-- 包名userlogin,包名主要是为了分类,对url没有影响。命名空间是/user,它只对url有影响-->
<!-- action名是url访问名 class是操作aciton的类-->
<!-- 将包的命名空间和action名 合并作为url访问的最终地址如下-->
<!-- 访问地址http://127.0.0.1:8080/ProjectName/user/login.action-->
<package name="userlogin" extends="struts-default" namespace="/user">
<!-- 这是本包里的全局result,本包里的所有action都可以用该result-->
<global-results>
<result name="error">/execption.jsp</result>
</global-results>
<!--下面为默认页面,如果访问到该目录下没有的地址,则会显示该界面-->
<!--<action name="errordefault">-->
<!--<result>/error.jsp</result>-->
<!--</action>-->
<!--<default-action-ref name="errordefault" ></default-action-ref>-->
<!-- 下面这个没有指定方法名,默认调用的是UserLogin里的execute()方法-->
<action name="login" class="com.jiyiweb.action.UserLogin">
<!-- 这个默认访问execute()方法-->
<!-- http://127.0.0.1:8088/ProjectName/login.action-->
<result name="success">/success.jsp</result>
<!-- 下面这个就是为了动态方法调用-->
<!-- 访问地址http://127.0.0.1:8088/ProjectName/login!add.action-->
<result name="add">/add.jsp</result>
<!--<result name="error">/error.jsp</result>-->
</action>
<!-- 下面这个是方法名调用,在method等于UserLogin里的add()方法则将执行add()方法-->
<action name="loginadd" method="add" class="com.jiyiweb.action.UserLogin">
<result>/add.jsp</result>
<!--<result name="error">/error.jsp</result>-->
</action>
<!-- 下面为通配符访问方法,是官方推荐的方法-->
<!-- 调用url http://localhost:8088/user/logintpf_add.action-->
<!-- 注意:下划线后面的方法名,不写方法名默认调用的是execute方法-->
<action name="logintpf_*" method="{1}" class="com.jiyiweb.action.UserLogin">
<result name="add">/{1}.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
<!-- 常量配置,也可以配置在struts.properties文件里-->
<!--http://blog.csdn.net/flfna/article/details/4900172-->
<!-- 编码设置,请求参数中会出现编码问题,默认是UTF-8-->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 是否设置为开发模式,默认false,开发阶段设置为true-->
<constant name="struts.devMode" value="true"/>
<!-- 每次http请求到达时是否重新加载国际化资源,默认false,开发阶段设置为true-->
<constant name="struts.i18n.reload" value="true"/>
<!-- 请求后缀形如.action的都交由Struts2处理,值中多个值用逗号隔开-->
<constant name="struts.action.extension" value="action,do,struts2,"/>
<!-- 这个是动态方法调用,在action用感叹号访问action的方法-->
<!-- 主要是在访问的时候http://127.0.0.1:8088/ProjectName/login!add.action-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
</struts>

简单说明

1、DOCTYPE 是 xml 里的申明,使得本 xml 可以使用配置里的 dtd

2、 节点,如果项目太大,则应该将配置分写在不同的配置文件里,可以用 include 包括另外一个配置文件,另一文件的配置也需要DOCTYPE。

3、 节点,用于分类,name 为包名,这个自己起,不重名就行,extends 通常继承自 struts-default, namespace 为访问路径。

4、 节点,package 节点里配置若干个action,通常一个action 代表一个映射地址,也就是一个 url 地址,name 是 url 访问的地址,与 package 里的 namespace 一起构成 url 访问的最终地址: 如: http://127.0.0.1:8088/mtest/PackageNameSpace/ActionName

5、 就是结果返回的页面,name 应该对应于 action 类里的返回值 ,没有设置则是默认结果,形式为: /hello.jsp

6、 是包下的全局返回结果,与 action 并列,可以在任意一个 action 使用

二、web访问路径(重点)

1. 路径配置说明

关于 web 路径配置的地方有三处:

  1. web.xml 里的 <welcome-file-list>, 这个是所有 java web 都有的。
  2. web.xml 里的 struts 拦截器设置的 <filter-mapping> 匹配配置。
  3. struts.xml 里的常量 constant name="struts.action.extension

下面分别说明:

2. welcom-file-list

对于 web.xml 里的这个标签主要配置的是 web 项目的主页面。也就是当我们访问一个项目的根目录时所显示的界面。

举个例子:我们的项目名叫 hellotest, 我们配置 tomcat 时会有两种配置方法:

一种是将 tomcat 的 webapps 作为服务器根目录,这时我们的访问我们项目的根目录为http://localhost:8080/hellotest,

另一种方法是将我们的项目就作为 tomcat 服务器根目录了,那我们访问我们项目的根目录为 http://localhost:8080。通常我们用后一种tomcat 配置方法,因为这样可以与实际运行情况相符。那么不管哪一种配置方法,访问了我们项目的根目录,我们都没有指定访问哪个页面为什么还是会有界面显示呢?因为 tomcat 默认将项目的 index.html, index.htm, index.jsp 作为访问项目根目录而显示的界面。与在web.xml 里配置如下这种情况一样:

1
2
3
4
5
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

这个是写在 web.xml 里的,上面的代码与默认配置是一样的,也就是说不写上面的这些代码也会有效果的。那它有什么用呢?它通常做为用户想自定义自己项目根目录访问界面的,比如我的项目下有 index.jsphello.jsp, 而我就是想让 hello.jsp 作为默认界面,那我们就这么配置:

1
2
3
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>

当我们访问项目根目录,比如: http://localhost:8080 时,显示的界面将会是 hello.jsp 的界面,而不会是 index.jsp. 因为当我们配置了该标签,就覆盖了默认的设置了,其所隐含的真正路径是: http://localhost:8080/hello.jsp

3.web.xml 里 struts 拦截器设置

通常的配置:

1
2
3
4
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

这里的意思是以 action 为后缀的访问路径和没有后缀的访问路径都会被 struts 的拦截器拦截,进入 action 里处理后再返回出视图,是的,没错,没有后缀的也会拦截的,struts 默认拦截的有两个的,一个是 action 后缀,一个就是没有后缀的。举个例子:

  • 当访问url为 http://localhost:8080/add.action 这个为以action结尾的访问路径,将会进入继承actionsupport的类里处理,然后返回出视图。
  • 还有这样 http://localhost:8080/index 这个是没有后缀的访问路径,也会进入struts的拦截器里。
  • http://localhost:8080/index.jsp 以及 http://localhost:8080/hello.html 等都不会进入struts的拦截器的,直接由tomcat返回页面视图的。

我们举个更实际点的例子吧:

  • 假设我们的项目根目录下有 index.jsp 和 hello.jsp,所有其他配置好后都默认,也就是说 Struts 拦截 action 后缀和无后缀的 url 路径。
  • 在 struts.xml 里配置如下的包:
1
2
3
4
5
<package name="default" extends="struts-default" namespace="/">
<action name="index">
<result>hello.jsp</result>
</action>
</package>

当我们

(1) 访问http://localhost:8080返回index.jsp视图

(2) 访问 http://localhost:8080/index.jsp返回index.jsp视图

(3) 访问 http://localhost:8080/hello.jsp返回hello.jsp视图

(4) 访问 http://localhost:8080/index 返回hello.jsp

(5) 访问 http://localhost:8080/index.action 返回hello.jsp

(1)、(2)、(3)访问机制是一样的,是直接由tomcat直接返回视图;(4)和(5)的机制相同但是与前面的不同,(4)、(5)是被struts拦截后通过配置文件的配置后返回视图的。

4. struts.xml 里的常量配置

其配置示例:

1
<constant name="struts.action.extension" value="action,do,"/>

它的作用是让 struts 拦截哪些后缀结尾的 url 的。

当我们新建好项目时,struts.xml 里没有任何东西的,所以默认的拦截是 action没有后缀 的 url 路径。但是

若按如下配置:

1
<constant name="struts.action.extension" value="action"/>

那么它将只会拦截 action 结尾的 url 路径了。

当我们写下:

1
<constant name="struts.action.extension" value=","/>

那么它将只会拦截 没有后缀 的 url 路径了。

当我们写下:

1
<constant name="struts.action.extension" value="action,do,"/>

那么它将会拦截 action, do 结尾的 url 以及没有后缀 的 url 路径。

当写下:

1
<constant name="struts.action.extension" value="action,do"/>

那么它将只会拦截 actiondo 结尾的 url 路径了,没有后缀的将不会拦截。

Struts 中如果我们没有配置拦截后缀,则默认是所有的 .action 后缀的访问 url 将被 Struts 框架拦截,然后通过 action 返回视图。而所有的 .jsp.html 将直接通过 url 是可访问的。

三、实践理解路径控制—-设置主页

方法有三种:

  1. 什么都不用做,只要项目目录有 index.html 或者 index.htm 或者 index.jsp,那么这三中页面就会默认被配置为根目录下主页。

  2. 配置 web.xml 里的 welcome-file-list, 想让哪个界面作为主页都可以。配置后会覆盖默认 tomcat 的默认首页设置。

  3. 通过 struts.xml 里的配置来设置主页。

这里简单介绍下:

拦截:因为要用 struts 来配置,所以要使 url 进入 action, 也就是说要拦截 tomcat 默认设置的 .html, .htm, .jsp 后缀的访问 url, 所以要将控制后缀常量设置为:

1
<constant name="struts.action.extension" value="action,html,htm,jsp,"/>

这样我们就拦截了主页默认的 index.html 或者 index.jsp 后缀 index.htm.

配置:然后配置 package 如下:

1
2
3
4
5
6
<package name="default" extends="struts-default" namespace="/">
<default-action-ref name="myhome"/>
<action name="myhome">
<result>/index.jsp</result>
</action>
</package>

这里的 default-action-ref 是包的默认 action,也就是说当拦截了的路径匹配到了包后没有匹配到action则此默认的action就作为视图返回的路径了。

访问思考:所以当我们访问 http://localhost:8080 时,经过tomcat的默认配置,隐含的实际访问路径为:http://localhost:8080/index.jsp 但是此时的 jsp 后缀 url 都会被 struts 拦截,所以就进 struts.xml 里的路径映射,因为只有一级路径所以映射到 default 包,而这个包里没有配置 index.jsp 名称,所以进入本包的默认 action, 则返回视图 /index.jsp。所以最后显示了主页 index.jsp。

四、总结

做一个web网站,最重要有三个:一是前端,二是后台,而第三个就是逻辑控制了;这个逻辑控制比较大,它包括了前后台的数据传输,用户与前端、后台的交互(如:资源访问)等。而最不好控制的就是暴露给用户的路径逻辑了,因为你不知道用户在浏览器里输入什么路径的,所以很多情况下我们都要在后台严格控制资源的访问权限,这些控制并不是可以通过写代码那么好实现的,通常要有一定的框架思维和全局观念,这也就是我们通常用框架的原因,解决逻辑控制,尤其是路径控制访问!

  • 通常 web.xml 中的 welcome-list 无须设置,因为我们项目几乎都应该含有index的页面的。
  • 如果我们设置了 jsp 后缀的也拦截的话,一定记住要对每个 jsp 界面都应该设置对应的 action。
  • 通常我们会在 html 里引用其他的页面,比如超链接等,这时要写的链接大家一定要注意是不是被你拦截的页面,如果拦截则一定要按照 PackageName/ActionName 这样配置。
赞赏一下
0%