您的位置:首页 > 编程语言 > Java开发

ssh之雇员管理系统(5)-将struts+spring整合

2013-05-14 13:05 447 查看
四、让web.xml中在tomcat启动的时候去实例化spring容器和struts

在web.xml中增加下面这个就可以实例化spring容器

<!-- 对Spring容器进行实例化 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

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 {

/**
* 当一个请求发来时次方法将被执行
*/

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,由于是面向接口编程,所以我们用到接口
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 stub
return super.execute(map,form,request,response);
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: