0

我在 Spring MVC 应用程序中面临一种情况,我有一个所有页面通用的表单。因此,我在一个单独的 jsp 页面中采用了该表单,并将该页面包含在其他 jsp 页面中。形式很简单:

   <form:form method="POST" commandName="coachingDomain" action="searchForm">

    <form:input path="coachingName" placeholder="Search for coaching" />

    <button type="submit" value="Search">Search</button>
</form:form>    

现在的场景是当我运行应用程序时,服务器无法在控制器中找到coachingDomain的绑定。为了更清楚起见,假设表单存在于页面 A1 中并包含在页面 A2 中。在 A2 控制器中,我绑定了 coachingDomain,如下所示:

  @RequestMapping(method = RequestMethod.GET)
public String showPage(ModelMap modelMap, HttpServletRequest request,
        HttpServletResponse response) {
    modelMap.addAttribute("coachingDomain",
            new CoachingDomain());
    return "welcome";
}

但是仍然出现绑定错误。在我看来,服务器首先试图找到 A1 页面的控制器。所以,我创建了 A1 控制器(作为抽象)并使 A2 扩展 A1 控制器。然后,我在 A1 中放置了相同的代码。但仍然存在同样的错误。

我的 A1 和 A2 控制器是:

   public abstract class A1 {

public void showPage1(ModelMap modelMap, HttpServletRequest request,
        HttpServletResponse response) {
    System.out.println("A1 called");
    modelMap.addAttribute("coachingDomain",
            new CoachingDomain());
     }
 }

  @Controller
  @RequestMapping(value = "/A2.html")
 public class A2 extends A1 {

@RequestMapping(method = RequestMethod.GET)
public String showPage(ModelMap modelMap, HttpServletRequest request,
        HttpServletResponse response) {
    System.out.println("A2 called");
            showPage1(modelMap, request, response);
    modelMap.addAttribute("searchCoachingDomain",
            new SearchCoachingDomain());
    return "A2";
     }
   }

我的jsp页面是:

A1.jsp:

    <body>
   <form:form method="POST" commandName="coachingDomain" action="searchForm">

    <form:input path="coachingName" placeholder="Search for coaching" />

    <button type="submit" value="Search">Search</button>
</form:form>    
  </body>

A2.jsp:

<%@include file="/WEB-INF/views/root.jsp"%>

错误是:

       Neither BindingResult nor plain target object for bean name 'coachingDomain' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)

互联网上给出了继承示例,但我无法说明如何制作所有 jsp 页面通用的表单。我想到的一种方法可能是使用 Javascript,我将提交按钮与 JS 函数绑定并调用所需的 URL。但我希望是否可以通过使用 Spring MVC @RequestMapping(method = RequestMethod.POST) 技术来实现相同的目标。请建议我在这里...

4

0 回答 0