我使用 JQuery 数据表 + Spring + DB 构建了一个简单的应用程序来显示一些数据。数据显示为表格。
我最初构建了后端模型+数据库(MongoDB)+ View ,其中两个属性显示良好。
但是现在,当我向所有层添加更多属性时,JSP 不会仅向我显示新添加属性的值。
我已检查我的 Controller 是否正在模型中发送正确的值作为对 JSP 的响应。 JSP 内的 JSTL 标记似乎没有显示正确的值。
我的模型中有 5 个属性。
姓名、年份、导演、评级、排名。
之前添加的名称和年份工作正常。
导演、评分和排名是稍后添加的,不起作用。下面我给出了我的模型类的代码。
下面是我的jSP的代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>My Movie Manager</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="main.css" />
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.css">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.js"></script>
</head>
<body>
<h1>Welcome to My movie manager</h1>
<h3>My ranking of movies</h3>
<input type="button" value="Add a new movie" id="addbtn" />
<input type="button" value="Select and remove a movie" id="delbtn" />
<br><br>
<table id="table_id" class="display">
<thead>
<tr>
<th>My Ranking</th>
<th>Name</th>
<th>Year</th>
<th>Rating</th>
<th>Director</th>
</tr>
</thead>
<tbody>
<c:forEach items="${movieslist}" var="movie">
<tr>
<td>$(movie.ranking)</td>
<td>${movie.name}</td>
<td>${movie.year}</td>
<td>$(movie.rating)</td>
<td>$(movie.director)</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
<script>
$(document).ready( function () {
var table = $('#table_id').DataTable();
$('#addbtn').click(addrow);
$('#delbtn').click(delrow);
table.on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
}
else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
} );
} );
</script>
</html>
下面是我的 Spring Controller
@Controller
@RequestMapping("/movies")
public class MovieController{
protected final Log logger = LogFactory.getLog(getClass());
@RequestMapping(method = RequestMethod.GET)
public String getMovies(Model model) {
// TODO Auto-generated method stub
logger.info("returning hello view");
List<Movies> moviesList = DbManager.getInstance().getMovies();
model.addAttribute("movieslist", moviesList);
return "hello";
}
}
下面是我的域类
package springapp.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "movies")
public class Movies {
@Id
public String _id;
public String name;
public String year;
public String director;
public float rating;
public int ranking;
public int getRanking() {
return ranking;
}
public void setRanking(int ranking) {
this.ranking = ranking;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public float getRating() {
return rating;
}
public void setRating(float rating) {
this.rating = rating;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getId() {
return _id;
}
public void setId(String _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return name;
}
}
最佳答案
请按如下方式更正:
使用${}而不是$()。应该是
<td>${movie.myranking}</td> <td>${movie.name}</td> <td>${movie.year}</td> <td>${movie.rating}</td> <td>${movie.director}</td> </tr> </tbody>
关于java - 一些 Spring 模型属性未显示在我的 JSP 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41650781/