2015-10-04 52 views
1
I am new to spring and I am facing the following issue: 

編輯說明:Spring MVC中無法找到在控制器方法

我能夠做一個新的控制器和ModelAndView的方法做出POST調用,但現在我面臨着數據的一個多問題沒有從JSP傳遞到控制器。 我的JSP代碼:

form:form method="POST" action="UserOperation" 
     modelAttribute="DebitModel"> 
     <h3>Debit</h3> 

        <c:if test="${!empty listAccounts}"> 
         <select name="item" id="account_dropdown" 
          onchange="changeAccountDetails(this.options[this.selectedIndex].value)"> 
          <c:forEach items="${listAccounts}" var="account"> 
           <option 
            value="${account.accID},${account.acctype},${account.balance}">AccID: 
            ${account.accID} Balance: ${account.balance}</option> 
          </c:forEach> 
         </select> 
         <br> 
         <br> 
         <label ><b>Account ID: </b></label> 
         <label id="accIdText" path="accID">${listAccounts[0].accID}</label> 
         <label><b>Account Type: </b></label> 
         <label id="accTypeText" path="acctype">${listAccounts[0].acctype}</label> 
         <label><b>Balance: </b></label> 
         <label id="accBalText" path="balance">${listAccounts[0].balance}</label> 
        </c:if> 
        <br> <br> Enter Amount<input type="text" 
         id="amount" width="80" path="amount"></input> <br> <br> 
     <input type="submit" value="Debit"></input> 
    </form:form> 

有一個名爲DebitModel正常的bean類。 我試圖搜索,並在許多地方的數據沒有得到通過,並找到解決方案作爲形式:輸入提到,但如果我使用這個,我沒有得到綁定FoundException'java.lang.IllegalStateException:無論是BindingResult還是普通的目標對象的bean名' DebitModel'作爲請求屬性 '提供,即使存在<%@ taglib uri =「http://www.springframework.org/tags/form」prefix =「form」%>。

The Controller class Code: 

@Controller 
@RequestMapping(value = "/UserOperation") 
public class DebitController { 
    @Autowired 
    private AccountService mAccountService; 

    @RequestMapping(method = RequestMethod.POST) 
    public ModelAndView registerCustomer(@ModelAttribute("DebitModel") DebitModel debitModel, BindingResult result, 
      HttpServletRequest request) throws NoSuchAlgorithmException, FileNotFoundException { 

     ModelAndView modelAndView = new ModelAndView(); 
     System.out.println("data------------------------" + debitModel.toString()); 
     return modelAndView; 
    } 

} 
+0

郵報工作的GET方法。 – chrylis

+0

這只是一個建議,你可以嘗試使用(at)RequestBody而不是(at)ModelAttribute。 http://stackoverflow.com/questions/21824012/spring-modelattribute-vs-requestbody。 @ a007 –

+0

顯示你的彈簧mvc配置。 –

回答

0

顯示您的控制器類。

似乎你在路徑定義中使用了錯誤的模式。嘗試在jsp和控制器級別更改「/ UserHomeDebit」「UserHomeDebit」

當您提交表單時,可以使用瀏覽器url進行檢查。讓我們覺得你的應用程序名稱爲sampleapplication。那麼URL將被

http://localhost:8080/sampleapplication

然後,如果你是使用/ UserHomeDebit作爲控制器的路徑映射,然後URL將如下改變。

http://localhost:8080/UserHomeDebit

所以它是錯誤的正確的一個必須

http://localhost:8080/sampleapplication/UserHomeDebit

所以只是改變了路徑正如我剛纔所說,檢查

0

<form:form>標籤的行動屬性,你必須指定完整的URL路徑到你的應用程序。有幾種方法可以做到這一點,但最簡單和優雅的方法有兩種:首先通過<c:url>標籤指定一個url。爲此,首先需要減少JSTL c preffix。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 

然後設置期望的URL(http://localhost:8080/[your-app-name]/UserOperation):

<c:url value="/UserOperation" var="actionUrl" /> 

,並傳遞URL variableto你可以通過在JSP頁面的開頭附加以下行做動作形式的屬性:

<form:form method="POST" action="${actionUrl}" ... 

二soluti在做到這一點,simplier但不能肯定是否更優雅是直接讓你的應用程序春表達式語言的上下文URL在action屬性和附加對應於您的控制器方法的URL的一部分:

<form:form method="POST" action="${pageContext.request.contextPath}/UserOperation" ... 
相關問題