我有JSP视图和2个表单,我有2个控制器(1 - UserController,2 - Home Controller)
UserController - 管理网站的所有登录和重新显示 Home Controller - 管理所有用户操作
为什么? *从 UserController 调用主页,并将内部操作发送到 HomeController
更新
home.jsp - >需要签名到“HomeController”的表单 *它包括2个表格(1-“scanRequest”> UserController,2-“scanForm”> HomeController)
<form:form method="POST" action="${contextPath}/scanRequest" modelAttribute="scanForm" class="form-signin">
<h2 class="form-signin-heading">Create new scan: </h2>
<table>
<tr>
<td>
<spring:bind path="seller_name">
<div class="form-group ${status.error ? 'has-error' : ''}">
<form:input type="text" path="seller_name" class="form-control" placeholder="Seller Name" autofocus="true"></form:input>
<form:errors path="seller_name"></form:errors>
</div>
</spring:bind>
</td>
<td>
<input type="hidden" name="scanForm" value="${UserRequestDTO}" />
<button class="btn btn-lg btn-primary btn-block" type="submit">Scan</button>
</td>
</tr>
</table>
</form:form>
<form:form method="POST" action="${contextPath}/scanTest" modelAttribute="scanTestForm" class="form-signin">
<h3>${msg}</h3>
<input type="hidden" name="scanTestForm" value="${UserRequestDTO}" />
<button class="btn btn-lg btn-primary btn-block" type="submit">Test</button>
</form:form>
HomeController中:
@Controller
public class HomeController {
@ModelAttribute("scanTestForm")
public UserRequestDTO getScanForm(){
return new UserRequestDTO();
}
@RequestMapping(value = "/scanTest", method = RequestMethod.POST)
public String scanRequest(@ModelAttribute("scanTestForm")UserRequestDTO userRequestDTO, BindingResult bindingResult, Model model) {
String strMsg = "-----------scanTest---------- \r\n";
if (bindingResult.hasErrors()) {
return "home";
}
model.addAttribute("msg", strMsg);
return "home";
}
}
UserController中
@ModelAttribute("scanForm")
public UserRequestDTO getScanForm(){
return new UserRequestDTO();
}
@RequestMapping(value = "/scanRequest", method = RequestMethod.POST)
public String scanRequest(@ModelAttribute("scanForm")UserRequestDTO userRequestDTO, BindingResult bindingResult, Model model) {
logger.info("scanRequest():");
String strMsg = "---------------------- \r\n" + userRequestDTO.getSeller_name() + "\r\n";
// Checking if there is any errors with the seller
if (bindingResult.hasErrors()) {
return "home";
}
model.addAttribute("msg", strMsg);
return "home";
}
**我只是它的工作原理,之后我可以继续我的代码.. **更新:错误 - 当我点击“测试”按钮**
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
יול 26, 2016 2:34:44 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [searcher] in context with path [/Searcher] threw exception [An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 47
44: <table>
45: <tr>
46: <td>
47: <spring:bind path="seller_name">
48: <div class="form-group ${status.error ? 'has-error' : ''}">
49: <form:input type="text" path="seller_name" class="form-control" placeholder="Seller Name" autofocus="true"></form:input>
50: <form:errors path="seller_name"></form:errors>
Stacktrace:] with root cause
javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'scanForm' available as request attribute
at org.springframework.web.servlet.tags.BindTag.doStartTagInternal(BindTag.java:120)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAw
答案 0 :(得分:1)
经过更多细节后,更容易理解发生了什么 - 即使仍然存在黑暗面,比如你第一次展示home.jsp
。但我们在这里:
home.jsp
(如何?)Test
按钮/scanTest
HomeController.scanRequest
发送帖子请求
scanTestForm
,添加msg
属性并转发该属性以查看home
(我假设它是home.jsp
) scanTestForm
和msg
构建响应作为请求属性 - 但没有scanForm
,因为控制器尚未将其添加到模型中... <form:form ... modelAttribute="scanForm" ...>
在请求中查找scanForm
属性,找不到并引发错误如何解决:
model.addAttribute("scanForm", userRequestDTO);
即可。它将由JSP找到并且应该足以超越此错误home.jsp
的控制器。这是post-redirect-get模式。您甚至可以使用redirectAttributes
但无论如何,如果他们在响应构建时应使用相同的值,我无法理解为什么你在同一页面中使用2个不同的模型属性名称。因为modelAttribute名称仅在此时使用,当JSP构建响应时,而不是在浏览器发回POST请求时。