简体   繁体   中英

Controller of Spring MVC working but model is empty in JSP

Visit my previous question to see my configurations of spring mvc Spring MVC Controller working but not creating the specified response URL ,It is creating the url from request mapping string

Now am facing one strange error in jsp page

The Controller class method is

@RequestMapping(value="/branchshow.travel", method=RequestMethod.GET)
public String getBranchShow(){

    List<BranchModel> branches = branchService.getBranches();
    System.out.println(branches.size());
    new ModelMap().addAttribute("branches", branches);
    return "/branch/branchshow";

}

I got the requested jsp page but the model named 'branches' is empty in jsp. I got the size of list as 140 in console. So it means that returned model is empty in 'branchshow.jsp'

I checked many links, all are saying its because of jsp version but am using 3.0 my web.xml starting is

 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" 
version="3.0">

so the version is 3.0

I would like to show my branchshow.jsp code here

 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 <%@ page isELIgnored="false" %>
 <%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html lang="en">

 <head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>Service-Home</title>

<!-- resources linking -->
<spring:url value="/resources/js/login_forms.js" var="login_forms"/>
<spring:url value="/resources/js/jquery.js" var="jqueryjs"/>
<spring:url value="/resources/js/bootstrap.min.js" var="bootstrapminjs"/>
<spring:url value="/resources/css/bootstrap.min.css"  
var="bootstrapmincss"></spring:url>
<spring:url value="/resources/css/custom.css" var="customcss">  
</spring:url>
<spring:url value="/resources/css/simple-sidebar.css" var="sidebarcss">
</spring:url>


<spring:url value="/resources/images/toggle.png" var="hide"/>
<spring:url value="/resources/images/insert.png" var="insert"/>
<spring:url value="/resources/images/edit.png" var="update"/>
<spring:url value="/resources/images/delete.png" var="delete"/>
<spring:url value="/resources/images/search.png" var="search"/>

<!-- Custom CSS -->
<link  rel="stylesheet" href="${bootstrapmincss}">
<link  rel="stylesheet" href="${sidebarcss}">
<link  rel="stylesheet" href="${customcss}">

</head>

<body>

<div id="wrapper">

    <!-- Sidebar -->
    <div id="sidebar-wrapper">
        <ul class="sidebar-nav">
            <li>
                    <font color="WHITE" style="font-style:bold">CALICUT TRAVELS-SERVICES</font>
            </li>
            <li>
                <a href="#">Master</a>
                    <ul>
                        <li><a href="branchinsert.travel">Branch</a></li>
                        <li><a href="../hotel/branchinsert.travel">Hotel</a></li>
                        <li><a href="../airline/branchinsert.travel">Air line</a></li>
                        <li><a href="../customers/branchinsert.travel">Customers</a></li>
                        <li><a href="../staff/branchinsert.travel">Staff</a></li>
                        <li><a href="../suppliers/branchinsert.travel">Suppliers</a></li>
                    </ul>
            </li>
            <li>
                <a href="#">Packages</a>
                    <ul>
                        <li><a href="#">Airline</a></li>
                        <li><a href="#">Hotel</a></li>
                        <li><a href="#">Transportation</a></li>
                    </ul>
            </li>
            <li>
                <a href="#">Sales Invoices</a>
                    <ul>
                        <li><a href="#">Book rooms</a></li>
                        <li><a href="#">Book tickets</a></li>
                        <li><a href="#">Book visa</a></li>
                        <li><a href="#">Book package</a></li>
                    </ul>
            </li>
            <li>
                <a href="#">Sales Returns</a>
                    <ul>
                        <li><a href="#">Cancel rooms</a></li>
                        <li><a href="#">Cancel tickets</a></li>
                        <li><a href="#">Cancel visa</a></li>
                        <li><a href="#">Cancel Package</a></li>
                    </ul>
            </li>
            <li>
                <a href="#">Miscellaneous</a>
                    <ul>
                        <li><a href="#">Taxi</a></li>
                        <li><a href="#">Food</a></li>
                    </ul>
            </li>
            <li>
                <a href="#">Purchase</a>
                    <ul>
                        <li><a href="#">Airline</a></li>
                        <li><a href="#">Hotel</a></li>
                        <li><a href="#">Visa</a></li>
                        <li><a href="#">Transportation</a></li>
                        <li><a href="#">Packages</a></li>
                    </ul>
            </li>
            <li>
                <a href="#">Purchase Returns</a>
                    <ul>
                        <li><a href="#">Airline</a></li>
                        <li><a href="#">Hotel</a></li>
                        <li><a href="#">Visa</a></li>
                        <li><a href="#">Transportation</a></li>
                        <li><a href="#">Package</a></li>
                    </ul>
            </li>
            <li>
                <a href="#">Reports</a>
                    <ul>
                        <li><a href="#">Sales invoices</a></li>
                        <li><a href="#">sales returns</a></li>
                        <li><a href="#">purchase invoices</a></li>
                        <li><a href="#">purchase returns</a></li>
                        <li><a href="#">packages</a></li>
                        <li><a href="#">Customer summery report</a></li>
                    </ul>
            </li>
        </ul>
    </div>
    <!-- /#sidebar-wrapper -->

    <!-- Page Content -->
    <div id="service-home-conetent">
        <div class="container-fluid">
            <div class="row">
                    <table>
                       <tr>
                        <th align="center" width='20'><a href="#menu-toggle" id="menu-toggle"><img alt="HIDE" src="${hide}" width='20'></a></th>
                        <th align="center" width="400" colspan="4"><h2>BRANCH</h2></th>
                        </tr>
                        <tr align="center">
                        <th align="center" width='200'></th>
                        <th align="center" width='20'><a href="branchinsert.travel"><img src="${insert}" width='20'></a></th>

                        <th align="center" width='20'><a href="branchupdate.travel"><img src="${update}" width='20'></a></th>

                        <th align="center" width='20'><a href="branchdelete.travel"><img src="${delete}" width='20'></a></th>

                        <th align="center" width='20'><a href="branchshow.travel"><img src="${search}" width='20'></a></th>

                        </tr>

                        <tr>
                         <th align="center" width='200'></th>
                         <th align="center" width='20'>Insert</th>
                         <th align="center" width='20'>update</th>
                         <th align="center" width='20'>Delete</th>
                         <th align="center" width='20'>Search</th>
                        </tr>
                    </table>

        <input type="hidden" id='form_use' value='view'/>

            <fieldset class="well the-fieldset">
                <legend class="the-legend">  BRANCHES </legend>

                    <table>
                        <tr>
                            <th> Branch Name</th>
                        </tr>
                        <c:forEach var="branch" items="${branches}">

                        <tr>
                            <td>${branch.branchName}</td>
                        </tr>
                        </c:forEach>
                    </table>

             </fieldset>


            </div>
        </div>
    </div>
    <!-- /#page-content-wrapper -->

</div>
<!-- /#wrapper -->

<!-- jQuery -->
<script src="${jqueryjs}"></script>

<!-- Bootstrap Core JavaScript -->
<script src="${bootstrapminjs}"></script>
<script src="${login_forms}"></script>
<!-- Menu Toggle Script -->
<script>
$("#menu-toggle").click(function(e) {
    e.preventDefault();
    $("#wrapper").toggleClass("toggled");
});
</script>

I enabled EL in jsp then also this issue persists. If anybody faced this issue before let me know the solution

On the line you create a new ModelMap but you don't return it

new ModelMap().addAttribute("branches", branches);

You method could return a ModelAndView object to store your model attribute 'branches' and view '/branch/branchshow'

@RequestMapping(value="/branchshow.travel", method=RequestMethod.GET)
public ModelAndView getBranchShow() {
    ModelAndView mav = new ModelAndView();
    List<BranchModel> branches = branchService.getBranches();
    System.out.println(branches.size());
    mav.getModelMap().put("branches", branches);
    mav.setViewName("/branch/branchshow");
    return mav;

}

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