B.K B.K - 7 months ago 13
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

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

}

Answer

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: