这是在 JSP 下:
<%!
public PortletURL getActionURL(RenderResponse response, String action) {
PortletURL portleturl = response.createRenderURL();
try {
portleturl.setWindowState(LiferayWindowState.EXCLUSIVE);
portleturl.setParameter("struts_action", "helloForm");
} catch (Exception e) {
e.printStackTrace();
}
return portleturl;
}
%>
<%
PortletURL helloFormssss = getActionURL(renderResponse, "helloForm");
System.out.println("The URL is helloFormssss " + helloFormssss);
%>
将此 URL 传递给 jQuery AJAX:
function my_button_handler() {
jQuery.ajax({
type: "POST",
url: "helloFormssss",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
}
这是我的Action
课
public class Struts2Action extends DefaultActionSupport {
String name = null;
public String getPOPChartData() throws Exception {
// Enter business logic here
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String name = ParamUtil.getString(request, "namer");
System.out.println("The name is " + name);
PrintWriter writer = response.getWriter();
writer.print("Hi");
return ActionSupport.SUCCESS;
}
}
这是在struts.xm
l 文件中:
<action name="helloForm" class="com.robisoft.portlet.struts.action.Struts2Action" method="getPOPChartData">
<result name="input">/WEB-INF/view/index.jsp</result>
<result name="success">/WEB-INF/view/result.jsp</result>
</action>
网址是 helloFormssss
http://localhost:8086/web/guest/home?p_p_id=HelloStruts_WAR_HelloStrutsportlet_INSTANCE_yip2vomViZSU&p_p_lifecycle=0&p_p_state=exclusive&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_HelloStruts_WAR_HelloStrutsportlet_INSTANCE_yip2vomViZSU_struts_action=helloForm
这是我进入服务器控制台的异常
14:31:37,973 错误 [Jsr168Dispatcher:38] 无法执行操作。 java.lang.IllegalStateException:在 com.robisoft.portlet.struts.action.Struts2Action.getPOPChartData(Struts2Action.java ) 的 org.apache.struts2.portlet.servlet.PortletServletResponse.getWriter(PortletServletResponse.java:169)
的事件阶段不允许:56) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452) 在com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291) 在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254) 在 com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java: 176) 在 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 在 com.opensymphony.xwork2。com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java: org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) 98)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation .java:248) 在 com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) 在 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor。在 com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) 在 com.opensymphony.xwork2 的拦截(MethodFilterInterceptor.java:98) .interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 在 com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) 在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 在 org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) 在 com.opensymphony.xwork2。DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache .struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243) 在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 在 com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100 ) 在 com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) 在 com.opensymphony 的 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)。xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)在 com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) 在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 在 com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor .java:171) 在 com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 在 com.opensymphony。xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) 在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 在 org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 在 com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190) 在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java :248) 在 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 在 org.apache.struts2 的 com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)。org.apache.struts2.portlet.interceptor.PortletStateInterceptor.intercept(PortletStateInterceptor.java: 52) 在 org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) 在 org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher 的 com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) .serviceAction(Jsr168Dispatcher.java:451) at org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher.processAction(Jsr168Dispatcher.java:298) at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:70) at com。 liferay.portal.kernel.portlet。PortletFilterUtil.doFilter(PortletFilterUtil.java:48) at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:93) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)