关于SSH三大框架整合——配置文件方式、配置文件+注解方式

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框架来创建,推荐的方式
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值