Fork me on GitHub

一.Struts2——初始&&配置&&Action的创建方式

今天我们来看看Struts2框架,真是类,其实之前这个框架已经看过了,但是全部忘了,所以只能重新看,心累

Struts2框架的核心功能就是拦截器

Struts2概念:

    初始Struts2
image

    Struts2的使用优势

  • 自动封装参数
  • 参数校验
  • 结果的处理(转发|重定向)
  • 国际化
  • 显示等待页面
  • 表单的防止重复提交

    Struts2的使用优势

  1. struts2与struts1区别就是技术上没有什么关系.
  2. struts2的前身时webwork框架.

搭建Struts2框架:

    1.导包

image

注意:Struts2需要导入的包没有像hibernate那样明确标明出来,需要自己去找,然而在Struts2的压缩包里面的apps文件夹下有个struts2-blank.war包,表示人家的案例项目,并且这个项目是个空项目(blank嘛),将其放在tomcat的webapp目录下,运行tomcat,然后发现其被解压了,然后点进去,在WEB-INF下面找到lib包,里面就是我们需要导入的Struts2的包了。

    2.书写Action类

1
2
3
4
5
6
7
8
package yp.itcast.a_hello;

public class HelloAction {
public String hello(){
System.out.println("hello");
return "success";
}
}

    3.书写src/struts.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?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>

<package name="hello" namespace="/hello" extends="struts-default">
<action name="HelloAction" class="yp.itcast.a_hello.HelloAction" method="hello">
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
</package>
</struts>

    4.将Struts2的核心过滤器配置到web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2_day01</display-name>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

注意:在这里我们需要几下Struts2的核心过滤类org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

    5.测试

http://localhost:8080/struts2_day01/hello/HelloAction

Struts2框架访问流程&Struts2架构:

    1.访问流程

image

    2.Struts2架构

image

    3.Aop面向切面编程

image

Struts2配置详解:

    1.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
<?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>

<!--
package:将Action配置封装,就是可以在package中配置很多Action.
name属性:给包起个名字,起到标识符的作用,随便起,但是不能与其他包名重复
namespace属性:给Action的访问路径中定义一个命名空间
extends属性:继承一个指定包
abstract="true":包是否为抽象;标识性属性(给开发人员看的) ,标识该包不能独立运行,专门被继承
-->

<package name="hello" namespace="/hello" extends="struts-default">
<!--
action元素:配置action类
name属性:决定了Action访问资源名
class属性:Action类的完整类名(反射)
method属性:指定调用Action中的哪个方法来处理请求
-->
<action name="HelloAction" class="yp.itcast.a_hello.HelloAction" method="hello">
<!--
result元素:结果配置
name属性:标识结果处理的名称,与Action方法的返回值对应
type属性:指定调用哪一个result类来处理结果,默认使用转发
标签体:填写页面的路径
-->
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
</package>
<!-- 引入其他struts配置文件 -->
<include file="yp/itcast/b_dynamic/struts.xml"></include>
<include file="yp/itcast/c_default/struts.xml"></include>
</struts>

    2.Struts2常量配置

            Struts2默认常量配置位置

image

            修改Struts2常量配置(方式先后也是加载顺序)

方式①:src/strust.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
<?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>

<!-- i18n:国际化. 解决post提交乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>

<!-- 指定访问action时的后缀名 -->
<!-- <constant name="struts.action.extension" value="do"></constant> -->
<constant name="struts.action.extension" value="action,,"></constant>


<!-- 指定struts2是否以开发模式运行
1:热加载主配置。(不需要重启服务器)
2:提供更多的错误信息输出,方便开发时的调试。
-->
<constant name="struts.devMode" value="true"></constant>

<package name="hello" namespace="/hello" extends="struts-default">
<action name="HelloAction" class="yp.itcast.a_hello.HelloAction" method="hello">
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
</package>
<!-- 引入其他struts配置文件 -->
<include file="yp/itcast/b_dynamic/struts.xml"></include>
<include file="yp/itcast/c_default/struts.xml"></include>
</struts>

方式②:在src下创建struts.properties文件
方式③:在项目的web.xml文件中

1
2
3
4
5
<!-- 配置常量的第三种方式(会覆盖) -->
<context-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</context-param>

    3.Struts2进阶配置

            动态方法调用

方式①和②:

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
<?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.enable.DynamicMethodInvocation" value="true"></constant>
<package name="dynamic" namespace="/dynamic" extends="struts-default">

<action name="Demo1Action" class="yp.itcast.b_dynamic.Demo1Action">
<result name="success">/hello.jsp</result>
</action>


</package> -->

<!-- 动态方法调用第二种方法:通配符
使用{1} 取出第一个*号匹配的内容

-->
<package name="dynamic" namespace="/dynamic" extends="struts-default">

<action name="Demo1Action_*" class="yp.itcast.b_dynamic.Demo1Action" method="{1}">
<result name="add">/hello.jsp</result>
<result name="delete">/index.jsp</result>
</action>


</package>


</struts>

            Struts中默认配置

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
<?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>

<package name="default" namespace="/default" extends="struts-default">
<!-- 放在后面会报错
找不到包下的action,会使用Demo2Action作为默认action处理请求
-->
<default-action-ref name="Demo2Action"></default-action-ref>


<!-- method属性:execute(默认值) -->
<!-- result的name属性:success(默认值) -->
<!-- result的type属性:dispatcher转发(默认值)-->
<!-- class属性:com.opensymphony.xwork2.ActionSupport(默认值) -->
<action name="Demo2Action" method="execute">
<result>/hello.jsp</result>
</action>


</package>


</struts>

    Struts2中Action类的书写方式

方式①:

1
2
3
4
5
6
7
8
9
package yp.itcast.d_api;

//方式一:创建一个类,可以是POJO
//POJO:不用继承任何父类,也不需要实现任何接口
//使struts2框架的代码侵入性更低
public class Demo3Action {


}

方式②:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package yp.itcast.d_api;

import com.opensymphony.xwork2.Action;

//方式二:实现一个接口Action
// 里面有execute方法,提供action方法的规范
// Action接口预置了一些字符串,可以在返回结果时使用
public class Demo4Action implements Action {

public String execute() throws Exception {
// TODO Auto-generated method stub
return null;
}


}

方式③:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package yp.itcast.d_api;


import com.opensymphony.xwork2.ActionSupport;

//方式三:继承一个类,ActionSupport
//帮我们实现了Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable
//如果我们需要用到这些接口的实现时,不需要自己来实现了

public class Demo5Action extends ActionSupport{



}