Java Liferay portlet+;struts2+;AJAX
从最近几天开始,我已经尝试了很多,但仍然无法解决这个问题 我正在使用liferay 6.1和struts 2。基本上,我的liferay portlet中有两个下拉列表,一个用于国家,另一个用于州。选择国家/地区时,需要根据所选国家/地区将状态列表置于下拉列表中。我正试图通过AJAX实现这一点。(下面是我从google找到的代码,它在一个单独的动态Web项目中工作,但当我在liferay portlet中尝试此操作时,它出现了抛出错误 下面是我的代码: StartPage.jspJava Liferay portlet+;struts2+;AJAX,java,ajax,jsp,struts2,liferay,Java,Ajax,Jsp,Struts2,Liferay,从最近几天开始,我已经尝试了很多,但仍然无法解决这个问题 我正在使用liferay 6.1和struts 2。基本上,我的liferay portlet中有两个下拉列表,一个用于国家,另一个用于州。选择国家/地区时,需要根据所选国家/地区将状态列表置于下拉列表中。我正试图通过AJAX实现这一点。(下面是我从google找到的代码,它在一个单独的动态Web项目中工作,但当我在liferay portlet中尝试此操作时,它出现了抛出错误 下面是我的代码: StartPage.jsp <scr
<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>
您可以使用AJAX AUI脚本来实现这一点。我正在使用Liferay 6.2 sp2 SDK和Liferay MVC portlet。您可以使用struts方法来尝试这一点,看看它是否对您有所帮助 使用
liferay。在AUI脚本中提供函数
<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),
}, .....
}
在调用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();" %>'>
<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),
}, .....
}
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException {
resourceResponse.setContentType("text/javascript");
... ...
//Send Data Back
resourceResponse.setContentType("text/html");
}
<!-- Create a serveResource URL -->
<portlet:resourceURL var="ajaxCallResourceURL" />
<aui:select name="mCountry" id="mCountry" label="Country of permanent address*" inlineLabel="left" onChange='<%= renderResponse.getNamespace() + "updatePermState();" %>'>