1. 导入CRM项目的UI页面,找到添加客户的页面,修改form表单,访问Action
* 将menu.jsp中133行的新增客户的跳转地址改为:href="${pageContext.request.contextPath}/jsp/customer/add.jsp"
* 将jsp/customer下的add.jsp的提交页面的地址改为:action="${pageContext.request.contextPath }/customer_save"。当点击保存按钮之后,访问customer这个action中的save方法。
2. 创建包结构:
* ssh1
* com.huida.dao
* com.huida.domain
* com.huida.service
*com.huida.web
3. 在com.huida.web下创建action类CustomerAction。
在com.huida.service下创建接口:CustomerService,实现接口的类:CustomerServiceImpl。
CustomerService接口中的方法为:
package com.huida.service; public interface CustomerService { public void save(); }
CustomerServiceImpl实现类的内容为:
package com.huida.service; public class CustomerServiceImpl implements CustomerService { @Override public void save() { System.out.println("Service层中的save方法被执行了"); } }
4.编写CustomerAction接收请求,在struts.xml中完成Action的配置
CustomerAction的接收请求为:
package com.huida.web;import com.huida.domain.Customer; import com.huida.service.CustomerService; import com.opensymphony.xwork2.ActionSupport;public class CustomerAction extends ActionSupport{ /* * 保存客户的方法 */ public String save(){ System.out.println("Action中执行了save方法"); return NONE; } }
在struts.xml中对Action的配置为:
<package name="crm" namespace="/" extends="struts-default"> <action name="customer_*" class="com.huida.web.CustomerAction" method="{1}"> <result name=""></result> </action> </package>
5.在Action中获取到service(开发不会使用,因为麻烦)
* 可以通过 WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext()); 来获取,但是这种方式编写代码太麻烦了!!
代码如下:
ackage com.huida.web; import org.apache.struts2.ServletActionContext; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.huida.domain.Customer; import com.huida.service.CustomerService; import com.opensymphony.xwork2.ActionSupport; public class CustomerAction extends ActionSupport { /* * 保存客户的方法 */ public String save(){ System.out.println("Action中执行了save方法"); //传统的web工厂方法 WebApplicationContext webApplicationContext=WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext()); CustomerService customerService=(CustomerService) webApplicationContext.getBean("customerService"); customerService.save(); return NONE; } }
* 还可以通过new ClassPathXmlApplicationContext("applicationContext.xml")来获取。
代码如下:
package com.huida.web; import org.apache.struts2.ServletActionContext; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.huida.domain.Customer; import com.huida.service.CustomerService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport { /* * 保存客户的方法 */ public String save(){ System.out.println("Action中执行了save方法"); //使用spring的工厂 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); CustomerService customerService=(CustomerService) ac.getBean("customerService"); customerService.save(); return NONE; } }
通过这两种方法我们都可以实现从action中获取service。
6. 我们可以验证一下struts与spring整合是否成功。
启动服务器-->在浏览器中输入http://localhost:8080/ssh1-->在页面中点击客户管理-->新增客户-->点击保存按钮。在控制台上输出如下内容:
通过以上步骤我们便将struts与spring通过传统的方法整合起来了。但是这种整合很麻烦,所以在下一篇博文中我就对整合的常用方法进行总结。