Spring 模型对象在 jsp 上变为空
Spring model object becomes null on jsp
我有一个 spring 项目,我的问题是我的模型对象在 jsp 上变为空,当我在控制器中调试它时它显示数据,但在 jsp 上没有任何显示并且日志中没有错误。
我最近添加了 spring 磁贴配置,磁贴工作正常,但这些对象早些时候停止显示它工作正常。
我对此进行了很多搜索,并在 Whosebug 上检查了与 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";
}
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..
修改 return 类型为 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。
不要重新初始化 ModelAndView
对象
@RequestMapping(value = "/viewClient", method = RequestMethod.GET)
public String viewClient(ModelAndView model) {
ArrayList<SystemUser> systemUsers = clientController.getAllUsers();
model.addObject("systemUsers", systemUsers);
return "viewClient";
}
这个问题你也可以试试这个接口模型:
@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>
我有一个 spring 项目,我的问题是我的模型对象在 jsp 上变为空,当我在控制器中调试它时它显示数据,但在 jsp 上没有任何显示并且日志中没有错误。
我最近添加了 spring 磁贴配置,磁贴工作正常,但这些对象早些时候停止显示它工作正常。
我对此进行了很多搜索,并在 Whosebug 上检查了与 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";
}
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..
修改 return 类型为 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。
不要重新初始化 ModelAndView
对象
@RequestMapping(value = "/viewClient", method = RequestMethod.GET)
public String viewClient(ModelAndView model) {
ArrayList<SystemUser> systemUsers = clientController.getAllUsers();
model.addObject("systemUsers", systemUsers);
return "viewClient";
}
这个问题你也可以试试这个接口模型:
@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>