struts2框架快速入门
1.struts2框架概述:
struts2是一个基于MVC模式的轻量级的web层开源框架,它是WebWork的更新产品
2.struts2快速入门:
步骤一: 引入struts2依赖
* 下载地址: https://struts.apache.org/
* 解压apps/struts2-blank.war/WEB-INF/lib下的所有jar包
* 将依赖导入到web工程
步骤二: 编写Action,并配置struts2.xml
步骤三: 配置核心过滤器,StrutsPrepareAndExecuteFilter
3.struts2执行流程:
当浏览器向服务器发送一次请求,被服务器的核心过滤器拦截下来,解析请求url,并匹配struts2.xml中的action标签的name属性;匹配成功后,获得action标签的class属性值,通过反射Class.forName得到Class对象并调用action标签的method属性所指定的方法(默认执行execute方法);方法执行完毕后,获得方法的返回值(如果返回值是none,表示异步请求,不做页面跳转),匹配result标签的name属性,得到跳转url,并相应浏览器
4.struts.xml的配置
* 配置package标签
1.配置action时候,首先必须写package标签
2.在package标签里面有name属性,表示名称,理论上可以随便写,不重复即可
3.在package里面有extends属性,属性值固定的struts-default,
表示package里面配置的类,具有action功能
4.在package里面有namespace属性,由namespace的值和action标签的name属性值构成访问路径,多级访问路径/user/add,namespace属性值多级访问路径末尾不能带上"/"
5.在一个配置文件中可以有多个package标签
* 配置action标签
1.在action标签class属性配置action的访问路径
2.action标签必须写在package标签里面
3.在一个package里面可以写多个action标签,name属性不重复即可(如果重复了,下面的action会覆盖上面的),name属性中不能有"/"
4.在action标签class属性action类全限定类名
5.在一个action类里面可能有很多的方法,默认执行execute方法,执行其他的方法,使用action标签里面的属性 method配置
* 配置result标签
1.在一个action标签里面写多个result标签,result标签的name属性值不能相同的
2.result标签里面有name属性,匹配action类中的方法的返回值
5.struts2的常量配置
struts2默认的常量配置struts2-core-2.3.24.jar!\org\apache\struts2\default.properties,如果配置了相同的常量,后加载的常量配置会覆盖先加载的
5.1修改默认常量配置:
* 方式一: 在struts.xml中进行配置常量
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
* 方式二: 在src目录下创建struts.properties,以key=value的形式配置常量
struts.i18n.encoding=UTF-8
* 方式三: 在web.xml的核心过滤器中配置初始化参数
<init-param>
......
</init-param>
6.struts2分模块开发
<include file="com/baidu/action/User.xml"></include>
7.struts2编写Action类的三种方式:
* 方式一: 创建普通类,不继承任何类,不实现任何接口
* 方式二: 创建类实现Action接口,com.opensymphony.xwork2.Action
* 方式三: 创建类继承ActionSupport类,com.opensymphony.xwork2.ActionSupport(推荐)
8.struts.xml中action标签method属性的访问配置
* 方式一: 为同一个类定义多个action,eg: name="action_add",name="action_save"
* 方式二: 使用通配符来完成action访问(推荐)
<action name="customer_*" class="com.baidu.action.CustomerAction" method="{1}">
</action>
* 方式三: 用"!"动态拼接方法名
(1)开启动态访问:<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
(2)action标签不指定method属性,也不用通配符
<action name="customer" class="com.baidu.action.CustomerAction">
</action>
(3)用"!"动态拼接方法名,请求url: http://localhost:8080/customer!add.action
9.获取HttpServletRequest
HttpServletRequest request = ServletActionContext.getRequest();
struts2核心配置文件struts.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" value="UTF-8"></constant>
<package name="demo" extends="struts-default" namespace="/">
<action name="customer_*" class="com.baidu.action.CustomerAction" method="{1}">
<result name="list">/jsp/customer/list.jsp</result>
<result name="toAddPage">/jsp/customer/add.jsp</result>
<result name="add">/jsp/success.jsp</result>
</action>
</package>
</struts>
核心过滤器web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<filter>
<filter-name>strut2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>strut2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>