1.struts2框架简介
处理访问服务器的请求,替代了servlet的存在
优点:自动封装参数、参数校验、结果的处理(转发|重定向)、国际化、显示等待页面、表单防止重复提交
struts2的思想:基于拦截器Filter的框架,实现了aop思想(切面编程)
struts2与struts1没有任何联系,struts2的前身是webwork框架
2.struts2框架的搭建
1>导包
apps/struts2-blank/WEB-INF/lib下的所有包
2>书写action类
public class HlloAction {
public String hello(){
System.out.println("第一次运行struts2");
return "success";
}
}
3>书写sturts.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>
<!-- package:将Action配置封装.就是可以在Package中配置很多action.
name属性: 给包起个名字,起到标识作用.随便起.不能其他包名重复.
namespace属性:给action的访问路径中定义一个命名空间
extends属性: 继承一个 指定包
abstract属性:包是否为抽象的; 标识性属性.标识该包不能独立运行.专门被继承
-->
<package name="hello" namespace="/hello" extends="struts-default">
<!-- action元素:配置action类
name属性: 决定了Action访问资源名.
class属性: action的完整类名
method属性: 指定调用Action中的哪个方法来处理请求
-->
<action name="helloAction" class="com.imwj.action.HlloAction" method="hello">
<!-- result元素:结果配置
name属性: 标识结果处理的名称.与action方法的返回值对应.
type属性: 指定调用哪一个result类来处理结果,默认使用转发.
标签体:填写页面的相对路径
-->
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
4>web.xml中配置struts2的核心过滤器
<!-- 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>
5>测试使用
http://localhost:8080/WEB39_struts2框架的搭建/hello/helloAction
3.struts的访问流程|架构
4.配置详解
1>struts.xml的配置
2>struts2的常量配置
struts2的配置文件加载顺序:struts.xml > struts.properties > web.xml
struts2的默认常量配置文件位置:struts2-core/org.apache.struts2/default.properties
修改struts2的常量配置(键值对形式):在struts.xml中(重点、用constant标签)、src下创建struts.properties、在web.xml中(context-param)
<!-- i18n:国际化. 解决post提交乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
常量配置:
<!-- i18n:国际化. 解决post提交乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 指定反问action时的后缀名-->
<constant name="struts.action.extension" value="action"></constant>
<!-- 指定struts2是否以开发模式运行1.热加载主配置.(不需要重启即可生效)2.提供更多错误信息输出,方便开发时的调试-->
<constant name="struts.devMode" value="true"></constant>
3>struts2的进阶配置:动态方法调用
可以动态的去调用方法,在struts.xml中的action标签中配置
<!-- 动态方法调用方式2:通配符方式 使用{1} 取出第一个星号通配的内容 -->
<action name="Demo1Action_*" class="co.imwj.dynamic.Demo1Action" method="{1}" >
4>struts中的一些默认配置
<!-- 找不到包下的action,会使用Demo2Action作为默认action处理请求 -->
<default-action-ref name="Demo2Action"></default-action-ref>
<!-- method属性:execute -->
<!-- result的name属性:success -->
<!-- result的type属性:dispatcher 转发 -->
<!-- class属性:com.opensymphony.xwork2.ActionSupport -->
5.action类详解
三种方式:pojo(一个普通类不实现、不继承其他类或接口)、实现action接口、继承ActionSupport(常用)
继承ActionSupport(常用):
帮我们实现了 Validateable, ValidationAware, TextProvider, LocaleProvider 这些类或者接口(功能更强大)
public class Demo1Action extends ActionSupport{
总结:
1.什么是struts2
1>运行在web层.负责处理请求的.
2>struts2已经帮我们封装了很多web中常用的功能(拦截器)
3>struts2 与 struts1 没什么关系. struts2是基于webwork框架
2.struts2框架搭建
1> 导包
|- struts2/apps/struts-blank.war/WEB-INF/lib
2> 书写Action
3> 书写 主配置文件 src/struts.xml
4> 在web.xml 配置 过滤器
|- strutsprepareAndExecuteFilter
3.struts2 架构
4.配置详解
struts2 核心配置
<struts>
<package name namespace extens >
<action name class method >
<result name type >
struts2 常量配置
解决乱码
后缀名配置
开发模式
开启!动态方法调用
动态方法调用
XXXAction_* => {1}
5.Action创建
1> POJO 普通java类.不需要继承,不需要实现
2> 实现Action接口.
3> 继承ActionSupport.
Action方法
public String xx() throws Exception{}