加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JSF 2.0.无法在preRenderView事件处理程序中获取POST参

发布时间:2020-12-15 03:10:48 所属栏目:Java 来源:网络整理
导读:我正在努力开发一项服务. 关键是我的index.xhtml应该从HTTP Request获取参数(POST和GET)和cookie. 我尝试与 f:metadata组合使用和 f:event type =“preRenderView”像这样: f:metadata f:event type="preRenderView" listener="#{deConversation.start}"/
我正在努力开发一项服务.

关键是我的index.xhtml应该从HTTP Request获取参数(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"); 
}

尝试这个我认为它会帮助你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读