java JSP中如何显示对象的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25829989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:45:12  来源:igfitidea点击:

How to display data of objects in JSP

javaspringhibernatejsp

提问by B.K

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?

我通过注册表将一些用户详细信息存储到数据库(休眠和弹簧)中。我想在一个单独的 JSP 页面中显示所有用户的用户详细信息。有人能告诉我怎么做吗?

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

userdao里面的代码

public void saveUser(User user) {

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

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

}

回答by Luiggi Mendoza

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

您应该获取要在 GET 请求中向用户显示的元素。这包括以下步骤:

  • 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.
  • 拥有正确的 URL 映射和视图来处理 GET。
  • 在将预处理您的 URL 的方法中获取数据。
  • 存储数据以作为请求属性显示给用户。
  • 转发到视图 (JSP)。
  • 在视图中,显示来自请求属性的数据。

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:

然后,在 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:

更多信息:

回答by Chaitanya

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

编写另一种方法来获取所有用户,然后将检索到的用户列表存储在您的模型对象中,然后在您的 JSP 中使用 JSTL forEach 标记来显示用户,您可以使用此链接查看如何在 JSP 上使用JSTL forEach 循环:ForEach 循环中的 JSP 错误