Struts2的执行流程-实现客户列表查询
一、Struts2的执行流程
页面发送请求,请求先到达前端控制器(核心过滤器StrutsPrepareAndExecuteFilter),过滤器中执行一组拦截器,这组拦截器完成了部分的功能代码。
如果没有拦截器,就会执行目标Action。执行Action后返回一个逻辑视图名称。根据Result进行页面跳转。
struts2框架,它不仅支持基于XML的配置方式,同时也支持基于注解配置的方式。
注解和XML的配置,都是告知struts2框架,当我们jsp页面发送请求,根据配置执行对应动作类的方法,
并根据返回值,前往指定的结果视图(jsp页面或者其他动作)。 它们只是配置的形式不一样。
使用Struts.xml:
优点:配置流程清晰,可读性高,维护方便
缺点:配置繁琐。
使用注解:
优点:配置简洁。
缺点:维护不方便,可读性差。
代码:
1.Action类
public class HelloStrutsAction {
public String execute(){
System.out.println("Hello Struts2...");
return "success";
}
}
2.修改Action的配置
<struts>
<package name="demo1" extends="struts-default" namespace="/">
<action name="helloStruts" class="struts2.HelloStrutsAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
3.创建success.jsp
<body>
<h1>Struts2的成功跳转页面</h1>
</body>
二、Struts2的常见配置
1.struts.xml中配置
核心配置文件,主要负责管理Action
通常放在src目录下,在该目录下的struts.xml文件可以被自动加载
2.Package标签的配置
Package称为是包,为了管理Action的配置。
包可以“继承”已定义的包,并可以添加自己包的配置
包的作用:简化维护工作,提高重用性
* name:包的名称,属性为必需的且唯一,用于指定包的名 称
* extends:属性指定要扩展的包,通常情况下继承struts-default。
* namespace:(可选)名称空间,namespace通常和<action>标签中的name属性共同决定 访问路径。
* namespace=””
* namespace=”/”
* namespace=”/aaa”
* abstract:抽象的,用于被其他的包继承。
3.配置文件的加载顺序
Struts2中的配置文件
配置文件的数量:
一共有6个配置文件。
6个配置文件执行时机:
default.properties 它都是key=value的形式。类似于开关。
struts-default.xml 配置的都是struts2的核心。纯struts2核心。
struts-plugin.xml 它是当我们使用了plugin的jar时才有的配置文件。如果没用插件,就没有。
struts.xml 我们程序中的struts2核心配置。是针对当前项目的核心配置。 推荐的
struts.properties struts2不仅支持基于xml的配置还可以再使用.properties。但是我们不推荐使用
web.xml 配置的是struts2的一些开关性配置。靠过滤器的初始化参数配置
它最后加载,但加载的只是和struts2相关的配置。
它们会按照上面的顺序逐个加载,如果遇到相同的配置,后加载的覆盖先加载的。
init_FileManager();
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] ----加载JavaBean
4.结果页面的类型
result标签的属性:
name:逻辑视图的名称。SUCCESS(默认值) ERROR INPUT NONE LOGIN
type:页面跳转的类型。
* dispatcher :默认值。转发到某个JSP
* redirect :重定向到某个JSP
* chain :链。转发到某个Action
* redirectAction :重定向到某个Action
* stream :文件下载的时候用到。
三、使用struts2+hibernate实现客户列表查询
1.搭建hibernate开发环境并准备实体类和映射文件
/**
* 客户的实体类
* /
public class Customer implements Serializable {
private Long custId;
private String custName;
private String custSource;
private String custIndustry;
private String custLevel;
private String custAddress;
private String custPhone;
生成get,set方法;
}
2.映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itheima.domain">
<class name="Customer" table="cst_customer">
<id name="custId" column="cust_id">
<generator class="native"></generator>
</id>
<property name="custName" column="cust_name"></property>
<property name="custSource" column="cust_source"></property>
<property name="custIndustry" column="cust_industry"></property>
<property name="custLevel" column="cust_level"></property>
<property name="custAddress" column="cust_address"></property>
<property name="custPhone" column="cust_phone"></property>
</class>
</hibernate-mapping>
3.修改主页jsp页面进入程序入口:
<TBODY>
<TR>
<TD class=menuSmall>
<A class=style2 href="${pageContext.request.contextPath}/welcome.html" target=main>- 新增客户</A>
</TD>
</TR>
<TR>
<TD class=menuSmall>
<A class=style2 href="${pageContext.request.contextPath}/customer/findAllCustomer.action" target=main>- 客户列表</A>
</TD>
</TR>
</TBODY>
4.在配置文件中配置findAllCustomer.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>
<!-- 开启开发者模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 配置动作 -->
<package name="customer" extends="myDefault" namespace="/">
<!-- 查询所有客户 -->
<action name="findAllCustomer" class="com.itheima.web.action.CustomerAction" method="findAllCustomer">
<result name="findAllCustomer">/jsp/customer/list.jsp</result>
</action>
</package>
</struts>
5.创建动作类并提供动作方法
/**
* 客户的动作类
*/
public class CustomerAction extends ActionSupport {
private CustomerService customerService = new CustomerServiceImpl();
/**
* 查询所有客户
* @return
*/
public String findAllCustomer(){
//1.使用service对象查询所有客户
List<Customer> customers = customerService.findAllCustomer();
//2.获取request对象
HttpServletRequest request = ServletActionContext.getRequest();
//3.存入request域中
request.setAttribute("customers", customers);
return "findAllCustomer";
}
}
6.编写service接口和实现类
/**
* 客户的业务层接口
*/
public interface CustomerService {
/**
* 查询所有客户
* @return
*/
List<Customer> findAllCustomer();
}
/**
* 客户的业务层实现类
*/
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao = new CustomerDaoImpl();
@Override
public List<Customer> findAllCustomer() {
Session s = null;
Transaction tx = null;
try{
s = HibernateUtil.getCurrentSession();
tx = s.beginTransaction();
List<Customer> customers = customerDao.findAllCustomer();
tx.commit();
return customers;
}catch(Exception e){
tx.rollback();
throw new RuntimeException(e);
}
}
}
7.编写dao接口和实现类
/**
* 客户的持久层接口
*/
public interface CustomerDao {
/**
* 查询所有客户
* @return
*/
List<Customer> findAllCustomer();
}
/**
* 客户的持久层实现类
*/
public class CustomerDaoImpl implements CustomerDao {
@Override
public List<Customer> findAllCustomer() {
return HibernateUtil.getCurrentSession().createQuery("from Customer").list();
}
}
8.修改jsp/customer/list.jsp你成功要跳转的jsp页面
list.jsp中的代码节选,只涉及forEach标签遍历的部分。
<c:forEach items="${customers}" var="customer">
<TR style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
<TD>${customer.custName }</TD>
<TD>${customer.custLevel }</TD>
<TD>${customer.custSource }</TD>
<TD>${customer.custIndustry }</TD>
<TD>${customer.custAddress }</TD>
<TD>${customer.custPhone }</TD>
<TD>
<a href="${pageContext.request.contextPath}/customer/CustomerServlet?method=editCustomerUI&custId=${customer.custId}">修改</a>
<a href="${pageContext.request.contextPath }/customer/CustomerServlet?method=removeCustomer&custId=${customer.custId}">删除</a>
</TD>
</TR>
</c:forEach>