【问题标题】:Some of Spring model attributes are not displayed in my JSP我的 JSP 中没有显示一些 Spring 模型属性
【发布时间】:2017-01-14 13:57:16
【问题描述】:

我已经构建了一个简单的应用程序来使用 JQuery 数据表 + Spring + DB 显示一些数据。数据显示为表格。

我最初构建了后端模型 + 数据库 (MongoDB) + 视图,其中两个属性显示良好。

但是现在当我向我的所有层添加更多属性时,JSP 不会只显示新添加的属性的值。

我已检查我的控制器是否在模型中发送正确的值作为对我的 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
@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;
    }

}

【问题讨论】:

标签: java spring jsp spring-mvc jstl


【解决方案1】:

请更正如下:

  1. 使用 ${} 不使用 $()。应该是

                <td>${movie.myranking}</td>
                <td>${movie.name}</td>
                <td>${movie.year}</td>
                <td>${movie.rating}</td>
                <td>${movie.director}</td>
            </tr>
    
    
        </tbody>
    

【讨论】:

  • 唷...这是有史以来最愚蠢的错误之一 :) ...非常感谢 Barath
猜你喜欢
  • 2013-03-23
  • 2013-06-01
  • 2013-08-27
  • 1970-01-01
  • 2012-03-13
  • 2017-07-23
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
相关资源
最近更新 更多