我正在尝试开发一项服务。
关键是我的 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 参数。
是否有可能使用读取 cookie 和 POST 参数<f:event type="preRenderView">
?