SSH整合(xml版本)

(一、)整合前的准备:

所有的准备资料都可以从这里获取:

https://download.csdn.net/download/gray_humor/11065452

第一步:创建java web工程:

第二步:创建数据库和表结构:

create database crm;
use crm;

/*创建客户表*/
CREATE TABLE `cst_customer` (
  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
  `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
  `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
  `cust_address` varchar(128) DEFAULT NULL COMMENT '客户联系地址',
  `cust_phone` varchar(64) DEFAULT NULL COMMENT '客户联系电话',
  PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

第三步:编写实体类 


/**
 * 客户的实体类(数据模型)
 */
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;
	public Long getCustId() {
		return custId;
	}
	public void setCustId(Long custId) {
		this.custId = custId;
	}
	public String getCustName() {
		return custName;
	}
	public void setCustName(String custName) {
		this.custName = custName;
	}
	public String getCustSource() {
		return custSource;
	}
	public void setCustSource(String custSource) {
		this.custSource = custSource;
	}
	public String getCustIndustry() {
		return custIndustry;
	}
	public void setCustIndustry(String custIndustry) {
		this.custIndustry = custIndustry;
	}
	public String getCustLevel() {
		return custLevel;
	}
	public void setCustLevel(String custLevel) {
		this.custLevel = custLevel;
	}
	public String getCustAddress() {
		return custAddress;
	}
	public void setCustAddress(String custAddress) {
		this.custAddress = custAddress;
	}
	public String getCustPhone() {
		return custPhone;
	}
	public void setCustPhone(String custPhone) {
		this.custPhone = custPhone;
	}
	@Override
	public String toString() {
		return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource
				+ ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress
				+ ", custPhone=" + custPhone + "]";
	}	
}

第四步:编写业务层接口和实现类 


/**
 * 客户的业务层接口
 */
public interface ICustomerService {

	/**
	 * 查询所有客户
	 * @return
	 */
	List<Customer> findAllCustomer();
	
	/**
	 * @param customer
	 */
	void saveCustomer(Customer customer);
}

/**
 * 客户的业务层实现类
 */
public class CustomerServiceImpl implements ICustomerService {

	private ICustomerDao customerDao;

	public void setCustomerDao(ICustomerDao customerDao) {
		this.customerDao = customerDao;
	}

	@Override
	public List<Customer> findAllCustomer() {
		return customerDao.findAllCustomer();
	}

	@Override
	public void saveCustomer(Customer customer) {
		customerDao.saveCustomer(customer);
	}
}

第六步:创建持久层接口 


/**
 * 客户的持久层接口
 */
public interface ICustomerDao {
	
	/**
	 * 查询所有客户
	 * @return
	 */
	List<Customer> findAllCustomer();
	
	/**
	 * 保存客户
	 * @param customer
	 */
	void saveCustomer(Customer customer);
}

注意:做上述操作时,并不需要导入任何jar包。

(二)基于XML的独立式整合

1、保证spring框架在web工程中独立运行

第一步:拷贝spring的ioc,aop和事务控制三组jar包

第二步:编写spring配置文件并导入约束


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        		http://www.springframework.org/schema/beans/spring-beans.xsd
        		http://www.springframework.org/schema/tx 
        		http://www.springframework.org/schema/tx/spring-tx.xsd
        		http://www.springframework.org/schema/aop 
        		http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

第三步:把业务层和持久层配置到文件中


<!-- 把资源交给spring来管理 -->
<!-- 配置dao -->
<bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl"></bean>
	
<!-- 配置service -->
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
	<!-- 注入dao -->
	<property name="customerDao" ref="customerDao"></property>
</bean>

持久层实现类代码:
	此时不要做任何操作,就输出一句话。目的是测试spring框架搭建的结果。
/**
 * 客户的持久层实现类
 */
public class CustomerDaoImpl implements ICustomerDao {

	@Override
	public List<Customer> findAllCustomer() {	
		System.out.println("查询了所有用户");
		return null;
	}

	@Override
	public void saveCustomer(Customer customer) {
		System.out.println("保存了用户");
	}

}
2.1.4第四步:测试spring能否独立运行
/**
 * 测试类,测试spring框架可以独立运行
 */
public class Spring01Test {

	public static void main(String[] args) {
		//1.获取spring容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
		//2.跟Id获取bean对象
		ICustomerService customerService = (ICustomerService) ac.getBean("customerService");
		customerService.findAllCustomer();
	}
}

 

2、保证hibernate框架能够在web工程中独立运行

第一步:拷贝hibernate必备jar包到工程的lib目录

第二步:编写实体类的映射文件


<?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>

第三步:编写hibernate主配置文件 


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- 1.连接数据库的信息 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///crmroperty>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">1234</property>
		<!-- 2.hibernate的基本配置 -->
		<!-- 数据库的方言-->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 是否显示sql语句-->
		<property name="hibernate.show_sql">true</property>
		<!-- 是否格式化sql语句-->
		<property name="hibernate.format_sql">false</property>
		<!-- 采用何种方式生成数据库表结构 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 配置使用c3p0数据源 -->
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!--把session绑定到当前线程上的配置-->
		<property name="hibernate.current_session_context_class">thread</property>
		<!-- 3.映射文件的位置 -->
		<mapping resource="com/itheima/domain/Customer.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

第四步:编写测试类-测试保存客户


/**
 * hibernate的测试类
 * 		保证hibernate框架可以独立运行
 */
public class Hibernate02Test {

	@Test
	public void testFindAll(){
		//1.读取配置文件
		Configuration cfg = new Configuration();
		cfg.configure();
		//2.根据配置文件获取SessionFactory
		SessionFactory factory = cfg.buildSessionFactory();
		//3.根据SessionFactory获取一个Session
		Session s = factory.getCurrentSession();
		//4.开启事务
		Transaction tx = s.beginTransaction();
		//5.执行操作
		Query query = s.createQuery("from Customer");
		List list = query.list();
		for(Object o : list){
			System.out.println(o);
		}
		//6.提交事务
		tx.commit();
		//7.释放资源
		factory.close();
	}
	
	@Test
	public void testSave(){
		Customer c = new Customer();
		c.setCustName("传智专修学院");
		//1.读取配置文件
		Configuration cfg = new Configuration();
		cfg.configure();
		//2.根据配置文件获取SessionFactory
		SessionFactory factory = cfg.buildSessionFactory();
		//3.根据SessionFactory获取一个Session
		Session s = factory.getCurrentSession();
		//4.开启事务
		Transaction tx = s.beginTransaction();
		//5.执行操作
		s.save(c);
		//6.提交事务
		tx.commit();
		//7.释放资源
		factory.close();
	}
}

3、整合spring和hibernate框架

第一步:在spring配置文件中配置SessionFactory 


<!-- 配置SessionFactory -->
<bean id="sessionFactory" 
			class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 使用的是hibernate主配置文件中的内容,我们只需要指定hibernate主配置文件的所在位置 -->
	<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>

第二步:改造Dao继承HibernateDaoSupport


/**
 * 客户的持久层实现类
 */
public class CustomerDaoImpl extends HibernateDaoSupport implements ICustomerDao {

	@Override
	public List<Customer> findAllCustomer() {	
		return (List<Customer>) getHibernateTemplate().find("from Customer");
	}

	@Override
	public void saveCustomer(Customer customer) {
		getHibernateTemplate().save(customer);
	}
}

第三步:在spring配置文件中给Dao注入SessionFactory 


<!-- 配置dao -->
<bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

第四步:测试


/**
 * 整合spring和hibernate的测试类
 * spring整合Junit
 * 	第一步:拷贝jar包
 * 		spring-junit-4.2.4.jar
 *  第二步:使用注解替换运行器(原来junit的main方法)
 *  	@RunWith(支持spring的main方法)
 *  	@ContextConfiguration(指定spring的配置文件位置)
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:bean.xml"})
public class SpringHibernate03Test {
	
	@Autowired
	private ICustomerService customerService;

	@Test
	public void testFindAll(){
		List list = customerService.findAllCustomer();
		for(Object o : list){
			System.out.println(o);
		}
	}
	
	@Test
	public void testSave(){
		Customer c = new Customer();
		c.setCustName("传智学院test");		
		customerService.saveCustomer(c);
	}
}

测试结果:
    无论保存还是查询都运行失败!
    按常理来说,我们没有配置事务,保存失败是可以理解的。为什么查询也会失败呢?
分析原因:
    是由于spring的HibernateTemplate对象在使用Session时,spring创建了Session的代理对象,在这个过程中,spring对hibernate绑定Session到当前线程的配置不认识了,所以运行失败。

第五步:修改把Session绑定到当前线程上 


<!-- 是hibernate把session绑定到当前线程上的配置 
<property name="hibernate.current_session_context_class">thread</property>-->
<!-- 是spring把sesion绑定到当前线程上的配置 -->
<property name="hibernate.current_session_context_class">
	org.springframework.orm.hibernate5.SpringSessionContext
</property>

此时再运行刚才的测试:
        查询可以使用了。保存不能使用,原因是没有事务。

第六步:配置Spring的事务

(1)、配置事务管理器并注入SessionFactory


<!-- 配置事务管理器 -->
<bean id="transactionManager" 
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
	<!-- 注入SessionFactory -->
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

 (2)、配置事务的通知及通知的属性


<!-- 配置事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<!-- 配置事务的属性 -->
	<tx:attributes>
		<tx:method name="*" read-only="false" propagation="REQUIRED"/>
		<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
	</tx:attributes>
</tx:advice>

(3)、配置AOP建立切入点表达式和事务通知的关系


<!-- 配置aop -->
<aop:config>
	<!-- 配置通用切入点表达式 -->
	<aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
	<!-- 建立事务通知和切入点表达式的对应关系 -->
	<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>

再次测试:
    此时保存和查询都可以正常使用了。

 

4、保证struts2框架能够在web工程中独立运行

第一步:拷贝struts2的必备jar包

第二步:在类的类的根路径下编写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.devMode" value="true"></constant>
</struts>

第三步:在web.xml中配置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>

第四步:导入jsp页面

第五步:修改menu.jsp


<A class=style2 
	href="${pageContext.request.contextPath}/customer/addUICustomer.action"  
	target=main>
	- 新增客户
</A>

第六步:在struts.xml中配置action


<!--  获取添加客户页面 -->
<action name="addUICustomer" class="com.itheima.web.action.CustomerAction" 
			method="addUICustomer">
	<result name="addUICustomer">/jsp/customer/add.jsp</result>
</action>

第七步:编写动作类和方法


/**
 * 客户的动作类
*/
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	private Customer customer = new Customer();

	@Override
	public Customer getModel() {
		return customer;
	}
	
	/**
	 * 获取添加客户页面
	 * @return
	 */
	public String addUICustomer(){
		return "addUICustomer";
	}
}

第八步:测试

运行结果:通过点击【新增客户】可以跳转到客户添加页面

 

5、整合spring和struts2

第一步:拷贝struts2-spring-plugin-2.3.24.jar到lib目录

第二步:在action中使用构造函数获取Service对象


public CustomerAction(){
	ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(
							ServletActionContext.getServletContext());
		//由于动作类是多例的,每次都会创建容器,导致资源的浪费。一个应用应该只有一个容器
		System.out.println(ac);
		customerService = (ICustomerService) ac.getBean("customerService");
	}

第三步:测试

运行结果:查询客户列表测试通过。保存测试通过。

6、优化配置

一、配置spring的监听器

ServletContextListener监听器可以监听到ServletContext对象的创建和销毁。

 

Spring框架为我们提供了一个监听器:ContextLoaderListener

它是ServletContextListener接口的实现类,负责监听ServletContext对象的创建,为我们创建容器,监听ServletContext对象的销毁,销毁容器。

我们只需要配置上即可。

ContextLoaderListener在spring-web-4.2.4.RELEASE.jar中

所以我们要先导入这个jar包。

在web.xml中配置监听器:

<listener>
    <listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
</listener>

当配置了此监听器后,就不需要使用Action的构造函数了,可以把构造函数那段删除了。

此监听器只能读取WEB-INF目录中的名称为applicationContext.xml的配置文件。这显然限制了我们的配置。

我们可以通过配置全局初始化参数的方式,指定spring配置文件的位置.

二、配置指定spring配置文件的位置

我们把spring配置文件放到此处,需要配置全局初始化参数:


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/spring/applicationContext.xml</param-value>
</context-param>

三、分文件编写spring配置


我们写到这里,其实搭建环境已经基本结束了,但是发现spring的配置文件杂乱无章,使我们在找配置的时候,很难一下找到。所以我们采用分配置文件编写的方式。

编写主配置文件引入其他配置文件


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
      		http://www.springframework.org/schema/beans/spring-beans.xsd
      		http://www.springframework.org/schema/tx 
      		http://www.springframework.org/schema/tx/spring-tx.xsd
      		http://www.springframework.org/schema/aop 
      		http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 引入其他spring配置文件 -->
	<import resource="applicationContext-customer.xml"/>
	<import resource="applicationContext-jdbc.xml"/>
	<import resource="applicationContext-tx.xml"/>
</beans>

编写针对需求的配置文件applicationContext-customer.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
      		http://www.springframework.org/schema/beans/spring-beans.xsd
      		http://www.springframework.org/schema/tx 
      		http://www.springframework.org/schema/tx/spring-tx.xsd
      		http://www.springframework.org/schema/aop 
      		http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 把资源交给spring来管理 -->
	<!-- 配置dao -->
	<bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 配置service -->
	<bean id="customerService" 
					class="com.itheima.service.impl.CustomerServiceImpl">
		<!-- 注入dao -->
		<property name="customerDao" ref="customerDao"></property>
	</bean>
</beans>

编写数据库连接的配置文件applicationContext-jdbc.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
      		http://www.springframework.org/schema/beans/spring-beans.xsd
      		http://www.springframework.org/schema/tx 
      		http://www.springframework.org/schema/tx/spring-tx.xsd
      		http://www.springframework.org/schema/aop 
      		http://www.springframework.org/schema/aop/spring-aop.xsd">
	
	<!-- 配置SessionFactory -->
	<bean id="sessionFactory" 
			class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
	<!-- 使用的是hibernate主配置文件中的内容,我们只需要指定hibernate配置文件的位置 -->
		<property name="configLocation" 
				   value="classpath:config/hibernate/hibernate.cfg.xml">/>
	</bean>
</beans>

编写事务控制的配置文件applicationContext-tx.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
      		http://www.springframework.org/schema/beans/spring-beans.xsd
      		http://www.springframework.org/schema/tx 
      		http://www.springframework.org/schema/tx/spring-tx.xsd
      		http://www.springframework.org/schema/aop 
      		http://www.springframework.org/schema/aop/spring-aop.xsd">

	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" 
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<!-- 注入SessionFactory -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 配置事务的通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 配置事务的属性 -->
		<tx:attributes>
			<tx:method name="*" read-only="false" propagation="REQUIRED"/>
			<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置aop -->
	<aop:config>
		<!-- 配置通用切入点表达式 -->
		<aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" 
						id="pt1"/>
		<!-- 建立事务通知和切入点表达式的对应关系 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
	</aop:config>
</beans>

四、配置指定struts2配置文件位置


我们的spring和hibernate配置文件都存到了src/config/的对应包中了,只有struts2配置文件还在类的根路径下,它也可以通过配置的方式指定struts.xml的位置。配置的是过滤器的初始化参数。初始化参数的name和value都是固定写法。


<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	</filter-class>
	<init-param>
     	<param-name>config</param-name>
      	<param-value>
			struts-default.xml,struts-plugin.xml,config/struts/struts.xml
		</param-value>
    </init-param>
</filter>
<filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

分文件编写struts2配置文件
当我们后面做的模块越来越多,struts2一个配置文件写起来也会杂乱无章,所以我们也可以把struts2的配置文件分开编写。

编写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.devMode" value="true"></constant>
	
	<package name="myDefault" extends="struts-default" abstract="true">
		<!--  有公共的配置就写在此处,没有就空着 -->
	</package>

	<!--引入其他struts2配置文件 -->
	<include file="config/struts/struts-customer.xml"></include>
</struts>

针对不同模块编写不同的配置文件struts-customer.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="customer" extends="myDefault" namespace="/customer">
		<!--  获取添加客户页面 -->
		<action name="addUICustomer" 
				class="com.itheima.web.action.customerAction" 
				method="addUICustomer">
			<result name="addUICustomer">/jsp/customer/add.jsp</result>
		</action>
		
		<!--  查询客户列表 -->
		<action name="findAllCustomer" 
				class=" com.itheima.web.action.customerAction" 
				method="findAllCustomer">
			<result name="findAllCustomer">/jsp/customer/list.jsp</result>
		</action>
	</package>
</struts>

7、管理Action的两种方式


第一种方式:让struts2自己来管理
此种方式就是在action标签的class属性中提供动作类的全限定类名。



<action name="addUICustomer" 
		class="com.itheima.web.action.customerAction" 
		method="addUICustomer">
	<result name="addUICustomer">/jsp/customer/add.jsp</result>
</action>

 第二种方式:让spring来管理(实际开发中采用的方式)


此种方式就是在spring配置文件中配置Action,在struts2配置文件action标签的class属性里写bean的id。



spring配置文件:
<!-- 配置Action -->
<bean id="customerAction" class="com.itheima.web.action.CustomerAction" 
		scope="prototype">
	<!-- 注入service -->
	<property name="customerService" ref="customerService"></property>
</bean>	

struts2配置文件:
<!--  获取添加客户页面 -->
<action name="addUICustomer" class="customerAction" method="addUICustomer">
	<result name="addUICustomer">/jsp/customer/add.jsp</result>
</action>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值