1. Spring整合Struts2框架,存在2种方式(Action对象由哪个框架创建的)
* 导入开发的jar包
* struts2-spring-plugin-2.3.24.jar
* Action对象可以由Struts2框架创建,默认的方式
* 因为导入struts2-spring-plugin-2.3.24.jar包,提供类struts-plugin.xml配置文件,
该文件中配置了常量:<constant name="struts.objectFactory" value="spring" />
* 允许在Action中可以去Spring的容器中获取到指定名称的service的对象,默认按名称进行注入的
* 具体整合的代码如下
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
private static final long serialVersionUID = 4406904609220191155L;
private Customer model = new Customer();
public Customer getModel() {
return model;
}
private CustomerService customerService;
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
/**
* 查询所有的数据
* @return
* @throws Exception
*/
public String list() throws Exception {
System.out.println("查询所有的客户...");
/* 之前的整合的方式,底层的原理
// 获取到web版本的工厂,获取到service的对象,调用方法
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
CustomerService cs = (CustomerService) context.getBean("customerService");
cs.findAll();
*/
// 第一种方式,Action对象由Struts2框架来创建
customerService.findAll();
return NONE;
}
}
<package name="crm" extends="struts-default" namespace="/">
<!-- 客户的模块 -->
<action name="customerAction_*" class="cn.itcast.customer.action.CustomerAction" method="{1}">
</action>
</package>
* service的代码
public class CustomerServiceImpl implements CustomerService {
public List<Customer> findAll() {
System.out.println("查询所有的客户...");
return null;
}
}
<bean id="customerService" class="cn.itcast.customer.service.impl.CustomerServiceImpl"/>
* Action对象可以由Spring框架来创建,推荐的方式
* 导入开发的jar包
* struts2-spring-plugin-2.3.24.jar
* Action对象可以由Struts2框架创建,默认的方式
* 因为导入struts2-spring-plugin-2.3.24.jar包,提供类struts-plugin.xml配置文件,
该文件中配置了常量:<constant name="struts.objectFactory" value="spring" />
* 允许在Action中可以去Spring的容器中获取到指定名称的service的对象,默认按名称进行注入的
* 具体整合的代码如下
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
private static final long serialVersionUID = 4406904609220191155L;
private Customer model = new Customer();
public Customer getModel() {
return model;
}
private CustomerService customerService;
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
/**
* 查询所有的数据
* @return
* @throws Exception
*/
public String list() throws Exception {
System.out.println("查询所有的客户...");
/* 之前的整合的方式,底层的原理
// 获取到web版本的工厂,获取到service的对象,调用方法
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
CustomerService cs = (CustomerService) context.getBean("customerService");
cs.findAll();
*/
// 第一种方式,Action对象由Struts2框架来创建
customerService.findAll();
return NONE;
}
}
<package name="crm" extends="struts-default" namespace="/">
<!-- 客户的模块 -->
<action name="customerAction_*" class="cn.itcast.customer.action.CustomerAction" method="{1}">
</action>
</package>
* service的代码
public class CustomerServiceImpl implements CustomerService {
public List<Customer> findAll() {
System.out.println("查询所有的客户...");
return null;
}
}
<bean id="customerService" class="cn.itcast.customer.service.impl.CustomerServiceImpl"/>
* Action对象可以由Spring框架来创建,推荐的方式