ssh框架整合,做登录的功能??
用struts2.xml + spring + hibernate3.0 做登录的功能,总是报错,小弟第一次用3个框架整合,初学者,希望得到各位前辈的指点
//web.xml applicationContext.xml默认放在src下
<!-- 配置监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
//applicationContext.xml里面的配置文件
<!-- 用户登录dao -->
<bean id="logindao" class="com.jy.dao.LoginUserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 用户登录service -->
<bean id="loginservice" class="com.jy.service.LoginUserService">
<property name="userdao" ref="logindao"></property>
</bean>
//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<Users> 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
<package name="default" extends="struts-default" namespace="/">
<action name="loginUser" class="loginservice">
<result name="success">/su.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
报错信息:
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";
}