2015-09-19 96 views
2

自從最近幾天以來,我已經嘗試了很多,但仍無法解決此問題。Liferay portlet + struts2 + Ajax

我正在使用liferay 6.1和struts 2.基本上我的liferay portlet中有兩個下拉列表,一個是國家和其他州的。選擇國家時,州列表需要根據所選國家在下拉列表中。我試圖通過AJAX來做到這一點。 (下面的代碼,我從谷歌發現,其工作在一個單獨的動態Web項目,但是當我試圖在此portlet的Liferay的投擲的錯誤

下面是我的代碼:

StartPage.jsp

<script > 
$(document).ready(function() { 
    $('#country').change(function(event) { 
     var country = $("select#country").val(); 
     alert(country); 
     $.getJSON("<s:url action='ajaxAction' namespace='ajax' includeParams='none' />", {countryName : country}, function(jsonResponse) { 
       $('#ajaxResponse').text(jsonResponse.dummyMsg); 
       alert($('#ajaxResponse').text(jsonResponse.dummyMsg)); 
       var select = $('#states'); 
       select.find('option').remove(); 
       $.each(jsonResponse.stateMap, function(key, value) { 
        $('<option>').val(key).text(value).appendTo(select); 
       }); 
      }); 
    }); 
}); 
</script> 




<s:form name="StartPage" id="StartPage"> 

     <s:select id="country" name="country" 
      list="{'Select Country','India','US'}" label="Select Country" /> 
     <br /> 
     <br /> 
     <s:select id="states" name="states" list="{'Select State'}" 
      label="Select State" /> 
     <br /> 
     <br /> 
     <div id="ajaxResponse"></div> 

</s:form> 

struts.xml的

<package name="default" extends="json-default"> 
     <action name="ajaxAction" class="com.action.AjaxJsonAction"> 
      <result type="json">/WEB-INF/view/StartPage.jsp 
      </result> 
     </action> 
    </package> 

Action類:

public class AjaxJsonAction extends DefaultActionSupport{ 

    private Map<String, String> stateMap = new LinkedHashMap<String, String>(); 
    private String dummyMsg; 
    //Parameter for Jquery 
    private String countryName; 

    @Override 
    public String execute() { 
     System.out.println("i am executed..."); 
     System.out.println("CountryName: " + countryName); 
     if (countryName.equals("India")) { 
      stateMap.put("1", "Kerala"); 
      stateMap.put("2", "Tamil Nadu"); 
      stateMap.put("3", "Jammu Kashmir"); 
      stateMap.put("4", "Assam"); 
     } else if (countryName.equals("US")) { 
      stateMap.put("1", "Georgia"); 
      stateMap.put("2", "Utah"); 
      stateMap.put("3", "Texas"); 
      stateMap.put("4", "New Jersey"); 
     } else if (countryName.equals("Select Country")) { 
      stateMap.put("1", "Select State"); 
     } 
     dummyMsg = "Ajax action Triggered"; 
     System.out.println("exiting....."); 
     return "success"; 
    } 

    public Map<String, String> getStateMap() { 
     return stateMap; 
    } 

    public String getDummyMsg() { 
     return dummyMsg; 
    } 

    public String getCountryName() { 
     return countryName; 
    } 

    public void setStateMap(Map<String, String> stateMap) { 
     this.stateMap = stateMap; 
    } 

    public void setDummyMsg(String dummyMsg) { 
     this.dummyMsg = dummyMsg; 
    } 

    public void setCountryName(String countryName) { 
     this.countryName = countryName; 
    } 
} 

Errro登錄:

java.lang.IllegalArgumentException: application/json;charset=UTF-8 is not a supported mime type 
    at com.liferay.portlet.MimeResponseImpl.setContentType(MimeResponseImpl.java:159) 
    at org.apache.struts2.portlet.servlet.PortletServletResponse.setContentType(PortletServletResponse.java:219) 
    at org.apache.struts2.json.JSONUtil.writeJSONToResponse(JSONUtil.java:225) 
    at org.apache.struts2.json.JSONResult.writeToResponse(JSONResult.java:211) 
    at org.apache.struts2.json.JSONResult.execute(JSONResult.java:172) 
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373) 

回答

1

您可以使用AJAX腳本AUI此。我正在使用Liferay 6.2 sp2 SDK和Liferay MVC portlet。你可以用struts的方法來試試看看它是否對你有幫助。

在您的AUI腳本中使用liferay.provide函數。

<aui:script> 
Liferay.provide(
    window, 
    '<portlet:namespace />updatePermState', 
    function() { 
     var A = AUI(); 
     var url = '<%= ajaxCallResourceURL.toString() %>'; 
     A.io.request(
      url, 
      { 
       //data to be sent to server 
       data: { 
       <portlet:namespace />param1: (document.getElementById('<portlet:namespace/>mCountry').value), 
       }, ..... 

} 

在您的portlet類中創建servResource函數。這將與您的AUI調用來連線:

public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException { 

     resourceResponse.setContentType("text/javascript"); 
... ... 
//Send Data Back 
resourceResponse.setContentType("text/html"); 
} 

在調用JSP文件中添加此:

<!-- Create a serveResource URL --> 
<portlet:resourceURL var="ajaxCallResourceURL" /> 

AJAX調用。我在Country字段上使用onChange方法。我的UpdatePermState()函數動態地創建一個帶有狀態的div。

<aui:select name="mCountry" id="mCountry" label="Country of permanent address*" inlineLabel="left" onChange='<%= renderResponse.getNamespace() + "updatePermState();" %>'>