当前位置:我的异常网» Java Web开发 » ssh框架调整,做登录的功能?
ssh框架调整,做登录的功能?
www.myexceptions.net 网友分享于:2013-09-11 浏览:12次
ssh框架整合,做登录的功能??
用struts2.xml + spring + hibernate3.0 做登录的功能,总是报错,小弟第一次用3个框架整合,初学者,希望得到各位前辈的指点
//web.xml applicationContext.xml默认放在src下
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/WEB-INF/classes/applicationContext.xml
//applicationContext.xml里面的配置文件
//dao层
//用户登录
public int LoginUser(String userName,String userPassword){
String hql = "from Users where userName='"+userName+"' and userPassword='"+userPassword+"' ";
Query query = this.getSession().createQuery(hql);
List userList = query.list();
return userList.size();
}
//service层
private String userName;
private String userPassword;
private LoginUserInfo info;
private LoginUserDao userdao;
public String execute(){
try{
LoginUserDao dao = new LoginUserDao();
int i = dao.LoginUser(this.getUserName(), this.getUserPassword());
System.out.println(this.getUserName());
if(i > 0 ){
return "success";
}
else{
return "error";
}
}
catch(Exception ex){
ex.printStackTrace();
}
return "error";
}
//struts.xml
/su.jsp
/error.jsp
报错信息:
java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:143)
at com.jy.dao.LoginUserDao.LoginUser(LoginUserDao.java:19)
at com.jy.service.LoginUserService.execute(LoginUserService.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
------解决方案--------------------
登录dao层
public int LoginUser(String userName,String userPassword){
List list = this.getHibernateTemplate().find("from Users where userName='"+userName+"' and userPassword='"+userPassword+"' ");
return list.size();
}
action层
try{
String [] name = {"applicationContext.xml"};
BeanFactory bean = new ClassPathXmlApplicationContext(name);
LoginUserService login = (LoginUserService)bean.getBean("loginservice");
int i = login.getUserdao().LoginUser(userName, userPassword);
if(i > 0 ){
return "success";
}
else{
return "error";
}
文章评论