【发布时间】:2018-10-09 12:18:41
【问题描述】:
我试图在单击按钮后立即在 JSF 中获取我的 RequestID 或 RequestURI。我试过这个:
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext exContext = context.getExternalContext();
Map<String, String> x = exContext.getRequestHeaderMap();
System.out.println(x.get("request"));
System.out.println(context.getViewRoot().getViewId());
System.out.println(exContext.getRequestContextPath());
问题是,我的页面是一个模板,它使用新的 XHTML 重新加载侧面的一部分。基本的一面是home.xhtml,我总是得到这个ID。
但我需要的是其中包含的 XHTML,例如 view1.xhtml(在 home.xhtml 中加载)。
这可能吗?我需要这个来将请求视图保存在我的小型翻译应用程序的数据库中。
编辑: 我的 WebApp 应该跟踪哪个视图调用了我的资源包中的特殊翻译标签,并将视图(view1.xthml,而不是 home.xhtml)和标签写入数据库。也许也可以获取 html 元素 ID(但这并不重要)?每个视图都有多个包含!
示例:login.xhtml 包含在 home.xhtml 中
<p:panel header="#{translations['titel']}">
现在应用应该保存(login.xhtml,标题)。
谢谢!
EDIT2:
XHTML:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>example</title>
<link rel="shortcut icon" type="image/x-icon"
href="#{request.contextPath}/resources/images/logo/example.ico"/>
</h:head>
<h:body>
<h:form id="menubar">
<p:menubar>
<p:submenu label="#{translations['language']}" icon="fa fa-language">
<p:menuitem value="German" action="#{language.setLocale(de)}"/>
<p:menuitem value="English" action="#{language.setLocale(en)}"/>
</p:submenu>
</p:menubar>
</h:form>
</h:body>
资源包的faces-config.xml 条目:
<resource-bundle>
<base-name>example.i18n.resourcebundle.DBResourceBundle</base-name>
<var>translations</var>
</resource-bundle>
Translation-Bean-Function:
public String test() {
FacesContext fCtx = FacesContext.getCurrentInstance();
UIComponent currentComponent = UIComponent.getCurrentComponent(fCtx);
if (null != currentComponent) {
Location location = (Location) currentComponent.getAttributes().get(UIComponent.VIEW_LOCATION_KEY);
if (null != location) {
System.out.println(location);
System.out.println(location.getPath());
return location.getPath();
}
}
return null;
}
现在我想将label="#{translations['language']}" 的所有请求保存在地图中。该映射应以“language”作为键,以请求的 XHTML(例如“login.xhtml”)作为值。 Translations 包含我需要的大部分翻译,如果它不在里面,我也想要一个条目。
在这种情况下,如果请求调用test() 函数就足够了。
【问题讨论】:
-
仅通过从视图中检索
ui:include并查找其值 -
2 个问题:1) 有这样的功能吗? 2)我有多个包含,我如何区分哪个是正确的?
-
1) 是的,google 用于在 jsf 视图中查找组件。 2) 是的,通过 id
-
我稍微改进了我的问题,我认为这不是我想要的