java - JSF 2.0 MB 的模板化和注入(inject) || CDI bean

标签 java templates jsf jsf-2 java-ee-6

问题:托管 Bean 未通过模板注入(inject)。

目标:我想减缓模板中的注销按钮。

场景:我正在使用 jsf 2.0 为 Web 部件构建 j2ee 6 应用程序。

模板文件布局/template.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:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >
<h:head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link href="/resources/css/default.css" rel="stylesheet" type="text/css"/>
    <link href="/resources/css/cssLayout.css" rel="stylesheet" type="text/css"/>
    <title>Facelets Template</title>
</h:head>
<h:body>
    <div id="baner" class="baner" >
        <ui:insert name="baner" >
            <h:panelGrid style="color:blue; width:100%; height:100px;" border="5">
                Baner Name go here
            </h:panelGrid>
        </ui:insert>
    </div>
    <div id="menu" class="menu">
        <ui:insert name="menu">
            some...
        </ui:insert>
    </div>

    <div id="top" class="top">
        <ui:insert name="top">Top Section</ui:insert>
    </div>
    <div>
        <div id="left">
            <ui:insert name="left">                
                <h:commandButton value="Do log out" action="#{securityBacking.invalidate}" />
                <h:commandButton value="log out" action="#{securityBacking.logout}" />
            </ui:insert>
            <!--log out-->
        </div>
        <div id="content" class="left_content">
            <ui:insert name="content">Main Content</ui:insert>
        </div>
    </div>
</h:body>
</html>

模板客户端index.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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:vt="http://java.sun.com/jsf/composite/security">
<body>

<ui:composition template="layout/template.xhtml">
    <ui:define name="top">
        <h:form>
            <h:panelGrid border="4">
                <!--TODO add i18n-->
                <p style="color:red;">Partner`s login.</p>
                <vt:loginPanel/>               

                <f:view>
                    <h:outputLabel value="#{rulesBean.userPrincipalName}"/>
                    <h:outputLabel value="#{rulesBean.user}"/>
                    <h:outputLabel value="#{rulesBean.manager}"/>
                    <h:commandButton value="Do log out" action="#{securityBacking.invalidate}" />
                    <h:commandButton value="log out" action="#{securityBacking.logout}" />
                </f:view>
            </h:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>

</body>
</html>

web.xml 中的faces 声明

<context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

SecurityBacking.java:

@Named("securityBacking")
@RequestScoped
public class SecurityBacking {


    public String logout() {
        String result = "/login?faces-redirect=true";
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.
                getExternalContext().getRequest();
        try {
            System.out.println("calling logout");
            request.logout();
            System.out.println("called logout");
        } catch (ServletException ex) {
            Logger.getLogger(SecurityBacking.class.getName()).
                    log(Level.SEVERE, null, ex);
            result = "/loginError?faces-redirect=true";
        }
        return result;
    }

    public boolean isInRole(String role) {
        ExternalContext external = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) external.getRequest();
        String user = request.getRemoteUser();

        return request.isUserInRole("partner");
    }

    public void invalidate() {
        try {
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
            request.getSession(false).invalidate();
            response.sendRedirect(request.getContextPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
<小时/>

如你所见:

<h:commandButton value="log out" action="#{securityBacking.logout}" />

<h:commandButton value="Do log out" action="#{securityBacking.invalidate}" />

在layout/template.xhtml和index.xhtml中减速,但是在layout/template.xhtml中减速的按钮不能同时工作,它们在index.xhtml中工作。

当我查看 Safari Web Inspector 时,我看到:

在index.xhtml中减速

<input xmlns="http://www.w3.org/1999/xhtml" type="submit" name="j_idt12:j_idt29" value="log out" />

以及layout/template.xhtml

<input xmlns="http://www.w3.org/1999/xhtml" type="submit" name="j_idt32" value="log out" />

据我了解,bean 不会在标记内注入(inject) daclet 模板,但我在 j2ee 6 教程和规范中没有找到任何有关此内容的信息,或者也许 рфмут'е 注意到此类信息。

问题1:我关于注入(inject)的说法正确吗?

问题2:为什么不通过模板注入(inject)?

Q3:在这种情况下,模板化的不同方式是什么?

问题 4:此案例的最佳实践是什么?

(我正在使用 glassfish v3 网络服务器)

谢谢!

最佳答案

模板中的按钮未嵌套在 <h:form> 中。移动<h:form>将元素从您的index.xhtml移出并放入您的模板中,或者将模板按钮包装在辅助表单中。

关于java - JSF 2.0 MB 的模板化和注入(inject) || CDI bean ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3092892/

相关文章:

java - 无法从 Azure key 保管库获取证书 - 无法获取新的访问 token

c++ - 如何声明模板友元函数的特化

c++ - 来自 boost::multi_array 的二维数组 - 无法编译

ajax - 避免 richfaces 在 ajax 响应中发回 javascript 库

java - 字符串的replace all就是replace none

java - 外部库位于 APK 中的什么位置?

Java 将日期转换为其他格式

c++ - gcc 中 Unresolved 重载函数类型

Java 设计 : Locking and Monitor reports

jsf - IntelliJ 对包含父页面中声明的 EL 变量发出 "Cannot resolve variable"警告