[英]JSF 2.0 Temlating and injection of MB || CDI bean
Problem: managed bean not injecting by template. 问题:托管bean未按模板注入。
Goal: I wan to decelerate logout button in template. 目标:我希望减速模板中的注销按钮。
Scenario: I am building j2ee 6 application with jsf 2.0 for web part. 场景:我正在使用用于Web部件的jsf 2.0构建j2ee 6应用程序。
template file layout/template.xhtml 模板文件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>
tempalte client index.xhtml : 临时客户端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>
faces declaration in web.xml 面对web.xml中的声明
<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 : 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();
}
}
}
As you see : 正如你看到的 :
<h:commandButton value="log out" action="#{securityBacking.logout}" />
and 和
<h:commandButton value="Do log out" action="#{securityBacking.invalidate}" />
are decelerated in layout/template.xhtml & index.xhtml, but buttons decelerated in layout/template.xhtml not working in the sam time, they working in index.xhtml. 在layout / template.xhtml和index.xhtml中被减速,但是在layout / template.xhtml中减速的按钮在同一个时间内不起作用,它们在index.xhtml中起作用。
When i looking to Safari web inspector i see : 当我寻找Safari Web检查器时,我看到:
for decelerated in index.xhtml 在index.xhtml中减速
<input xmlns="http://www.w3.org/1999/xhtml" type="submit" name="j_idt12:j_idt29" value="log out" />
and for layout/template.xhtml 和layout / template.xhtml
<input xmlns="http://www.w3.org/1999/xhtml" type="submit" name="j_idt32" value="log out" />
As I understand bean not injecting daclets template inside tag, but I haven`t find anything about this in j2ee 6 tutorials & spec, or maybe рфмут'е notice such information. 据我了解bean没有在标签内注入daclets模板,但是我在j2ee 6教程和规范中没有找到任何关于此的信息,或者也许рфмут'е注意了这些信息。
Q1: Am I right about injection? Q1:我注射正确吗?
Q2: Why It is not injecting through templating ? Q2:为什么它不是通过模板注入?
Q3: What is the different way to templating in this case? 问题3:在这种情况下,模板制作有什么不同的方法?
Q4: What is the best practice for this case? 问题4:这种情况的最佳做法是什么?
(I am using glassfish v3 web server) (我正在使用glassfish v3 Web服务器)
Thank you ! 谢谢 !
The buttons in your template are not nested in an <h:form>
. 模板中的按钮未嵌套在
<h:form>
。 Move the <h:form>
elements out of your index.xhtml, and into your template, or wrap the template buttons in a secondary form. 将
<h:form>
元素移出index.xhtml并移至模板中,或将模板按钮包装为辅助形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.