如何在JSP中显示对象的数据 [英] How to display data of objects in JSP

查看:191
本文介绍了如何在JSP中显示对象的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过寄存器形式将一些用户详细信息存储到db(hibernate和spring)中。我想在单独的JSP页面中显示所有用户的用户详细信息。请有人告诉我该怎么做?



以下是我的控制器代码

  @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);
返回新的ModelAndView(registerForm,command,user);


$ b @RequestMapping(value =/registerProcess.htm,method = RequestMethod.POST)
public ModelAndView registerUser(@ModelAttribute(user)用户用户,模型模型){

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);
返回新的ModelAndView(registerProcess,user,user);

}

}

userdao

  public void saveUser(User user){

Session session = getSessionFactory()。openSession() ;
交易tx;
tx = session.beginTransaction();

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



解决方案

在GET请求中获取要显示给用户的元素。这涉及以下步骤:


  • 具有适当的URL映射和视图来处理GET。

  • 获取将预处理您的URL的方法中的数据。

  • 存储数据以作为请求属性显示给用户。

  • 转发到查看(JSP)。
  • 在视图中显示来自请求属性的数据。



A非常简单的例子基于你当前的代码,并假设存在一些方法:

  @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);
返回新的ModelAndView(registerForm,command,user);
}

@RequestMapping(value =/ registerProcess.htm,method = RequestMethod.POST)
public ModelAndView registerUser(@ModelAttribute(user)User user,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);
返回新的ModelAndView(registerProcess,user,user);
}

//这是具有适当映射的新方法
@RequestMapping(value =/ userList.htm,method = RequestMethod.GET)
public ModelAndView registerPage(ModelMap map){
//此方法应检索所有用户的数据
List< User> userList = userDao.getAllUsers();
map.addAttribute(userList,userList);
返回新的ModelAndView(userList,map);


然后,在userList.jsp中:

 <%@ taglib prefix =curi =http://java.sun.com/ jsp / jstl / core%> 
<!DOCTYPE html>
< html lang =en>
< head>
< title>使用者名单< / title>
< / head>
< body>
用户名单:
< br />
< table>
< c:forEach items =$ {userList}var =user>
< tr>
< td> $ {user.userName}< / user>
< / tr>
< / c:forEach>
< / table>
< / body>
< / html>

请注意,这是关于如何执行此操作的一个非常基本的示例。

更多信息:




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

}

解决方案

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:

这篇关于如何在JSP中显示对象的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆