Spring JSP视图有2个表单 - 每个表单都有不同的控制器

时间:2016-07-25 11:54:21

标签: java spring jsp spring-mvc view

我有JSP视图和2个表单,我有2个控制器(1 - UserController,2 - Home Controller)

UserController - 管理网站的所有登录和重新显示 Home Controller - 管理所有用户操作

  • 当我尝试在一个控制器中工作时,它可以工作,但当我将方法拆分为特定控制器(HomeController)时,我有错误

为什么? *从 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

1 个答案:

答案 0 :(得分:1)

经过更多细节后,更容易理解发生了什么 - 即使仍然存在黑暗面,比如你第一次展示home.jsp。但我们在这里:

  • 您设法显示home.jsp(如何?)
  • 点击Test按钮
  • 浏览器向/scanTest
  • 处理的HomeController.scanRequest发送帖子请求
  • 控制器在模型中找到根据POSTed参数构建的属性scanTestForm,添加msg属性并转发该属性以查看home(我假设它是home.jsp
  • tomcat启动home.jsp以使用scanTestFormmsg构建响应作为请求属性 - 但没有scanForm,因为控制器尚未将其添加到模型中...
  • <form:form ... modelAttribute="scanForm" ...>在请求中查找scanForm属性,找不到并引发错误

如何解决:

  • 快速而又脏:只需在控制器中添加model.addAttribute("scanForm", userRequestDTO);即可。它将由JSP找到并且应该足以超越此错误
  • 更正确:不是仅仅转发到帖子后的视图,而是重定向到您第一次用来显示home.jsp的控制器。这是post-redirect-get模式。您甚至可以使用redirectAttributes
  • 将模型属性直接传递给其他控制器

但无论如何,如果他们在响应构建时应使用相同的值,我无法理解为什么你在同一页面中使用2个不同的模型属性名称。因为modelAttribute名称仅在此时使用,当JSP构建响应时,而不是在浏览器发回POST请求时。