从控制器传递到jsp时获取空对象

时间:2015-05-10 02:21:45

标签: jsp spring-mvc hibernate-4.x

将控件中的对象列表传递给jsp时出现问题。 这里是jsp页面

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Users</title>
</head>
 <body>
 <h1>User list</h1>

   <c:url var="editImgUrl" value="/edit.png" />
   <c:url var="deleteImgUrl" value="/delete.png" />

<table style="border: 1px solid; width: 100%; text-align:center">
    <thead style="background:#d3dce3">
        <tr>
            <th>Id</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>login</th>
            <th>service line</th>
            <th colspan="2"></th>
        </tr>
    </thead>
    <tbody style="background:#ccc">
    <c:forEach items="${userList}" var="user">
        <c:url var="editUrl" value="/edit?id=${user.iduser}" />
        <c:url var="deleteUrl" value="/delete?id=${user.iduser}" />

        <tr>
            <td>${user.iduser}</td>
            <td><c:out value="${user.firstName}" /></td>
            <td><c:out value="${user.lastName}" /></td>
            <td><c:out value="${user.login}" /></td>
            <td><c:out value="${user.serviceline}" /></td>

 <td><a href="edit?id=${user.id_user}"><img src="${editImgUrl}"></img></a></td>  
  <td><a href="delete?id=${user.id_user}"><img src="${deleteImgUrl}"></img></a>        </td>  

   </tr>

  </c:forEach>
   </tbody>
  </table>

   <a href="list3">Click Here to see user List</a>  
 </body>
</html>

这是我的控制器:

    package test3.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import test3.model.entities.User;
import test3.service.UserService;

@Controller
public class UserController {

    @Autowired  
     public UserService userService; 

     @RequestMapping("form")  
     public String viewRegistration(Map<String, Object> model) {
            User user = new User();    
            model.put("user", user);

          /**  List<String> servicelineList = new ArrayList<String>();
            servicelineList.add("ADM");
            servicelineList.add("PBS");
            servicelineList.add("CSD");
            model.put("servicelineList", servicelineList);**/

            return "form";
     }  

     @RequestMapping("register")  
     public ModelAndView registerUser(@ModelAttribute User user) {  
      userService.add(user);
      return new ModelAndView("redirect:list");  
     }  



     @RequestMapping(value="list", method = RequestMethod.GET)  
     public ModelAndView getAll(){ 
         ModelAndView model=new  ModelAndView("list");
     try {
        model.addObject("userList",userService.getAll());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  
      return model;
     }  

     public String getUser(Map<String, Object> model) {
            User user = new User();    
            model.put("user", user);

          /**  List<String> servicelineList = new ArrayList<String>();
            servicelineList.add("ADM");
            servicelineList.add("PBS");
            servicelineList.add("CSD");
            model.put("servicelineList", servicelineList);**/

            return "form";
     }  

}

这是我得到的观点:

包含以下值的表:$ user.iduser,$ user.lastname ...(我无法发布图片)

我认为查询的结果是空的,但我试图传递一个字符串列表,我遇到了同样的问题。

谢谢你的帮助

1 个答案:

答案 0 :(得分:3)

我通过添加

解决了这个问题
<%@ page isELIgnored="false" %>

到jsp文件。