1、Struts2执行流程
**流程:**当用户访问某一个Action的时候,需要先经过核心过滤器,在核心过滤器中执行一组拦截器(这组拦截器实现部分功能),然后执行目标Action,根据Action的返回值,回到Struts2.xml配置文件进行页面跳转。
2、Struts2配置过程
-
下载Struts2的开发环境
http://struts.apache.org/ -
解压Struts2开发包
apps :Struts2提供的应用,war文件:web项目打成war包。直接放入到tomcat可以允许。
docs :Struts2的开发文档和API
lib :Strtus2框架的开发的jar包
src :Struts2的源码 -
创建web项目,引入jar包
-
配置XML的提示
-
创建一个JSP页面
<body>
<h1>Struts 入门</h1>
<a href="${ pageContext.request.contextPath }/hello.action">跳转到Demo1的action</a>
</body>
- 编写Action的类
public class Struts2_Demo1 {
<!--默认执行excuse方法-->
public String execute(){
System.out.println("ִ执行了demo1的action");
return "success";
}
/**
* Action的编写方式二:实现一个Action的接口
* * 实现接口的这种方式:提供了五个常量(五个逻辑视图的名称)
* * SUCCESS :成功
* * ERROR :失败
* * LOGIN :登录出错页面跳转
* * INPUT :表单校验的时候出错
* * NONE :不跳转
* @author jt
*
*/
- 对Action进行配置,在src下创建(提供)名称叫做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>
<!--
package标签称为包,这个包与Java中的包的概念不一致。包为了更好管理action的配置。
package标签的属性
name :包的名称,只有在一个项目中不重名即可。
extends :继承哪个包,通常值为struts-default。
namespace :名称空间,与<action>标签中的name属性共同决定访问路径。
名称空间有三种写法:
带名称的名称空间 :namespace=”/aaa”
跟名称空间 :namespance=”/”
默认名称空间 :namespace=””
abstract :抽象的,用于其他包的继承。
-->
<package name="demo1" extends="struts-default" namespace="/">
<!--
action标签配置Action类。
action标签的属性
name :与namespace共同决定访问路径
class :Action类的全路径
method :执行Action中的哪个方法的方法名,默认值execute
converter :用于设置类型转换器
-->
<action name="hello" class="com.excesice.struts2.Struts2_Demo1">
<result name="success">/demo1/success.jsp</result>
</action>
</package>
<!-- 配置Struts2的常量 -->
<constant name="struts.action.extension" value="action"/>
<!--include的配置-->
<include file="com/itheima/struts/demo1/struts_demo1.xml"/>
</struts>
- 配置核心过滤器(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_exce1</display-name>
<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>
<filter>
<filter-name>struts2_demo1</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2_demo1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- 编写success.jsp
<body>
执行了返回函数!
</body>
3、Action的访问
有三种访问方式
<!-- 开启动态方法访问 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<!-- Struts2为了管理Action的配置,通过包进行管理。 -->
<!-- 配置Struts2的包 ================ -->
<package name="demo3" extends="struts-default" namespace="/">
<action name="userFind" class="com.itheima.struts.demo3.UserAction" method="find"></action>
<action name="userUpdate" class="com.itheima.struts.demo3.UserAction" method="update"></action>
<action name="userDelete" class="com.itheima.struts.demo3.UserAction" method="delete"></action>
<action name="userSave" class="com.itheima.struts.demo3.UserAction" method="save"></action>
<!-- 通配符的方式 -->
<action name="product_*" class="com.itheima.struts.demo3.ProductAction" method="{1}"></action>
<!-- 动态方法访问的方式 -->
<action name="customer" class="com.itheima.struts.demo3.CustomerAction"></action>
</package>
访问路径
<h3>通过method方式</h3>
<a href="${ pageContext.request.contextPath }/userFind.action">查询用户</a><br/>
<a href="${ pageContext.request.contextPath }/userUpdate.action">修改用户</a><br/>
<a href="${ pageContext.request.contextPath }/userDelete.action">删除用户</a><br/>
<a href="${ pageContext.request.contextPath }/userSave.action">保存用户</a><br/>
<h3>通过通配符的方式</h3>
<a href="${ pageContext.request.contextPath }/product_find.action">查询商品</a><br/>
<a href="${ pageContext.request.contextPath }/product_update.action">修改商品</a><br/>
<a href="${ pageContext.request.contextPath }/product_delete.action">删除商品</a><br/>
<a href="${ pageContext.request.contextPath }/product_save.action">保存商品</a><br/>
<h3>通过动态方法访问的方式</h3>
<a href="${ pageContext.request.contextPath }/customer!find.action">查询客户</a><br/>
<a href="${ pageContext.request.contextPath }/customer!update.action">修改客户</a><br/>
<a href="${ pageContext.request.contextPath }/customer!delete.action">删除客户</a><br/>
<a href="${ pageContext.request.contextPath }/customer!save.action">保存客户</a><br/>