jsf - Difference between h:commandLink and p:menuItem
问题描述
I already asked the question differently and it was answered as duplicate and the answer should be found in commandButton issue. I tried following all tipps, but in first place, the p:menuItem isn't really used in the examples and second in Debug Hints it apears that something is triggered, but I can't see what an my breakpoint at the beginning of the called method is never reached.
So I think I'm going to post way more code. This is the jsf-implementation which works:
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html">
<h:outputStylesheet library="css" name="menu.css"/>
<ul>
<li>
<h:commandLink value="Status" immediate="true"
action="status.xhtml?faces-redirect=true" />
</li>
<h:panelGroup rendered="#{aduserHandler.isAdmin}">
<li>
<h:commandLink value="Einstellungen" immediate="true"
action="parameter.xhtml?faces-redirect=true" />
</li>
</h:panelGroup>
<li>
<h:commandLink value="Abmelden" immediate="true"
action="#{aduserHandler.logout}" />
</li>
</ul>
</ui:composition>
Both methods isAdmin() and logout() are triggered and performed correctly.
This is the primefaces-implementation which isn't working:
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html">
<h:outputStylesheet library="css" name="menu.css"/>
<h:form id="menuform">
<p:growl id="messages" showDetail="true"/>
<div class="card">
<h5>Menu</h5>
<p:menu>
<p:submenu label="ATC">
<p:menuitem value="Status"
url="/atc/status.xhtml"
icon="pi pi-list"/>
</p:submenu>
<p:submenu label="KV">
</p:submenu>
<p:submenu label="Einstellung">
<p:menuitem value="Einstellungen"
url="/atc/parameter.xhtml"
icon="pi pi-cog"
rendered="#{aduserHandler.isAdmin}"/>
<p:menuitem value="Abmelden"
action="#{aduserHandler.logout}"
icon="pi-directions-alt"/>
</p:submenu>
</p:menu>
</div>
</h:form>
<h:commandLink value="Abmelden" immediate="true" action="#{aduserHandler.logout}" />
</ui:composition>
For testing purpose I even added the h:commandLink out of the first implementation but the logout() methode is in neither one of the buttons triggered. But to make it really weird, the isAdmin() methode works perfectly.
I tried menuitem with update="@form"
and update="menuform"
but neither worked.
I really want to understand how those menuitems work, because I wanted to use the possibility to code the menu in a class.
References to an own jQuery import have been deleted.
Edit: You were completely right. I've made the first attempt out of my JSF 101-Book and didn't realize that my layout.xhmtl goes like:
<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>
<h:outputStylesheet library="css" name="style.css" />
<title>Asset Tool Connector</title>
<link type="image/x-icon"
href="${facesContext.externalContext.requestContextPath}/resources/images/netzwerk.png"
rel="shortcut icon" />
</h:head>
<h:body>
<ui:insert name="header"/>
<f:event listener="#{aduserHandler.checkLoggedIn}" type="preRenderView" />
<h:form>
<div id="menu">
<ui:include src="menu.xhtml" />
</div>
<div id="main">
<ui:insert name="main"/>
</div>
</h:form>
<div id="footer">
© ATC - by 5332
</div>
</h:body>
</html>
But if I hadn't asked I had not looked anyway. Now what is better code for Primefaces? To leave the form around everything or to open a own form for every let's call it button group or function.
解决方案
I think the issue is that in the second case you have a h:form
embedded within another h:form
. (Point 2 on the list from @BalusC). I can't validate this because the calling code hasn't been posted, but it is suspicious when the code snippet for h:commandLink
doesn't include a form but needs one to work correctly, yet the PrimeFaces snippet does.
I'd also guess that the error is not apparent because in web.xml the javax.faces.PROJECT_STAGE
parameter is not set to Development
, meaning some warnings and errors are suppressed. See the first part of BalusC's list.
There are also probably no error handlers defined in your faces-config.xml file, which also means some errors get suppressed. See https://primefaces.github.io/primefaces/8_0/#/core/errorhandling.
When I replicate these circumstances in a simple JSF application using PrimeFaces 8 and Wildfly 21, I can see similar symptons to those you describe.
推荐阅读
- javascript - 如何使用表格列作为滑块
- asp.net-core-mvc - 重定向后 TempData 为空
- android - 打开动态模块时出现 NoClassDefFoundError
- c# - C#如何在泛型父类中调用重载运算符?
- c - 为什么 64 位系统没有像 creat 这样的系统调用?
- hibernate - Spring Boot 2 - hibernate 只获取输出参数的一部分
- mysql - 将包含日期 '10/13/2019 12:16:58 PM' 的字符串变量转换为 mysql 日期格式 ('yyyyy-mm-dd HH:mm:ss tt") vb.net 到 mysql 格式
- java - 从 .sql 加载模式以在 springboot 中创建 orm 映射
- ios - UINavigationBar 后退按钮在关闭搜索栏后总是变为蓝色
- python - 无法使用 Python 请求登录网站