四、让spring接管我们的struts(action控件)
- 首先我们在struts-config.xml中配置叫做代理请求 DelegatingRequestProcessor在org.springframework.web.struts下就是让spring接管struts
<!-- 配置代理请求处理 DelegatingRequestProcessor ,它的用处是 -->
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
如图所示配置前的时候是找action对应的type=com.hsp.web.action.LoginAction,配置后的话我们就得向spring容器中找这个action啦。
- 在spring中配置这个action,这个时候就是把LoginAction.java看成是bean,因为在spring中的配置,几乎是bean的配置啊。
<!-- 配置action --><bean name="/login" class="com.wang.web.action.LoginAction" />
这个时候我们就可以可以将struts-config.xml中的/login中的type=type=com.hsp.web.action.LoginAction给去掉啦,因为他会找到spring中的action,这里name必须和struts-config.xml的相同。这我们就可以通过spring容器来获取action,和配置action的一些属性
-
-
-
- 这个时候呢我们的LoginAction.java中就可以省略很多不必要的东西
package com.wang.web.action;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils;import com.wang.domain.Employee; import com.wang.service.interfaces.EmployeeServiceInter; import com.wang.web.form.EmployeeForm;public class LoginAction extends DispatchAction {/*** 当一个请求发来时次方法将被执行*/EmployeeServiceInter employeeServiceInter; //我们在此处定义出接口EmployeeServiceInter ,下面去实现他的set方法,这里的
employeeServiceInter要与ApplicationContext中配置的action的属性中的name值相同。
//在调用时候action时候,spring就会将这个employeeServiceInter添加给此处的set方法啦
public void setEmployeeServiceInter(EmployeeServiceInter employeeServiceInter) {
System.out.println("setEmployeeServiceInter方法被调用");
this.employeeServiceInter = employeeServiceInter;
}
public ActionForward login(ActionMapping map, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
//下面这段话既可以代替ApplicationContext ac = new AplicationContext("xxx.xml");
// WebApplicationContext ctx = // WebApplicationContextUtils.getWebApplicationContext
// (this.getServlet().getServletContext()); //得到验证的bean,由于是面向接口编程,所以我们用到接口
//由于我们把这个LoginAction看成是bean,而且也在ApplicationContext中配置啦,那下面这句话就是为得到这个
employeeService的bean,那我们在ApplicationContext中呢就可以配置action的属性,
让这个属性的ref为employeeService
EmployeeServiceInter employeeServiceInter = (EmployeeServiceInter)ctx.getBean("employeeService");//通过ActionForm,获取表单的值EmployeeForm employeeForm = (EmployeeForm) form;Employee e = new Employee();//从表单中获取值,set到Employee中 e.setId(Integer.parseInt(employeeForm.getId()));e.setPwd(employeeForm.getPwd());//通过逻辑去验证e = employeeServiceInter.vlidateEmployee(e);if(e !=null){//e不为空,把雇员e添加到session中便于页面用到request.getSession().setAttribute("loginuser", e);return map.findForward("ok");}else {return map.findForward("err");}}public ActionForward loginout(ActionMapping map, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {// TODO Auto-generated method stubreturn super.execute(map,form,request,response);} }现在我们在ApplicationContext.xml中配置action的属性啦,让他的属性的ref指向employeeService。
<!-- 配置action --><bean name="/login" class="com.wang.web.action.LoginAction" ><property name="employeeServiceInter" ref="employeeService"></property></bean>
那在LoginAction中的那句话EmployeeServiceInter employeeServiceInter = (EmployeeServiceInter)ctx.getBean("employeeService");就可以不用啦。
- 这个时候呢我们的LoginAction.java中就可以省略很多不必要的东西
-
-
- 通过使用sping来接管我们的action,还有一个好处,可以解决action 是单例的问题.通过在applicationContext.xml文件中配置属性 <bean scope=”singlton/prototype/request/sesssion/global session”/> 那ApplicationContext.xml中配置action就变为下面的形式
<!-- 配置action --><bean name="/login" scope="prototype" class="com.wang.web.action.LoginAction" ><property name="employeeServiceInter" ref="employeeService"></property></bean>