0

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

@Controller
public class RegisterController {

    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/registerForm.htm", method = RequestMethod.GET)
    public ModelAndView registerPage(ModelMap map) {
        User user = new User();
        map.addAttribute(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);
        userDao.saveUser(user);
        return new ModelAndView("registerProcess", "user", user);

    }

}

code inside userdao

public void saveUser(User user) {

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

    session.persist(user);
    tx.commit();

}
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
bkuriach
  • 350
  • 2
  • 5
  • 17

2 Answers2

2

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:

@Controller
public class RegisterController {

    @Autowired
    private UserDao userDao;

    @RequestMapping(value="/registerForm.htm",method=RequestMethod.GET)
    public ModelAndView registerPage(ModelMap map){
        User user=new User();
         map.addAttribute(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);
        userDao.saveUser(user);
        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">
<head>
    <title>User List</title>
</head>
<body>
    List of users:
    <br />
    <table>
        <c:forEach items="${userList}" var="user">
            <tr>
                <td>${user.userName}</user>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

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

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Hi Luiggi, It worked. Could you please explain the working or use of map.addAttribute("userList", userList);? Is it a temporary storage during execution? previously, while returning i used 3 parameters but in the one you mentioned there are only 2. if i return like this, how will it work in the view page? – bkuriach Sep 14 '14 at 04:57
  • @B.K this is covered here: [Passing data to jsp:include via c:set](http://stackoverflow.com/q/16619015/1065197). Also, what I set as request attribute is the `List`. You can set as many variables you need. But if the data is already stored in an object, then store the object rather than store the data separately. For the rest, read [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197) – Luiggi Mendoza Sep 14 '14 at 05:00
0

Write another method to get all the users and then store the list of retrieved users in your model object then use the JSTL forEach tag in your JSP to display the users, you can use this link to see how the data can be displayed on JSP using JSTL forEach loop: JSP Errors in ForEach Loop

Community
  • 1
  • 1
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
  • once i retrieve all the users, should i store those info in a separate model object? I am using a model object 'User' to store the details once user registers. – bkuriach Sep 14 '14 at 04:23
  • you can use User, the data should be stored in a list. See @Luiggi Mendoza answer, it has complete information – Chaitanya Sep 14 '14 at 04:44