2012-08-31 38 views
1

我根據中的更改動態加載spring複選框標記select標記。但它會產生如下錯誤:使用jQuery在jsp中動態加載spring複選框標記的錯誤

2012-08-31 09:27:14.829 org.springframework.web.servlet.tags.RequestContextAwareTag doStartTag: Neither BindingResult nor plain target object for bean name 'stockList' available as request attribute 
W 2012-08-31 09:27:14.831 /stocks 
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'stockList' available as request attribute 
C 2012-08-31 09:27:14.832 Uncaught exception from servlet 
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'stockList' available as request attribute 

在我的應用程序引擎服務器日誌中。下面

<link rel="stylesheet" 
    href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" /> 
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 
<script 
    src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script> 
<script type="text/javascript"> 
    function loadStocks() { 
     var exchange = $("#stockExchange").val(); 
     var a; 
     if (exchange = "BO") { 
      a = "<form:checkbox path='stockList' value='INFY' label='Infosys' />" 
        + "<form:checkbox path='stockList' value='HINDALCO' label='HINDALCO' />" 
        + "<form:checkbox path='stockList' value='TTM' label='Tata Motors' />"; 
     } else if (exchange == "NS") { 
      a = "<form:checkbox path='stockList' value='INFY' label='Infosys' />"; 
     } 
     $("#kit").html(a); 
    } 
</script> 
</head> 
<body> 
    <div data-role="page"> 
     <div data-role="content"> 
      <form:form method="POST" commandName="search"> 
       <ul data-role="listview"> 
        <li data-role="fieldcontain"> 
         <div id="kit"> 
          <form:select path="stockExchange" onchange="loadStocks()" 
           id="stockExchange"> 
           <form:option value="">Select Stock Exchange</form:option> 
           <form:option value="BO">BSE</form:option> 
           <form:option value="NS">NSE</form:option> 
          </form:select> 
         </div> 
        </li> 

        <li data-role="fieldcontain"><label>Select Stocks</label> 
         <div id="kit"> 
          <form:checkbox path="stockList" value="INFY" label="Infosys" /> 
          <form:checkbox path="stockList" value="HINDALCO" label="HINDALCO" /> 
          <form:checkbox path="stockList" value="TTM" label="Tata Motors" /> 
          <form:checkbox path="stockList" label="RELIANCE IND." 
           value="RELIANCE" /> 
         </div></li> 

        <li data-role="fieldcontain"><input type="submit" 
         value="Submit" /></li> 
       </ul> 
      </form:form> 
     </div> 
     <!-- /content --> 

    </div> 
    <!-- /page --> 

</body> 

回答

1

我的代碼被賦予這個JavaScript函數不會以這種方式工作。在瀏覽器中查看HTML源代碼,您會注意到它與JSP不同。 <form:xxxx/>標籤是spring標籤,可以快速生成HTML,並使屬性綁定更容易。因此,舉例來說:

<form:checkbox path="stockList" value="INFY" label="Infosys" /> 

將aproximately轉化爲這個普通的HTML:

<input type="checkbox" id="stockList" name="stockList" value="INFY" label="Infosys" /> 

至於你的JavaScript函數在客戶端,春天標籤不會改變,所以你將有在你的Javascript函數中使用純html。類似的東西:

function loadStocks() { 
    var exchange = $("#stockExchange").val(); 
    var a; 
    if (exchange = "BO") { 
     a = "<input type='checkbox' id='stockList1' name='stockList' value='INFY' label='Infosys' />" 
       + "<input type='checkbox' id='stockList2' name='stockList' value='HINDALCO' label='HINDALCO' />" 
       + "<input type='checkbox' id='stockList3' name='stockList' value='TTM' label='Tata Motors' />"; 
    } else if (exchange == "NS") { 
     a = "<input type='checkbox' id='stockList' name='stockList' value='INFY' label='Infosys' />"; 
    } 
    $("#kit").html(a); 
} 

希望我已經很好地解釋了我自己。

P.S .:爲什麼id的輸入是,stockList2stockList3?回答和相關信息here,檢查出來,會對你有所幫助。

編輯:

另一種解決方案,也許更容易,是建立在你的JSP一個隱藏的div與JS功能需要的投入,讓春天改造它。稍後,在您的JS函數中,您可以使用JQuery克隆由Spring生成的這些輸入。這樣可以避免你創建Spring可以理解的普通HTML。

相關問題