Spring + Hibernate DAO injection fails in Netbeans -
i have controller class logindata.java
@controller @managedbean(name = "login") @sessionscoped public class logindata implements serializable{ @autowired private logindao logindao; private string username; private string password; public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public void validateuser(){ try{ logindao.login(username, password); }catch(businessexception e){ } } }
i trying autowire dao , implementation:
logindao
public interface logindao { public boolean login(string username, string password)throws businessexception; public boolean register(string username, string password, usertype type)throws businessexception; }
logindaoimpl
public class logindaoimpl implements logindao{ private string username; private string password; private sessionfactory sessionfactory; public void setsessionfactory(sessionfactory sessionfactory) { this.sessionfactory = sessionfactory; } @override public boolean login(string username, string password) throws businessexception{ query query = sessionfactory.getcurrentsession().createquery( "select u user u u.username=:un"); query.setparameter("un", username); if(query.list().size()==0)throw new businessexception("no user in database!"); user user = (user)(query.list().get(0)); return gethashmd5(password).equals(user.getpassword()); } @override public boolean register(string username, string password, usertype type) throws businessexception{ throw new unsupportedoperationexception("not supported yet."); //to change body of generated methods, choose tools | templates. } public static string gethashmd5(string string) throws businessexception{ try { messagedigest md = messagedigest.getinstance("md5"); biginteger bi = new biginteger(1, md.digest(string.getbytes())); return bi.tostring(16); } catch (exception ex) { throw new businessexception(ex.getmessage()); } } }
i trying inject sessionfactory in dao implementation, don't know if code work. xml configuration:
dispatcher-servlet.xml
<bean class="org.springframework.web.servlet.mvc.support.controllerclassnamehandlermapping"/> <bean id="urlmapping" class="org.springframework.web.servlet.handler.simpleurlhandlermapping"> <property name="mappings"> <props> <prop key="index.htm">indexcontroller</prop> </props> </property> </bean> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver" p:prefix="/web-inf/jsp/" p:suffix=".jsp" /> <!-- index controller. --> <bean name="indexcontroller" class="org.springframework.web.servlet.mvc.parameterizableviewcontroller" p:viewname="index" /> <bean id="logindao" class="rs.ac.bg.etf.services.logindaoimpl"/>
web.xml
<context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/applicationcontext.xml</param-value> </context-param> <context-param> <param-name>javax.faces.project_stage</param-name> <param-value>development</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet> <servlet-name>faces servlet</servlet-name> <servlet-class>javax.faces.webapp.facesservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>faces servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list>
i null pointer exception when dao method called in controller jsf form. knows problem? ps: xml files generated netbeans ide.
you're missing @repository
annotation autowiring work.
try this:
@repository("logindao") public class logindaoimpl implements logindao
Comments
Post a Comment