問題: マネージド Bean がテンプレートによって注入されません。
目標: テンプレートのログアウト ボタンを遅くしたい。
シナリオ: Web パーツ用の jsf 2.0 を使用して j2ee 6 アプリケーションを構築しています。
テンプレートファイル layout/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 での face 宣言
<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 インスペクターを見ると、次のように表示されます。
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 がタグ内にダクレット テンプレートを注入していないことは理解していますが、j2ee 6 のチュートリアルと仕様でこれについて何も見つけていないか、そのような情報に気付くかもしれません。
Q1: 注射については正しいですか?
Q2: テンプレートを介して注入しないのはなぜですか?
Q3: この場合、テンプレート化の別の方法は何ですか?
Q4: この場合のベスト プラクティスは何ですか?
(私はglassfish v3 Webサーバーを使用しています)
ありがとうございました !