2017-09-06 1 views
0

jstlを使用しています。コントローラからjspに挿入するときにIDを取得する必要があります。これを達成するために、私はajaxを使用しており、フォームのmodelAttributeを渡すことができません。@ModelAttribtueをControllerに渡し、POSTメソッドを呼び出して、ajaxコールを使用してspring mvcにデータを挿入します。

JSP

<form:form method="POST" action="addCountry.html" name="form" modelAttribute="countryMaster" id="addCountryForm"> 
    <div class="box-body"> 
     <div class="row"> 
      <div class="col-md-6"> 
       <div class="form-group has-feedback" id="countryNameDiv"> 
        <form:label path="countryName" for="countryName">Country Name</form:label> 
         <form:input placeholder="Enter country Name" path="countryName" 
          class="form-control" name="countryName" 
          value="${countryMaster.countryName}" required="true" 
          data-error="country Name cannot be empty" /> 
          <form:errors path="countryName" cssClass="text-red" /> 
          <div class="help-block with-errors"></div> 
       </div> 
      </div> 
     </div> 
    </div> 
    <div class="box-footer"> 
     <form:button type="button" onclick="window.location.reload()" class="btn pull-left btn-primary">Clear</form:button> 
     <div class="pull-right"> 
      <button onclick="submitTheForm()" class="btn btn-primary">Add Country</button> 
      &nbsp;&nbsp;&nbsp;&nbsp;<a href="" class="btn pull-right btn-primary">Cancel</a>      
     </div> 
    </div> 
</form:form> 

AJAX

function submitTheForm(){ 
var value = $("#addCountryForm").serialize(); 
$.ajax({ 
    type : "post", 
    url : 'addSaveCountry.html', 
    dataType: "json" 
    data : { 
     countryMaster : value 
    }, 
    success: function(result){ 
     console.log(result); 
     if(resule.localeCompare("Some exception occured, try again.") == 0) { 
      /** 
      * Give a div where you display this message 
      **/ 
     } else if(result.localeCompare("New Country Inserted")) { 
      var alert = "<div class=\"alert alert-success alert-dismissible\" role=\"alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">x</span>"; 
      alert += "</button>"; 
      alert += "<strong>"+result+"!</strong>"; 
      alert += "</div>"; 
      var informationDiv = alert + "<br>"; 
      document.getElementById("informationDiv").innerHTML = informationDiv; 
     } else { 
      var alert = "<div class=\"alert alert-success alert-dismissible\" role=\"alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">x</span>"; 
      alert += "</button>"; 
      alert += "<strong>"+result+"!</strong>"; 
      alert += "</div>"; 
      var informationDiv = alert + "<br>"; 
      document.getElementById("informationDiv").innerHTML = informationDiv; 
     } 
    } 
});} 

コントローラコード

@RequestMapping(value="/addSaveCountry", method = RequestMethod.POST) 
public @ResponseBody String addCountryPost(@Validated @ModelAttribute("countryMaster") Country country, BindingResult result){ 
try { 
    if (result.hasErrors()) { 
     Gson gson = new Gson(); 
     String json = gson.toJson("Some exception occured, try again."); 
     return json; 
    } 
    int companyId = 1; 
    country.setCompanyId(companyId); 
    String createdBy, lastUpdatedBy; 
    createdBy = "IN129"; 
    lastUpdatedBy = "IN129"; 
    country.setCreatedBy(createdBy); 
    country.setLastUpdatedBy(lastUpdatedBy); 
    java.sql.Timestamp curTime = new java.sql.Timestamp(new java.util.Date().getTime()); 
    country.setCreatedDate(curTime); 
    country.setLastUpdatedDate(curTime); 
    boolean status = countryService.addorupdateCountry(country); 
    if (status == true) { 
     Gson gson = new Gson(); 
     String json = gson.toJson("New Country Inserted"); 
     return json; 
    } else { 
     Gson gson = new Gson(); 
     String json = gson.toJson("New Country Insertion Failed, Try Again Later"); 
     return json; 
    } 
}} 

私はそれを実行して、私は
を取得しています挿入しようと、「HTTPステータス405 - リクエスト方法'POST'はサポートされていません "
message - 要求メソッドPOSTがサポートされていません
description - 指定されたHTTPメソッドは、要求されたリソースに対して許可されていません。

ありがとうございます。

答えて

0

変更のhtmlの接尾辞から、あなたのURL:

RequestMapping

url : '/addSaveCountry', 

のパスに

url : 'addSaveCountry.html', 

も型に変更: "POST" は(大文字)

+0

私はから.htmlを削除した場合url、要求はコントローラに当たっていませんが、urlに.htmlを入れて、小文字から大文字に変更すると、値はコントローラに到達していますが、名前はnullであり、同じエラーページが表示されます。 – Ramanathan

+0

リクエストの本文に何が入っているのかチェックし、BodyクラスをCountryクラスにマップできるかどうかをチェックしてください。リクエストマッピングがvalue = "/ addSaveCountry"のときにURLが '/ addSaveCountry' –

関連する問題