2016-07-25 64 views
0

我有JSP視圖和內部2種形式,我有2個控制器(1 - UserController中,2 - 主頁控制器)彈簧JSP視圖與2個表格 - 每形式不同勢控制器

UserController中 - 管理所有登錄和regestration到該網站 家庭控制器 - 管理所有的用戶行爲

  • 當我試圖在一個控制器開始工作它的工作原理馬麗娟,但是當我分割方法spesific控制器(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> 

HomeControll ER:

@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"; 
    } 

**我正是它的工作之後,我可以繼續我的代碼.. **更新:錯誤 - 當我cliced上「測試「按鈕**

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

*不起作用*或*我開始出現錯誤*是無奈。如果可以的話,準確地說出是什麼錯誤並給出堆棧跟蹤。 –

+0

@SergeBallesta我現在加入了 – Guyb

+0

UserController是什麼樣的?我只看到一種形式。另一種形式在哪裏? – Tin

回答

1

瞭解更多詳情後,更容易瞭解發生了什麼 - 即使仍然存在黑暗邊比如你第一次顯示home.jsp。但是,我們在這裏:

  • 你設法顯示home.jsp(如何?)
  • 你點擊Test按鈕
  • 瀏覽器發送POST請求/scanTestHomeController.scanRequest
  • 控制器在模型中的發現處理屬性scanTestForm從POSTed參數構建,添加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請求時使用。

+0

非常感謝你這樣一個完整的答案。你正確..我試圖添加在相同的看法2行動不同控制器。我認爲你的第一個建議解決了我的問題。我不明白的是Post方法中的「redirectAttribute」 – Guyb