1.2 相关知识点
1.2.1 Struts2的概述
1.2.1.1 什么是Struts2:
Struts2是一个基于MVC设计模式的WEB层的框架。
1.2.1.2 常见web层框架
l Struts1
l Struts2
l WebWork
l SpringMVC
1.2.1.3 Struts2的执行流程图(前端控制器模式)
1.2.2 Struts2的入门
1.2.2.1 下载Struts2的开发包
l apps :Struts2提供一些项目。
l docs :Struts2提供的开发文档
l lib :Struts2提供的开发jar包
l src :Struts2提供的源代码
1.2.2.2 创建web工程,引入jar包
导入struts-2.3.24-all\struts-2.3.24\apps\struts2-blank\WEB-INF\lib全部jar包
1.2.2.3 创建一 个JSP页面
1.2.2.4 配置Struts2的核心过滤器(前端控制器)
1.2.2.5 编写Action的类
1.2.2.6 配置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包
* name:包的名称
* extends:继承其他的包
* namespace:名称空间
-->
<package name="demo1" extends="struts-default" namespace="/">
<!--
配置Action
* name:访问路径
* class:Action类的全路径
-->
<action name="hello" class="com.itheima.struts2.demo1.HelloAction"></action>
</package>
</struts>
1.2.2.7 修改Action
1.2.2.8 修改配置文件
1.2.3 Struts2的执行流程
有一次请求,先经过前端控制器(核心过滤器:StrutsPrepareAndExecuteFilter),过滤器内部执行一组拦截器,这组拦截器实现部分的功能,执行目标Action,根据返回值跳转不同的页面。
1.2.4 Struts2的配置文件编写(先介绍所有的配置文件和加载顺序)
1.2.4.1 Struts2的配置文件加载顺序(了解)
init_DefaultProperties(); // [1]---第一步加载default.properties
init_TraditionalXmlConfigurations(); // [2]---第二步加载struts-default.xml,struts-plugin.xml,struts.xml
init_LegacyStrutsProperties(); // [3]---第三步加载struts.properties
init_CustomConfigurationProviders(); // [5]---第四步加载客户一些配置提供类
init_FilterInitParameters() ; // [6]---第五步加载web.xml中配置核心过滤器参数
init_AliasStandardObjects() ; // [7]---第六步加载标准对象
配置文件的加载顺序:
default.properties
struts-default.xml
struts.plugin.xml
struts.xml
struts.properties
web.xml
1.2.4.2 package配置:包,为了更好管理action
l <package>标签
n name :包的名称,在配置文件中不要出现重名的包即可。
n extends :用于继承其他的包,通常继承struts-default.
n namespace :名称空间,与<action>标签中name一起决定访问路径的。
u namespace=”” :默认名称空间
u namespace=”/” :跟名称空间
u namespace=”/aaa” :带有名称名称空间
n abstract :抽象的,将这个值设置为true,用于其他包继承。
l <action>标签
n name :与namespace共同决定访问路径
n class :Action类全路径
n method :执行Action中的哪个方法,默认值是execute.
1.2.4.3 constant配置:配置struts2中的常量的
struts2中常量可以在以下三个位置进行修改:
l struts.xml(推荐)
<!-- 配置常量 -->
<constant name="struts.action.extension" value="abc"/>
l struts.properties
key=value
l web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.action.extension</param-name>
<param-value>xyz</param-value>
</init-param>
</filter>
*****后加载配置文件中的常量的值会将先加载配置文件中常量的值覆盖!!!
1.2.4.4 include配置:包含其他的配置文件
1.2.5 Struts2的Action的访问
1.2.5.1 Struts2的Action的编写规则
Action的类可以有三种编写方式:
l Action类是一个POJO的类。
/**
* Action的编写方式一:Action是POJO的类
* @author jt
*
*/
public class Struts2Demo1 {
public String execute(){
System.out.println("Struts2Demo1的方法执行了...");
return null;
}
}
l Action类可以实现一个Action接口。
/**
* Action的编写方式二:Action实现Action的接口
* @author jt
*
*/
public class Struts2Demo2 implements Action{
@Override
/**
* Action的接口中提供了五个逻辑视图的常量的名称:
* * SUCCESS:成功
* * NONE:不跳转
* * LOGIN:用于用户登录失败页面跳转
* * INPUT:表单出错情况下,页面跳转
* * ERROR:错误页面
*/
public String execute() throws Exception {
System.out.println("Struts2Demo2的方法执行了...");
return NONE;
}
}
l Action类可以基本一个ActionSupport类。(推荐)
/**
* Action编写的方式三:Action继承一个ActionSupport类
* @author jt
*
*/
public class Struts2Demo3 extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("Struts2Demo3的方法执行了...");
return NONE;
}
}
1.2.5.2 Struts2的Action的访问
一个请求对应了一个Action类,那么这样就会导致Action类编写过多,配置也会过多,需要将一个模块的多次请求只使用一个Action进行处理。
l 通过method参数设置:(不推荐:配置过多)
页面:
<h1>客户管理</h1>
<h4><a href="${ pageContext.request.contextPath }/findCustomer.action">查询客户</a></h4>
<h4><a href="${ pageContext.request.contextPath }/saveCustomer.action">保存客户</a></h4>
<h4><a href="${ pageContext.request.contextPath }/updateCustomer.action">修改客户</a></h4>
<h4><a href="${ pageContext.request.contextPath }/deleteCustomer.action">删除客户</a></h4>
编写Action
/**
* 客户管理的Action的类
* @author jt
*
*/
public class CustomerAction extends ActionSupport{
public String save(){
System.out.println("保存客户...");
return NONE;
}
public String find(){
System.out.println("查询客户...");
return NONE;
}
public String update(){
System.out.println("修改客户...");
return NONE;
}
public String delete(){
System.out.println("删除客户...");
return NONE;
}
}
配置Action
<package name="demo3" extends="struts-default" namespace="/">
<action name="findCustomer" class="com.itheima.struts2.demo3.CustomerAction" method="find"/>
<action name="saveCustomer" class="com.itheima.struts2.demo3.CustomerAction" method="save"/>
<action name="updateCustomer" class="com.itheima.struts2.demo3.CustomerAction" method="update"/>
<action name="deleteCustomer" class="com.itheima.struts2.demo3.CustomerAction" method="delete"/>
</package>、
l 通配符的配置:(前提,访问路径必须与方法名有一定联系)
页面:
<h1>联系人管理</h1>
<h4><a href="${ pageContext.request.contextPath }/linkMan_find.action">查询联系人</a></h4>
<h4><a href="${ pageContext.request.contextPath }/linkMan_save.action">保存联系人</a></h4>
<h4><a href="${ pageContext.request.contextPath }/linkMan_update.action">修改联系人</a></h4>
<h4><a href="${ pageContext.request.contextPath }/linkMan_delete.action">删除联系人</a></h4>
编写Action
public class LinkManAction extends ActionSupport{
public String save(){
System.out.println("保存联系人...");
return NONE;
}
public String find(){
System.out.println("查询联系人...");
return NONE;
}
public String update(){
System.out.println("修改联系人...");
return NONE;
}
public String delete(){
System.out.println("删除联系人...");
return NONE;
}
}
配置Action
<!-- 配置联系人的Action -->
<action name="linkMan_*" class="com.itheima.struts2.demo3.LinkManAction" method="{1}"/>
l 动态方法访问的方式
编写Action
public class ProductAction extends ActionSupport{
public String save(){
System.out.println("保存商品...");
return NONE;
}
public String find(){
System.out.println("查询商品...");
return NONE;
}
public String update(){
System.out.println("修改商品...");
return NONE;
}
public String delete(){
System.out.println("删除商品...");
return NONE;
}
}
配置Action
开启一个常量:
<!-- 开启动态方法访问 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
页面:
<h1>商品管理</h1>
<h4><a href="${ pageContext.request.contextPath }/product!find.action">查询商品</a></h4>
<h4><a href="${ pageContext.request.contextPath }/product!save.action">保存商品</a></h4>
<h4><a href="${ pageContext.request.contextPath }/product!update.action">修改商品</a></h4>
<h4><a href="${ pageContext.request.contextPath }/product!delete.action">删除商品</a></h4>
1.3 案例的代码实现
1.3.1 搭建开发环境
1.3.1.1 创建web项目,引入jar包
l Struts2的开发包
l Hibernate的开发包
l JSTL的包
1.3.1.2 引入配置文件和工具类
l struts.xml
l hibernate.cfg.xml
l log4j.properties
l HibernateUtils工具类
1.3.1.3 创建包结构
1.3.1.4 引入页面
1.3.1.5 配置web.xml的核心过滤器
<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>
1.3.2 代码实现
1.3.2.1 修改菜单页面提交的路径
1.3.2.2 编写实体和映射(直接复制)
1.3.2.3 编写Action
public class CustomerAction extends ActionSupport{
/**
* 查询所有客户的方法:findAll
*/
public String findAll(){
CustomerService customerService = new CustomerServiceImpl();
List<Customer> list = customerService.findAll();
// 存入域中:
ServletActionContext.getRequest().setAttribute("list", list);
return "findAll";
}
}
1.3.2.4 配置Action
<struts>
<!-- 配置常量 -->
<constant name="struts.action.extension" value="action"/>
<package name="crm" extends="struts-default" namespace="/">
<action name="customer_*" class="com.itheima.crm.web.action.CustomerAction" method="{1}">
<result name="findAll">/jsp/customer/list.jsp</result>
</action>
</package>
</struts>
1.3.2.5 编写Service
public class CustomerServiceImpl implements CustomerService {
@Override
// 业务层查询所有客户的方法:
public List<Customer> findAll() {
CustomerDao customerDao = new CustomerDaoImpl();
return customerDao.findAll();
}
}
1.3.2.6 编写DAO
public class CustomerDaoImpl implements CustomerDao {
@Override
// DAO中查询所有客户的方法:
public List<Customer> findAll() {
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
List<Customer> list = session.createQuery("from Customer").list();
transaction.commit();
return list;
}
}