简体   繁体   中英

Some of Spring model attributes are not displayed in my JSP

I have built a simple application to display some data using JQuery Data table + Spring + DB. The data is shown as a table.

I had initially built my backend model + DB (MongoDB) + View with two attributes which are displayed fine.

But now when i add couple of attributes more to my all the layers, the JSP is not showing me only the values for the newly added attributes.

i have checked that my controller is sending the right values in the model as a response to my JSP. It seems the JSTL tags inside the JSP are not showing the right value.

I have 5 attributes in my model.

name, year, director, rating, ranking.

name and year were added before is working fine.

director, rating and ranking are added later and are not working. Below i have given the code for my model class.

Below is the code of my 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>

Below is my 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";
    }
}

Below is my domain class

 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;
    }

}

Please correct like below :

  1. use ${} no $(). it should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM