Spring MVC 中控制器的问题
我正在尝试创建一个jsp表单来输入用户信息。 我正在使用控制器来显示输入页面。 我已经使用 modelmap 添加了一个 User bean 对象。控制器返回 ModelandView 对象。 问题就出在这里,没有模型值返回到jsp页面。有人知道为什么会发生这种情况吗?请帮助我......
<context:annotation-config/>
<context:component-scan base-package="com.mobilize.rest"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<import resource="configuration/messageConverter.xml"/>
<import resource="configuration/viewResolveContext.xml"/>
下面的代码显示了用于获取视图AddUSer.jsp页面的控制器 调试时,值会正确存储在对象中。 但问题是表单页面没有收到该对象。
@RequestMapping(value = "/AddUser.html", method = RequestMethod.GET)
public ModelAndView addUser(
@RequestParam(value = "reload", required = false, defaultValue = "false")
Boolean reload,
@RequestParam(value = "reset", required = false, defaultValue = "false")
Boolean reset,HttpServletRequest request) {
HttpSession httpSession = request.getSession();
ModelMap modelmap = new ModelMap();
Users users = new Users();
modelmap.addAttribute("users", users);
modelmap.addAttribute("MOB_USER_ADD_STATUS_SUCCESS_MESSAGE","trial");
return new ModelAndView("AddUser", modelmap);
}
[已编辑,从评论部分移走附加代码]
<div id="content" style="height:508px"> <br/>
<c:if test="${MOB_USER_ADD_STATUS_SUCCESS_MESSAGE!=null}">
<div align="center"
id="statusIndicator"
class="successMessage">${MOB_USER_ADD_STATUS_SUCCESS_MESSAGE}
</div>
</c:if>
${MOB_USER_ADD_STATUS_SUCCESS_MESSAGE}
<form:form commandName="users"
method="POST"
action="AddUser.html">
<table>
<tr>
<td> User Name: </td>
<td><form:input path="userName"
id="username">
</form:input>
</td>
</tr>
<tr>
<td></td>
<td><form:errors path="userName"
cssClass="errorMessage">
</form:errors>
</td>
</tr>
<tr>
<td> Password: </td>
<td> <form:textarea path="password" id="password"></form:textarea> </td>
</tr>
<tr>
<td></td>
<td><form:errors path="password" cssClass="errorMessage"></form:errors></td>
</tr>
<tr>
<td></td>
<td> <input type="submit" name="offer" value=" Add "/>
<input type= "reset"
name="Reset"
value=" Reset "
onclick="document.location.href='AddUser.html?reset=true'"/>
</td>
</tr>
</table>
</form:form>
</div>
I am trying to create a jsp form for inputting the user information.
I am using a controller to display the input page.
I have added an User bean object with the modelmap.The controller returns the ModelandView object.
The problem lies here.No model value gets returned to the jsp page. Anybody know why this happens??please help me...
<context:annotation-config/>
<context:component-scan base-package="com.mobilize.rest"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<import resource="configuration/messageConverter.xml"/>
<import resource="configuration/viewResolveContext.xml"/>
The code below shows the controller used to get the view AddUSer.jsp page
While debugging the values are properly stored in the object.
But the problem is that the object doesnt get received at the form page.
@RequestMapping(value = "/AddUser.html", method = RequestMethod.GET)
public ModelAndView addUser(
@RequestParam(value = "reload", required = false, defaultValue = "false")
Boolean reload,
@RequestParam(value = "reset", required = false, defaultValue = "false")
Boolean reset,HttpServletRequest request) {
HttpSession httpSession = request.getSession();
ModelMap modelmap = new ModelMap();
Users users = new Users();
modelmap.addAttribute("users", users);
modelmap.addAttribute("MOB_USER_ADD_STATUS_SUCCESS_MESSAGE","trial");
return new ModelAndView("AddUser", modelmap);
}
[Edited, additional code moved from comment section]
<div id="content" style="height:508px"> <br/>
<c:if test="${MOB_USER_ADD_STATUS_SUCCESS_MESSAGE!=null}">
<div align="center"
id="statusIndicator"
class="successMessage">${MOB_USER_ADD_STATUS_SUCCESS_MESSAGE}
</div>
</c:if>
${MOB_USER_ADD_STATUS_SUCCESS_MESSAGE}
<form:form commandName="users"
method="POST"
action="AddUser.html">
<table>
<tr>
<td> User Name: </td>
<td><form:input path="userName"
id="username">
</form:input>
</td>
</tr>
<tr>
<td></td>
<td><form:errors path="userName"
cssClass="errorMessage">
</form:errors>
</td>
</tr>
<tr>
<td> Password: </td>
<td> <form:textarea path="password" id="password"></form:textarea> </td>
</tr>
<tr>
<td></td>
<td><form:errors path="password" cssClass="errorMessage"></form:errors></td>
</tr>
<tr>
<td></td>
<td> <input type="submit" name="offer" value=" Add "/>
<input type= "reset"
name="Reset"
value=" Reset "
onclick="document.location.href='AddUser.html?reset=true'"/>
</td>
</tr>
</table>
</form:form>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误是我导入了错误的类。我没有导入 org.springframework.web.servlet.ModelAndView ,而是导入了
org.springframework.web.portlet.ModelAndView。这就是错误的原因。我应该更小心一点。
The mistake was i imported the wrong class. Instead of importing org.springframework.web.servlet.ModelAndView , i imported
org.springframework.web.portlet.ModelAndView. That was the reason for the error. I should have been a bit more careful.