【问题标题】:How To Use Ajax in Spring Portlet?如何在 Spring Portlet 中使用 Ajax?
【发布时间】:2015-08-13 09:17:02
【问题描述】:

我正在使用 liferay 6.2 CE ga4,并使用 Maven (MVC)。

这是我的代码:

国家:

<select id="country" name="country">
<option value="select">Select Country</option>
<option value="india">India</option>
<option value="usa">USA</option>
</select>

State:
<select id="state" name="state">
</select>

<script type="text/javascript">
$(document).ready(function(){

$( "#country" ).change(function() {
    var country = $(this).val();
      $.ajax({
            url: "${findState}" ,
            type: 'POST',
            datatype:'json',
            data: "countryName="+country ,
            success: function(data){
                $('#state').html('');
                $.each(content, function(i, state) {
                    $('#state').append($('<option>').text(state.name).attr('value', state.stateId));
                });
            }
        });
  }); 
});
</script>

这是我的 Java:

     @ResourceMapping(value="findState")
     public void findStateForCountry(ResourceRequest request, ResourceResponse response) throws IOException  {
         String countryName = ParamUtil.getString(request, "countryName");
         System.out.println("name="+countryName);
         String name = request.getParameter("countryName");
         System.out.println("name2="+name);
         //var countryName and var name allways NULL

        JSONArray stateArray = JSONFactoryUtil.createJSONArray();
        JSONObject stateObject,stateObject2;
        if(countryName.equalsIgnoreCase("india"))
        {
            stateObject = JSONFactoryUtil.createJSONObject();
            stateObject.put("stateId", "1");
            stateObject.put("name", "Delhi");

            stateObject2 = JSONFactoryUtil.createJSONObject();
            stateObject2.put("stateId", "2");
            stateObject2.put("name", "Gujrat");
        }
        else{

            stateObject = JSONFactoryUtil.createJSONObject();
            stateObject.put("stateId", "21");
            stateObject.put("name", "LA");

            stateObject2 = JSONFactoryUtil.createJSONObject();
            stateObject2.put("stateId", "22");
            stateObject2.put("name", "California");
        }
        stateArray.put(stateObject);
        stateArray.put(stateObject2);
        response.getWriter().println(stateArray);
     }

我想使用 pass 参数,但它不起作用。我用这个:

String countryName = ParamUtil.getString(request, "countryName"); 

还有这个:

String name = request.getParameter("countryName");

要获取参数但他们总是得到NULL

我的代码有问题吗?


答案是 :

在你的 liferay-portlet.xml 中使用它

<portlet>
      <portlet-name>welcome</portlet-name>
      <requires-namespaced-parameters>false</requires-namespaced-parameters>
  </portlet>

【问题讨论】:

    标签: java ajax spring maven spring-portlet-mvc


    【解决方案1】:

    一种简单的方法是使用查询字符串参数:

    $( "#country" ).change(function() {
        var country = $(this).val();
          $.ajax({
                url: "${findState}"+'&countryName='+ country,
                type: 'GET',
                datatype:'json',
                success: function(data){
                    $('#state').html('');
                    $.each(content, function(i, state) {
                        $('#state').append($('<option>').text(state.name).attr('value', state.stateId));
                    });
                }
            });
      }); 
    });
    

    然后,在您的控制器中,您可以像这样使用@RequestParam 注释:

     @ResourceMapping(value="findState")
     public void findStateForCountry(ResourceRequest request, ResourceResponse response, @RequestParam(value = "countryName") String countryName) throws IOException  {
         System.out.println("name="+countryName);
    
        JSONArray stateArray = JSONFactoryUtil.createJSONArray();
        JSONObject stateObject,stateObject2;
        if(countryName.equalsIgnoreCase("india"))
        {
            stateObject = JSONFactoryUtil.createJSONObject();
            stateObject.put("stateId", "1");
            stateObject.put("name", "Delhi");
    
            stateObject2 = JSONFactoryUtil.createJSONObject();
            stateObject2.put("stateId", "2");
            stateObject2.put("name", "Gujrat");
        }
        else{
    
            stateObject = JSONFactoryUtil.createJSONObject();
            stateObject.put("stateId", "21");
            stateObject.put("name", "LA");
    
            stateObject2 = JSONFactoryUtil.createJSONObject();
            stateObject2.put("stateId", "22");
            stateObject2.put("name", "California");
        }
        stateArray.put(stateObject);
        stateArray.put(stateObject2);
        response.getWriter().println(stateArray);
     }
    

    【讨论】:

      【解决方案2】:

      您可以使用如下所示的 URL 分配。

          $(document).ready(function(){
      
      $( "#country" ).change(function() {
          var country = $(this).val();
            $.ajax({
                  url: '<portlet:resourceURL var="findState"></portlet:resourceURL>' ,
                  type: 'POST',
                  datatype:'json',
                  data: "countryName="+country ,
                  success: function(data){
                      $('#state').html('');
                      $.each(content, function(i, state) {
                          $('#state').append($('<option>').text(state.name).attr('value', state.stateId));
                      });
                  }
              });
        }); 
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多