Spring模型对象在jsp上变为null

时间:2015-04-13 10:15:00

标签: java jsp spring-mvc tiles2

我有一个spring项目,我的问题是当我在控制器中调试它时,我的模型对象在jsp上变为null它显示数据但在jsp上没有显示任何内容,并且日志中没有错误。

我最近添加了spring tiles配置并且tile工作正常但是这些对象早先停止显示它工作正常。

我已经对此进行了大量搜索,并在stackoverflow上检查了Spring Model Object is not rendering之类的相同链接,但没有任何效果。

这是我的控制器:

@RequestMapping(value = "/viewClient", method = RequestMethod.GET)
public String viewClient(ModelAndView model) {
    ArrayList<SystemUser> systemUsers = clientController.getAllUsers();
    model = new ModelAndView("systemUsers");
    model.addObject("systemUsers", systemUsers);
    return "viewClient";
}

弹簧-config.xml中

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">

    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />

    <property name="order">
        <value>0</value>
    </property>

</bean>

<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">

    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>

</bean>

tiles.xml

<tiles-definitions>
    <definition name="base.definition" template="/WEB-INF/pages/Theme/Template/mainTemplate.jsp">
        <put-attribute name="title" value=""></put-attribute>
        <put-attribute name="header" value="/WEB-INF/pages/Theme/Template/header.jsp"></put-attribute>
        <put-attribute name="menu" value="/WEB-INF/pages/Theme/Template/menu.jsp"></put-attribute>
        <put-attribute name="body" value=""></put-attribute>
        <!-- <put-attribute name="footer" value="/WEB-INF/pages/Theme/Template/footer.jsp"></put-attribute> -->
    </definition>

    <definition name="base.definition.login" template="/WEB-INF/pages/Theme/Template/loginTemplate.jsp">
        <put-attribute name="title" value=""></put-attribute>

        <put-attribute name="body" value=""></put-attribute>
    </definition>

    <definition extends="base.definition" name="viewClient">
        <put-attribute name="title" value="View Client"></put-attribute>
        <put-attribute name="body" value="/WEB-INF/pages/Theme/Admin/viewClient.jsp"></put-attribute>
    </definition>
<tiles-definitions>

viewClient.jsp:

<table class="table table-striped table-advance table-hover">
    <h4>
    <!-- <i class="fa fa-angle-right"></i> --> Client Details
    </h4>
    <thead>
        <tr>
            <th><i class="fa fa-bullhorn"></i> index --${systemUsers.size()}--size </th>
            <th><i class="fa fa-bullhorn"></i> Username</th>
            <th><i class="fa fa-bullhorn"></i> Password</th>
            <th><i class="fa fa-bullhorn"></i> User role</th>
            <th><i class="fa fa-bullhorn"></i> Mobile No</th>

            <th><i class="fa fa-bullhorn"></i> Email ID</th>
            <th><i class="fa fa-bullhorn"></i> SMSC</th>
            <th><i class="fa fa-bullhorn"></i> Virtual Sim ID</th>

            <th></th>
        </tr>
    </thead>
    <tbody>
        <c:set var="count" value="0" scope="page" />
        <c:forEach var="user" items="${systemUsers}">
            <tr>
                <td>${count + 1}</td>
                <td>${ user.getUsername()}</td>
                <td>${ user.getPassword()}</td>
                <td>${user.getUserrole()}</td>
                <td>${user.getMobileNumber()}</td>
                <td>${user.getPrimaryEmailAddress()}</td>
                <td>${user.getSmsc()}</td>
                <td>${user.getVirtualSimID()}</td>
                <td>
                    <button class="btn btn-success btn-xs">
                        <i class="fa fa-check"></i>
                    </button>
                    <button class="btn btn-primary btn-xs">
                        <i class="fa fa-pencil"></i>
                    </button>
                    <button class="btn btn-danger btn-xs">
                        <i class="fa fa-trash-o "></i>
                    </button>
                </td>
            </tr>
            <c:set var="count" value="${count + 1}" scope="page" />
        </c:forEach>

    </tbody>
</table>

In the jsp the systemUsers are not getting iterated nor displayed not its size is getting displayed..

3 个答案:

答案 0 :(得分:3)

将其更正并将返回类型更改为ModelAndView

@RequestMapping(value = "/viewClient", method = RequestMethod.GET)
public ModelAndView viewClient() {
    ArrayList<SystemUser> systemUsers = clientController.getAllUsers();
    ModelAndView model = new ModelAndView("viewClient");
    model.addObject("systemUsers", systemUsers);
    return model ;
}

或使用

@RequestMapping(value = "/viewClient", method = RequestMethod.GET)
public String viewClient(ModelMap model) {
    ArrayList<SystemUser> systemUsers = clientController.getAllUsers();
    model.addAttribute("systemUsers", systemUsers);
    return "viewClient";
}

查看接受视图名称的ModelAndView

答案 1 :(得分:3)

您也可以针对此问题尝试此接口模型:

@RequestMapping(value = "/viewClient", method = RequestMethod.GET)
public String viewClient(Model model) {
    ArrayList<SystemUser> systemUsers = clientController.getAllUsers();
    model.addAttribute("systemUsers", systemUsers);
    return "viewClient";
}

确保在pom.xml中存在以下依赖项:

<properties>
    <spring.version>4.0.2.RELEASE</spring.version>
</properties>



 <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>${spring.version}</version>
 </dependency>

答案 2 :(得分:2)

不要重新初始化ModelAndView对象

    @RequestMapping(value = "/viewClient", method = RequestMethod.GET)
    public String viewClient(ModelAndView model) {
        ArrayList<SystemUser> systemUsers = clientController.getAllUsers();
        model.addObject("systemUsers", systemUsers);
        return "viewClient";
}