Struts2概述
1、 struts2框架应用javaee三层结构中web层框架
2、 struts2框架在struts1和webwork基础之上发展全新的框架
3、 struts2解决问题:
4 、使用的struts2版本-struts2-2.3.24-all
5、 web层常见框架
(1)struts2
(2)springMVCStruts2框架入门
第一步 导入jar包(1)在lib中有jar包,不能把这些jar都导入到项目中
(2)到apps目录里面,找到示例程序,从示例程序复制jar包
解压war包:
复制jar包:
第二步 创建action
package com.struct_stu.action;
public class HelloAction {
/*
* (1)访问servlet时默认执行的是servlet的service(),
* 再根据请求执行doGet()与doPost(),底层使用的是反射
* -写类继承HttpServlet,重写类中方法
* -在web.xml中配置servlet访问路径
* (使用eclipse的话,就直接右键项目就创建了)
*
* (2)访问action,每次反问action时候,默认执行的是名称为execute()
* -配置action访问路劲
*
* */
public String execute(){
return "ok";
}
}
第三步 配置action类访问路径
(1)创建struts2核心配置文件
- 核心配置文件名称和位置是固定的
- 位置必须在src下面,名称 struts.xml,不能写错,且位子必须在src下
(2)引入dtd约束,dtd约束也是在war包下找到
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
(3)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>
<package name="hellodemo" extends="struts-default" namespace="/">
<!-- name写访问名称 -->
<action name="hello" class="com.struct_stu.action.HelloAction">
<!-- 配置方法不同返回值,让其跳转到不同页面 -->
<result name="ok">/hello.jsp</result>
</action>
</package>
</struts>
访问路径:http://127.0.0.1/struts2_day001_t/hello.action,报错404,是因为没有配置struts过滤器

第四步 配置struts2过滤器
<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.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" 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>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>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执行过程
画图分析过程

查看StrutsPrepareAndExecuteFilter源代码
1、 过滤器在服务器启动时候创建,创建过滤器时候执行init方法
(1)在init方法中主要加载配置文件
- 包含自己创建的配置文件和struts2自带配置文件
** struts.xml
**web.xmlStruts2的核心配置文件
1、 名称和位置固定的
2、 在配置文件中主要三个标签 package、action、result,标签里面的属性标签package
1、 类似于代码包,区别不同的action,要配置action,必须首先写package标签,在package里面才能 配置action
2 、package标签属性
(1)name属性
- name属性值根功能本身没有关系的,
- 在一个配置文件中可以写多个package标签,name属性值不能相同的
(2)extends属性
- 属性值固定的,struts-default
- 写了这个属性之后,在package里面配置的类具有action功能
(3)namespace属性
- namespace属性值和action标签里面的name属性值构成访问路径,与hello构成/hello的访问路径,可以不写,默认值为"/",但是建议要写

标签action
1 、action标签配置action访问路径
2、 action标签属性
(1)name属性
- namespace属性值和action标签里面的name属性值构成访问路径
- 在package标签里面写多个action标签,但是action的name属性值不能相同的
(2)class属性
- action全路径
(3)method属性
- 比如在action里面默认执行的方法execute方法,但是在action里面写其他的方法
- 让action里面多个方法执行,使用method进行配置标签result
1、 根据action的方法返回值,配置到不同的路径里面
2 、result标签属性:配置方法不同返回值,让其跳转到不同页面
(1)name属性
- 和方法返回值一样

(2)type属性
- 配置如何到路径中(转发或者重定向)
- type属性默认值 做转发操作
重定向与转发区别:
区别一:
重定向时浏览器上的网址改变
转发是浏览器上的网址不变
区别二:
重定向实际上产生了两次请求
转发只有一次请求
重定向:
发送请求 -->服务器运行-->响应请求,返回给浏览器一个新的地址与响应码-->浏览器根据响应码,判定该响应为重定向,自动发送一个新的请求给服务器,请求地址为之前返回的地址-->服务器运行-->响应请求给浏览器
转发:
发送请求 -->服务器运行-->进行请求的重新设置,例如通过request.setAttribute(name,value)-->根据转发的地址,获取该地址的网页-->响应请求给浏览器
区别三:
重定向时的网址可以是任何网址
转发的网址必须是本站点的网址
详解:
重定向:以前的request中存放的变量全部失效,并进入一个新的request作用域。
转发:以前的request中存放的变量不会失效,就像把两个页面拼到了一起。
Struts2常量配置
1、 struts2框架,帮我们实现一部分功能,struts2里面有常量,在常量里面封装一部分功能
2 、struts2默认的常量位置(记住)libraies>strut2-core-2.3.24.jar>default.peoperties


3、 修改struts2默认常量值
(1)常用的方式
- 在struts.xml中进行配置
<constant name="struts.i18n.encoding=UTF-8" value="UTF-8"></constant>
(2)还有两种方式(了解)
- 在src下面创建 struts.properties,进行修改
- 在web.xml进行配置
4 、最常用常量,可以解决post提交方式中的中文乱码问题
(1)表单提交数据到action里面,在action可以获取表单提交数据,
(2)表单提交数据有中文,有乱码问题,解决:
- post提交直接设置编码
- get提交做编码转换
(3)如果在action获取表单通过post方式提交中文,中文乱码问题帮解决了,不需要自己处理问题分模块开发(工作中的开发方式)
1 、单独写配置文件,把配置文件引入到核心配置文件中
例如在包下写hello.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>
<constant name="struts.i18n.encoding=UTF-8" value="UTF-8"></constant>
<package name="hellodemo" extends="struts-default" namespace="/">
<action name="hello" class="com.struct2_stu.action.HelloAction">
<result name="ok">/hello.jsp</result>
</action>
</package>
</struts>
把配置文件引入到核心配置文件中:
<!-- 引入hello.xml配置文件 (分模块开发)-->
<!-- 如果hello.xml也在src下,直接写hello.xml就行了 -->
<include file="com/struct2_stu.action/hello.xml"></include>
Action编写方式
1 action编写有三种方式
第一种 创建普通类,这个不继承任何类,不实现任何接口
实例:
package com.struct2_stu.action;
public class HelloAction {
public String execute(){
return "ok";
}
}
第二种 创建类,实现接口 Action
实例:
package com.struct2_stu.action;
import com.opensymphony.xwork2.Action;
public class UserAction implements Action {
//实现接口
@Override
public String execute() throws Exception {
//return "success";
return SUCCESS;
}
}
第三种 创建类,继承类 ActionSupport(一般使用这种方式创建Action)
package com.struct2_stu.action;
import com.opensymphony.xwork2.ActionSupport;
public class PersonAction extends ActionSupport{
public String execute(){
System.out.println("action...");
return ActionSupport.NONE;
}
}
ActionSupport实现Action接口,可以使用Action中的静态常量
访问action的方法(重点)
1、 有三种方式实现
第一种 使用action标签的method属性,在这个属性里面写执行的action的方法
第二种 使用通配符方式实现
第三种 动态访问实现(不用)
2 演示错误
(1)如果action方法有返回值,在配置文件中没有配置,出现错误(本来result为ok,将其注释)
<package name="hellodemo" extends="struts-default" namespace="/">
<action name="hello" class="com.struct2_stu.action.HelloAction">
<result name="ok">/hello.jsp</result>
</action>
</package>
出现错误:
(2)在action里面的方法有返回值,如果有返回值时候类型必须是String
(3)action里面的方法可以没有返回值,没有返回值时候,在result标签不需要配置
- 把方法写成void
- 让返回值,返回 ”none“(建议使用返回值为String,返回none)
package com.struct2_stu.action;
import com.opensymphony.xwork2.ActionSupport;
public class PersonAction extends ActionSupport{
public String execute(){
System.out.println("action...");
return ActionSupport.NONE;
}
}
使用action标签method属性
1 、创建action,创建多个方法package com.struct2_stu.method;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport{
//创建action,并创建多个方法
//添加
public String add(){
System.out.println("add.......");
return ActionSupport.NONE;
}
//修改
public String update(){
System.out.println("update........");
return ActionSupport.NONE;
}
}
2、 使用method配置
<?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>
<!-- 配置action的方法访问 -->
<package name="bookdemo" extends="struts-default" namespace="/">
<!-- method属性:写要执行的action中的方法名称 -->
<action name="addAction" class="com.struct2_stu.method.BookAction" method="add"></action> -->
<!-- 执行update方法 -->
<action name="updateAction" class="com.struct2_stu.method.BookAction" method="update"></action>
</package>
</struts>
缺陷:action每个方法都需要配置,如果action里面有多个方法,配置很多的action
使用通配符实现(重点)
1 、在action标签里面name属性,name属性值里面写 符号 * 星号
(1)* 理解: 表示匹配任意内容
- 比如访问hello,* 可以匹配到
- 比如访问add,* 可以匹配到
name属性值中写字母+*,直接写*也行,例如:
(1)执行action中的add(),访问book_add,路径使用book_*都可以匹配到,*相当于变成了add
(2)执行action中的update(),访问book_update,路径使用book_*都可以匹配到,*相当于变成了update
上面两个路径使用book_*都可以匹配到,method属性写*值,{1}表示第一个*值
<?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="methoddemo">
<!-- name属性值中写符号 *,直接写*也行
(1)执行action中的add(),访问book_add,路径使用book_*都可以匹配到,*相当于变成了add
(2)执行action中的update(),访问book_update,路径使用book_*都可以匹配到,*相当于变成了update
上面两个路径使用book_*都可以匹配到
method属性写*值,{1}表示第一个*值
-->
<action name="book_*" class="com.struct2_stu.method.BookAction" method="{1}"></action>
</package>
</struts>
*号通配符解释:
案例-客户列表

本章小结:
1 、struts2概述
(1)应用在web层
2、 struts2入门案例
3、 struts2底层执行过程
4 、struts2相关配置
(1)struts.xml配置
- package、action、result标签
(2)常量配置
(3)分模块开发
5、 struts2的action创建
(1)有三种
(2)一般使用继承类 ActionSupport实现
6 、struts2的action方法访问
(1)使用action标签method属性,method属性值写执行的方法名称
(2)使用通配符方式,使用*匹配任意内容,method里面写*值,写法固定 {1}