java - JSF 2.0。无法在 preRenderView 事件处理程序中获取 POST 参数和 cookie

标签 java jsf jsf-2 prerender

我正在尝试开发一项服务。

重点是我的 index.xhtml 应该从 HTTP 请求中获取参数(POST 和 GET)和 cookie。

我尝试与 <f:metadata> 结合使用和 <f:event type="preRenderView">像这样:

<f:metadata>  
    <f:event type="preRenderView" listener="#{deConversation.start}"/>  
</f:metadata>

deConversation.start 代码:

public void start(ComponentSystemEvent event) {
    System.out.println("checkLogin");
    HttpServletRequest request = SsoHelper.getRequest();
    String requestSessId = SsoHelper.getRequestSessionId(request);
    String requestRedirect = SsoHelper.getRequestRedirect(request);

    System.out.println("sessId " + requestSessId);

    if (requestRedirect == null || requestRedirect.isEmpty()) {
        requestRedirect = "self";
    }

    if (requestSessId != null) {
        trySessId(requestSessId, requestRedirect);
    }

    externalResourcesHandler.setExternalRedirect(requestRedirect);
    tryToBeginConversation();

    if (!isAuthorized()) {
        SsoHelper.performNavigation("auth");
    }
}

SsoHelper 只是提供这样的 api:

public static String getRequestSessionId(HttpServletRequest request) {
    Map<String, Object> cookieMap = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap();
    String requestDeSessionId = null;
    if (cookieMap.containsKey("de_session_id")) {
        requestDeSessionId = ((Cookie) cookieMap.get("de_session_id")).getValue();
    }
    return requestDeSessionId;
}

public static String getRequestRedirect(HttpServletRequest request) {
    return getRequestParam(request, "redirect", "self");
}

public static String getRequestExternalCss(HttpServletRequest request) {
    return getRequestParam(request, "externalcss", null);
}

public static String getRequestParam(HttpServletRequest request, String name, String defaultValue) {
    String[] paramValues = HttpServletRequestHelper.getParamValues(request, name);
    String paramValue = null;
    if (paramValues != null && paramValues.length != 0) {
        paramValue = paramValues[0];
    }
    if(paramValue == null){
        paramValue = defaultValue;
    }
    return paramValue;
}

public static HttpServletRequest getRequest() {
    return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
}

public static void performNavigation(String destination) {
    FacesContext context = FacesContext.getCurrentInstance();
    ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) context.getApplication().getNavigationHandler();
    handler.performNavigation(destination);
}

重点是我无法在方法 start() 中获取任何 POST 参数或 cookie。我只能获取 GET 参数。

是否可以使用 <f:event type="preRenderView"> 读取 cookie 和 POST 参数? ?

最佳答案

我认为您应该使用 @PostConstruct 调用 init 方法,以便在页面呈现之前调用 init() 并采用全局变量为变量分配 Session 或 Cookies 值,从而实现您的要求。

例子:

@PostConstruct
public void init(){
    // Your Response
    Map request = (Map)FacesContext.getCurrentInstance().getExternalContext().getResponse();
    request.get("Your Key");

    // Cookies
    Map cookie = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap();
    cookie.get("Your Key");

    Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
    session.get("Your Key"); 
}

试试这个,我认为它会对你有帮助。

关于java - JSF 2.0。无法在 preRenderView 事件处理程序中获取 POST 参数和 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13271113/

相关文章:

jsf - p :remoteCommand not working in async mode

JSF EL 表达式比较字符串值

javascript - 从自动填充列表中选择值时运行 ajax 监听器操作

java - 为什么InvocationHandler中的invoke()方法有一个参数Object proxy?

Java Swing - JList 自定义单元格渲染 - 捕获 Action

java - 将 android studio 上的 SQLite 数据库与 Web 服务器上的 MySQL 数据库同步

ajax - JSF ajax 监听器

java - 无法触发 h :selectonemenu onselect event

jsf-2 - Jsf 复合组件属性不起作用

java - 来自 .proto 模式文件或字符串的原型(prototype)描述符