当前位置: 首页 > 知识库问答 >
问题:

Spring MVC的控制器工作正常,但JSP中的模型为空

宋华美
2023-03-14

访问我之前的问题看看我的配置 Spring mvc Spring MVC 控制器工作但没有创建指定的响应 URL,它是从请求映射字符串创建 url

现在在jsp页面中面临一个奇怪的错误

控制器类方法是

@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";

}

我获得了请求的jsp页面,但jsp中名为“branches”的模型为空。我在控制台中得到的列表大小为140。所以这意味着返回的模型在' branchshow.jsp '中是空的

我检查了很多链接,都说是因为jsp版本,但我使用的是3.0。xml起始为

 <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">

所以版本是3.0

我想在这里展示我的branchshow.jsp代码

 <%@ 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>

我在jsp中启用了EL,但这个问题仍然存在。如果之前有人遇到这个问题,请告诉我解决方案

共有1个答案

昝涛
2023-03-14

在行上,您创建了一个新的模型映射,但没有返回它

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

您的方法可以返回ModelAndView对象来存储模型属性“branchs”和视图“/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;

}
 类似资料: