使用模板时在JSF中获取ViewID

时间:2018-10-09 12:18:41

标签: jsf view request

我试图在我单击按钮后立即在JSF中使用$(document).ready(function() { var timer; $('#start').click(function() { timer = setInterval(function() { $("#animate").animate({ 'marginLeft': '200px' }, 2000) $("#animate").animate({ 'marginTop': '200px' }, 2000) $("#animate").animate({ 'marginLeft': '10px' }, 2000) $("#animate").animate({ 'marginTop': '0px' }, 2000) }, 50); }); }); $("#stop").click(function() { clearInterval(timer); });body { border: 1px solid blue; width: 237px; height: 263px; } div { margin: 7px; width: 40px; height: 40px; position: absolute; left: 0px; top: 30px; background: yellow; } div.newcolor { background: blue; }。我尝试过:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="start">Start</button>
<button id="stop">Stop</button>
<div id="animate"></div>

问题是,我的页面是一个模板,使用新的XHTML重新加载了部分内容。基本方面是/** * This class provides basic/common functionalities to be applied on Java Objects. */ public final class ObjectUtils { private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); private ObjectUtils() { throw new UnsupportedOperationException("Instantiation of this class is not permitted in case you are using reflection."); } /** * This method is responsible for de-serializing the Java Object into Json String. * * @param object Object to be de-serialized. * @return String */ public static String deserializeObjectToString(final Object object) { return GSON.toJson(object); } } ,我一直都准确地获得此ID。

但是我需要的是包含的XHTML,例如RequestID(已加载到RequestURI中)。

有可能吗?我需要此功能将请求视图保存到我的小型翻译应用程序的数据库中。

编辑:我的Web应用程序应跟踪哪个视图从我的资源包中调用了特殊翻译标记,并写入该视图( 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()); ,而不是home.xhtml)和标记进入数据库。也许也可以获取html元素ID(但这并不重要)吗?每个视图上都有多个包含项!

示例:view1.xhtml中包含的home.xhtml

view1.xthml

现在该应用程序应该保存(login.xhtml,titel)。

谢谢!

EDIT2:

XHTML:

home.xhtml
资源束的

faces-config.xml条目:

login.xhtml

Translation-Bean-Function:

home.xhtml

现在,我想将<p:panel header="#{translations['titel']}"> 的所有请求保存在地图中。该映射应将“ 语言”作为键,并将请求的XHTML(例如“ login.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> 函数就足够了。

1 个答案:

答案 0 :(得分:0)

在评估#{translations['titel']}时,您想知道该表达式在XHTML文件中的物理位置。

深入研究后,我发现了如何获取当前渲染的Tag的位置:

有关API,请参见

javax.faces.view.Location
UIComponent.VIEW_LOCATION_KEY
javax.faces.component.UIComponent.getCurrentComponent(FacesContext)

您可以使用以下代码来获取XHTML文件名:

public String getCurrentComponentPath() {
        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) {
                return location.getPath();
            }
        }
        return null;
    }

在JSF 2.3(Mojarra)中进行了测试。

注意:如果EL表达式不在当前模板的组件中,则不会获得当前的XHTML文件名,因为UIComponent.getCurrentComponent返回该组件父模板XHTML ,其中包括当前XHTML。

例如这将无法正常工作:

<!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:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

<ui:composition>

    #{translations['titel']}

</ui:composition>
</html>

虽然这样做:

<!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:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

<ui:composition>

    <h:outputText value="#{translations['titel']}"/>

</ui:composition>
</html>