java.lang.IllegalStateException 在 TokenHelper 中设置 SessionToken 时抛出

java.lang.IllegalStateException thrown while setSessionToken in TokenHelper

我有一个 Java 应用程序 运行 在 Linux OS 上使用 Jetty 服务器。

Struts 更改 Linux 系统日期和时间的操作,然后呈现另一个页面。 操作已成功执行,但在呈现页面时会抛出 java.lang.IllegalStateException.

我在该 JSP 页面中使用了 s:token 标记来防止重复提交表单。

错误跟踪如下:

ERROR TokenHelper Error creating HttpSession due response is committed to client. You can use the CreateSessionInterceptor or create the HttpSession from your action before the result is rendered to the client: null
 java.lang.IllegalStateException
    at org.eclipse.jetty.server.session.AbstractSession.checkValid(AbstractSession.java:109)
    at org.eclipse.jetty.server.session.HashedSession.checkValid(HashedSession.java:73)
    at org.eclipse.jetty.server.session.AbstractSession.getAttribute(AbstractSession.java:132)
    at org.apache.struts2.dispatcher.SessionMap.get(SessionMap.java:161)
    at org.apache.struts2.dispatcher.SessionMap.put(SessionMap.java:179)
    at org.apache.struts2.util.TokenHelper.setSessionToken(TokenHelper.java:93)
    at org.apache.struts2.util.TokenHelper.setToken(TokenHelper.java:79)
    at org.apache.struts2.components.Token.buildToken(Token.java:107)
    at org.apache.struts2.components.Token.evaluateExtraParams(Token.java:97)
    at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:886)
    at org.apache.struts2.components.UIBean.end(UIBean.java:535)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    at org.apache.jsp.pages.Continue_jsp._jspx_meth_s_token_0(org.apache.jsp.pages.Continue_jsp:495)
    at org.apache.jsp.pages.Continue_jsp._jspx_meth_s_form_0(org.apache.jsp.pages.Continue_jsp:429)
    at org.apache.jsp.pages.Continue_jsp.access(org.apache.jsp.pages.Continue_jsp:407)
    at org.apache.jsp.pages.Continue_jsp$Continue_jspHelper.invoke2(org.apache.jsp.pages.Continue_jsp:1197)
    at org.apache.jsp.pages.Continue_jsp$Continue_jspHelper.invoke(org.apache.jsp.pages.Continue_jsp:1221)

请让我们知道可能的解决方案或处理错误的建议。

ERROR TokenHelper Error creating HttpSession due response is committed to client. You can use the CreateSessionInterceptor class or create the HttpSession from your action before the result is rendered to the client.

您可以使用类似于

的代码创建HttpSession
ActionContext context = ActionContext.getContext();
SessionMap<String, T> sessionMap = (SessionMap<String, T>) context.getSession();

if (sessionMap == null) {
    sessionMap = new SessionMap<String, T>(ServletActionContext.getRequest());
    context.setSession((Map<String, Object>) sessionMap);
}

createSession interceptor.

This interceptor creates the HttpSession if it doesn't exist, also SessionMap is recreated and put in ServletActionContext.

This is particular useful when using the <@s.token> tag in freemarker templates. The tag do require that a HttpSession is already created since freemarker commits the response to the client immediately.