您似乎根本没有意识到 HTTP 和 Web 应用程序是如何工作的。您必须了解请求/响应周期。
AJAX 对您来说是正确的选择,但顾名思义,AJAX 是异步JavaScript - 您尝试将 java 方法调用放在 onchange 属性中。这行不通。
要首先执行您的要求,您必须找到您的 Portlet 类并实现serveResource(ResourceRequest req, ResourceResponse resp)
方法,您将在其中接收选定的值 ( String selectedVal = req.getParameter("selectedVal")
) 并根据该值返回一些内容。
String result = null;
if ("blah".equals(selectedVal))
{ result = "Something"; }
else
{ result = "Something Else"; }
resourceResponse.getPortletOutputStream().write(result.getBytes("UTF-8"));
然后,您必须对该方法进行 AJAX 调用。大致应该是这样的:
<portlet:resourceUrl var="resourceUrl">
<portlet:param name="selectedVal" value="PARAM_PLACEHOLDER_SELECTED_VAL" />
</portlet:resourceUrl>
<aui:script use="io">
function ajax<portlet:namespace />MySelect(selectedVal) {
A.io(
'${resourceUrl}'.replace("PARAM_PLACEHOLDER_SELECTED_VAL", selectedVal),
{
on: {
success: <portlet:namespace />processResponse(select, response);
}
}
);
function <portlet:namespace />processResponse(response) {
alert("Here's what java code returned:"+response+". Do whatever you want with it - with javascript");
}
</aui:script>
...
<aui:select label="My Selection" name="ms" id="ms" onchange="ajax<portlet:namespace>MySelect(this.values[this.selectedIndex])" >
<%
for(String item : itemList){
%>
<aui:option selected="<%= item.equals(selItem) %>" value="<%=item%>"><%=item%></aui:option>
<%}%>
</aui:select>
希望这可以帮助。