有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Java CDI Weld重新连接到对话

我想加入现有的对话范围

我以以下内容开始对话:

conversation.begin(packageId);

我接近于使用以下似乎有效的方法:

@Inject @Http CoversationContext context;

context.activate(packageId);

然而,我在日志中看到一条警告,这表明我做得不对

WARN: WELD-000335: Conversation context is already active, most likely it was not cleaned up properly during previous request processing: HttpServletRequestImpl [ POST /path/to/url ]

我也很高兴,如果有另一种方法可以删除对话并重新创建(只要我可以继续使用相同的自定义对话ID),我会尝试避免用户多次重新加载页面,用相同包数据的重复项填充内存

我还考虑过使用@SessionScoped bean,但我认为如果我可以将包ID设置为对话ID,那么就可以避免管理@SessionScoped bean


共 (1) 个答案

  1. # 1 楼答案

    只要cid参数在请求中,并且对话是long-running(因为您已经conversation.begin(packageId)),那么就不需要加入对话上下文,它在当前请求中已经处于活动状态

    但是,您需要做的是通过以下方式在每个请求表单或url参数中包含cid

    例如

    <h:link> <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/> </h:link>

    或者

    <h:form> <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/> /h:form>

    Note that the conversation must be long-running by explicitly starting it as conversation.begin(id)

    此外:

    At the end of your step processing, you need to explicitly call conversation.end() otherwise the conversation scoped beans will be destroyed only at the end of the session context

    对于图书标记,则需要在路径中包含cid参数或任何逻辑映射,然后使用过滤器转发cid参数:

    @WebFilter(urlPatterns = "/*")
    public class CidFilter implements Filter {
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
            String cid = extractCidParameterIfAny(request);
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            if (cid != null) {
                String forwardUrl = buildForwardUrlWithCidParameter(cid);
                HttpServletRequest wrapper = new CidHttpServletRequest(httpRequest);
                httpRequest.getRequestDispatcher(forwardUrl).forward(wrapper, response);
            } else {
                chain.doFilter(request, response);
            }
        }
    
    }