从控制器传递到 jsp 时得到空对象
getting empty objects while passing from controller to jsp
我在将对象列表从控制器传递到 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";
}
}
这是我得到的视图:
a table 具有这些值:$user.iduser、$user.lastname ...(我无法 post 图片)
我以为查询的结果是空的,但我试图传递一个字符串列表,但我遇到了同样的问题。
谢谢你的帮助
好吧,我通过添加
解决了这个问题
<%@ page isELIgnored="false" %>
到 jsp 文件。
我在将对象列表从控制器传递到 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";
}
}
这是我得到的视图:
a table 具有这些值:$user.iduser、$user.lastname ...(我无法 post 图片)
我以为查询的结果是空的,但我试图传递一个字符串列表,但我遇到了同样的问题。
谢谢你的帮助
好吧,我通过添加
解决了这个问题<%@ page isELIgnored="false" %>
到 jsp 文件。