B.K B.K - 2 years ago 73
Java Question

How to display data of objects in JSP

I have stored some user details through a register form into db (hibernate and spring). I want to display the user details of all users in a separate JSP page.Could anyone please tell me how to do that?

Below is my code of controller

public class RegisterController {

private UserDao userDao;

@RequestMapping(value = "/registerForm.htm", method = RequestMethod.GET)
public ModelAndView registerPage(ModelMap map) {
User user = new User();
return new ModelAndView("registerForm", "command", user);


@RequestMapping(value = "/registerProcess.htm", method = RequestMethod.POST)
public ModelAndView registerUser(@ModelAttribute("user") User user, Model model) {

model.addAttribute("userName", user.getUserName());
model.addAttribute("password", user.getPassword());
model.addAttribute("emailId", user.getEmailId());
System.out.println("user is " + user);
System.out.println("userdao is" + userDao);
return new ModelAndView("registerProcess", "user", user);



code inside userdao

public void saveUser(User user) {

Session session=getSessionFactory().openSession();
Transaction tx;



Answer Source

You should obtain the elements you want to show to user in a GET request. This involves the following steps:

  • Have a proper URL mapping and view to process the GET.
  • Obtain the data in the method that will pre process your URL.
  • Store the data to display to users as request attribute.
  • Forward to the view (JSP).
  • In view, display the data from request attributes.

A very simple example based on your current code and assuming the existence of some methods:

public class RegisterController {

    private UserDao userDao;

    public ModelAndView registerPage(ModelMap map){
        User user=new User();
        return new ModelAndView("registerForm","command",user); 

    public ModelAndView registerUser(@ModelAttribute("user") User user,Model model){
        model.addAttribute("userName", user.getUserName());
        model.addAttribute("password", user.getPassword());
        System.out.println("user is "+user);
        System.out.println("userdao is"+userDao);
        return new ModelAndView("registerProcess","user",user);

    //this is the new method with proper mapping
    @RequestMapping(value="/userList.htm", method=RequestMethod.GET)
    public ModelAndView registerPage(ModelMap map) {
        //this method should retrieve the data for all users
        List<User> userList = userDao.getAllUsers();
        map.addAttribute("userList", userList);
        return new ModelAndView("userList", map);

Then, in userList.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
    <title>User List</title>
    List of users:
    <br />
        <c:forEach items="${userList}" var="user">

Note that this is a very basic example about how to do this. The code can be heavily improved.

More info:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download