above is my Spring:4 MVC application with Hibernate integration.
from index page user goes to findUser.jsp by the following controller:
@RequestMapping(value = "/findUser", method = RequestMethod.GET)
public String findUserByName(@ModelAttribute("user") User user, BindingResult result, Model model) {
model.addAttribute("user", user);
return "findUser";
}
the findUser.jsp is as follow:
<body>
<form:form action="UserByName" method="post" modelAttribute="user">
<table>
<tr>
<td><form:label path="id"> ID </form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</form:form>
</body>
when the user enter and ID and hit the submit button it hits the controoler :
@RequestMapping(value = "/UserByName", method = RequestMethod.POST)
public String UserByName(@PathVariable("id") int id, Model model) {
model.addAttribute("user", this.userService.findByName(id));
return "usersByName";
}
and UserService impl is
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
public boolean save(User user) {
return userDao.save(user);
}
public int update(User user) {
return userDao.update(user);
}
@Transactional
public User findByName(int id) {
return this.userDao.findByName(id);
}
}
the method of userDao in userDaoImpl is:
public User findByName(int id) {
//User user = new User();
Session session = this.sessionFactory.getCurrentSession();
User u = (User) session.get(User.class, new Integer(id));
return u;
}
finally the controller display the usersByName.jsp which is:
<body>
<form:form modelAttribute="user">
<c:forEach items="${user}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${useremail}</td>
</tr>
</c:forEach>
</form:form>
</body>
am using Hibername and mySQL databse my model class is:
@Entity@Table(name = "USER")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "NAME")
private String name;
@Column(name = "PASSWORD")
private String password;
@Column(name = "USER_ID")
private String userId;
@Column(name = "EMAIL")
private String email;
@Column(name = "ROLE")
private String role;
@Column(name = "DEPARTMENT")
private String department;
ApplicationContext is
@ Configuration
@ComponentScan("com.sat.spring")
@EnableTransactionManagement
public class A
pplicationContextConfig {
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean(name = "dataSource")
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/UniversityProject");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
return properties;
}
@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
sessionBuilder.addProperties(getHibernateProperties());
sessionBuilder.addAnnotatedClasses(User.class);
return sessionBuilder.buildSessionFactory();
}
@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
@Autowired
@Bean(name = "userDao")
public UserDao getUserDao(SessionFactory sessionFactory) {
return new UserDaoImpl(sessionFactory);
}
}
what I am doing wrong?? I want the usersByName.jsp to display all user's info for that specific id from the USER mySQL database.