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();

}
3
  • 1
    And the problem is... Commented Sep 14, 2014 at 4:11
  • I am new to this. Do i need to save the objects differently? i need to display the data stored in the object in another page.What should be done for that? Commented Sep 14, 2014 at 4:15
  • You need not save the objects differently, you can find many examples in internet for this requirement, have you tried any? Commented Sep 14, 2014 at 4:20

2 Answers 2

3

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:

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@B.K this is covered here: Passing data to jsp:include via c:set. 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?
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

2 Comments

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.
you can use User, the data should be stored in a list. See @Luiggi Mendoza answer, it has complete information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.