Hi, I have followed steps from your tutorial titled SpringMVClogin with database. I am not using Jetty, as I have Tomcat as my server using Eclipse IDE. When I run the program I get a Http 404 page error. In Tomcat console it states the reason as:
"Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.pizza.service.LoginService com.pizza.controllers.LoginController.loginService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.pizza.dao.LoginDAO com.pizza.service.LoginServiceImpl.loginDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginDAO': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration"
My Code reads as follow:
Controller
package com.pizza.controllers;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;
import java.util.Map; import javax.validation.Valid;
import com.pizza.form.LoginForm; import com.pizza.service.*;
@Controller @RequestMapping("loginform.html") @Configuration @ComponentScan("com.pizza.service") public class LoginController {
@Autowired public LoginService loginService; @RequestMapping(method = RequestMethod.GET) public String showForm(Map model) { LoginForm loginForm = new LoginForm(); model.put("loginForm", loginForm); return "loginform"; } @RequestMapping(method = RequestMethod.POST) public String processForm(@Valid LoginForm loginForm, BindingResult result, Map model) { if (result.hasErrors()) { return "loginform"; } /* String userName = "UserName"; String password = "password"; loginForm = (LoginForm) model.get("loginForm"); if (!loginForm.getUserName().equals(userName) || !loginForm.getPassword().equals(password)) { return "loginform"; } */ boolean userExists = loginService.checkLogin(loginForm.getUserName(),loginForm.getPassword()); if(userExists){ model.put("loginForm", loginForm); return "loginsuccess"; }else{ result.rejectValue("userName","invaliduser"); return "loginform"; } }
}
DAO
package com.pizza.dao; import com.pizza.model.Users;
public interface LoginDAO{
public boolean checkLogin(String userName, String userPassword);
}
DAO Impl
package com.pizza.dao; import com.pizza.model.Users;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Query; import java.util.List;
@Repository("loginDAO") public class LoginDAOImpl implements LoginDAO{
@Resource(name="sessionFactory") protected SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } protected Session getSession(){ return sessionFactory.openSession(); } @Override public boolean checkLogin(String userName, String userPassword){ System.out.println("In Check login"); Session session = sessionFactory.openSession(); boolean userFound = false; //Query using Hibernate Query Language String SQL_QUERY =" from Users as o where o.userName=? and o.userPassword=?"; Query query = session.createQuery(SQL_QUERY); query.setParameter(0,userName); query.setParameter(1,userPassword); List list = query.list(); if ((list != null) && (list.size() > 0)) { userFound= true; } session.close(); return userFound; }
}
Service
package com.pizza.service; import com.pizza.model.Users;
public interface LoginService{
public boolean checkLogin(String userName, String userPassword);
}
package com.pizza.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import com.pizza.dao.*;
@Service("loginService") public class LoginServiceImpl implements LoginService {
@Autowired private LoginDAO loginDAO; public void setLoginDAO(LoginDAO loginDAO) { this.loginDAO = loginDAO; } public boolean checkLogin(String userName, String userPassword){ System.out.println("In Service class...Check Login"); return loginDAO.checkLogin(userName, userPassword); }
}
Model
package com.pizza.model;
import java.util.List; import java.io.Serializable; import java.sql.Timestamp;
import javax.persistence.Basic; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Column; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Transient; import javax.persistence.Embeddable;
@Entity @Table(name = "users") @SuppressWarnings("serial") public class Users implements Serializable {
@Id @GeneratedValue @Column(name = "id", length = 11 ) private Long id; @Column(name = "user_name") String userName; @Column(name = "user_password") String userPassword; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; }
}
application context
<!-- Base package for checking the annoted classes --> <context:component-scan base-package="com.pizza"></context:component-scan> <!-- Configure JDBC Connection--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/pizzaStore" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- Configure Hibernate 4 Session Facotry --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="annotatedClasses"> <list> <value>com.pizza.model.Users</value> <!-- Entity classes--> </list> </property> </bean>
Web XML
<display-name>My Pizza App</display-name> <servlet> <servlet-name>springMVCpizza-servletConfig</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVCpizza-servletConfig</servlet-name> <url-pattern>/forms/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>