ssh之雇员管理系统(5)-将struts+spring整合
四、让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>
那我们就要用到spring啦
我们现在要验证用户登录,则我们要通过在LoginAction.java中进行验证,当然也要用其spring啦
//下面这段话既可以代替ApplicationContext ac = new AplicationContext("xxx.xml");
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
以上就是我们在其中用到spring啦,这里的ctx就是我们的ac,我们肯定要得到他的getBean(“xxx”)
好啦 现在我们就可以根据面向接口编程来实例化这个bean啦
//得到验证的bean,由于是面向接口编程,所以我们用到接口
EmployeeServiceInter employeeServiceInter = (EmployeeServiceInter)ctx.getBean("employeeService");
那我们现在就要去验证啦,但是我们要去找接口的验证方法啦,还有逻辑实现的编辑啊,那该怎样 编辑实现逻辑呢。
那我们这样考虑吧,首先 //验证用户的方法,如果雇员存在则返回改雇员的全部信息,否则返回空
public Employee vlidateEmployee(Employee e);
这样的话说就要返回的是Employee类型啦,把它封装到一个List中,这样在我们页面显示的时候也就很好的取出来啦。
下面是验证逻辑的方法
@Transactional
public Employee vlidateEmployee(Employee e) {
String hql = "from Employee where id=? and pwd=?"; //这里不是SQL语句而是HQL语句,Employee是域模型,不是数据库中的表名字
List<Employee> list = sessionFactory.getCurrentSession().createQuery(hql).
setString(0, e.getId()+"").setString(1, e.getPwd()).list();
if (list.size() ==1) {
System.out.println("****");
return list.get(0);
}else {
return null;
}
}
- 这里是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 { /** * 当一个请求发来时次方法将被执行 */ 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); } }
作者:少帅
出处:少帅的博客--http://www.cnblogs.com/wang3680
您的支持是对博主最大的鼓励,感谢您的认真阅读。
本文版权归作者所有,欢迎转载,但请保留该声明。
支付宝 微信