3

I have a spring project and my problem is that my model objects becomes null on jsp when I debug it in the controller it shows data but on jsp nothing is displayed and there are no errors in the log.

I have added spring tiles config recently and tiles are working fine but these objects stopped getting displayed earlier it was working fine.

I have searched a lot on this and checked same links like Spring Model Object is not rendering on stackoverflow but nothing worked.

Here is my controller:

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

spring-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..
Community
  • 1
  • 1
kirti
  • 4,499
  • 4
  • 31
  • 60

3 Answers3

3

Make it correct and change return type to 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 ;
}

OR use

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

Look at ModelAndView that accepts view name.

Braj
  • 46,415
  • 5
  • 60
  • 76
3

You can also try this interface Model for this issue:

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

Make sure that in your pom.xml the following dependency exist:

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



 <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>${spring.version}</version>
 </dependency>
Saif Alam
  • 51
  • 4
2

Do not reinitialize ModelAndView Object

    @RequestMapping(value = "/viewClient", method = RequestMethod.GET)
    public String viewClient(ModelAndView model) {
        ArrayList<SystemUser> systemUsers = clientController.getAllUsers();
        model.addObject("systemUsers", systemUsers);
        return "viewClient";
}
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62