c:if and c:choose fail to conditionally build ui:composition [duplicate] - jsf

This question already has answers here:
understand the purpose of jsf ui:composition
(1 answer)
How to include another XHTML in XHTML using JSF 2.0 Facelets?
(2 answers)
Closed 5 years ago.
Been struggling with this for a bit now.
I have an Apache Shiro login.xhtml page that loads at startup.
Once authenticated, the user is redirected to an index.xhtml page in the same folder as the Apache Shiro login.xhtml page (both are in the webapp folder).
I want to prevent non authenticated users from loading the UI by using this in my index.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://xmlns.jcp.org/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:p="http://primefaces.org/ui"
xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<c:if test="${authUserDetail.userAuthenticated}">
<ui:composition template="/WEB-INF/templates/masterTemplate.xhtml">
<ui:define name="title">Title Page</ui:define>
<ui:define name="content">
<ui:include src="/WEB-INF/home/home_page.xhtml"/>
</ui:define>
</ui:composition>
</c:if>
<c:if test="${!authUserDetail.userAuthenticated}">
<h:outputLabel
value="You are not authorized to access this page."/>
</c:if>
</html>
With a backing bean AuthUserDetail.java looking like this:
package com.mycomp.view.shiro;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.omnifaces.cdi.ViewScoped;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import java.io.Serializable;
#Named
#ViewScoped
public class AuthUserDetail implements Serializable {
private boolean userAuthenticated = false;
public AuthUserDetail() {
Subject currentUser = SecurityUtils.getSubject();
this.userAuthenticated = currentUser.isAuthenticated();
}
public boolean isUserAuthenticated() {
return userAuthenticated;
}
public void setUserAuthenticated(boolean userAuthenticated) {
this.userAuthenticated = userAuthenticated;
}
}
If I test without by replacing the <ui:composition... code (only using a single <h:outputLabel> it seems to work. But using it as is, always renders the complete page even if the user is not logged in (using Google Chrome's incognito feature).
What am I missing???

Related

Unable to resolve "Managed Bean" issue while working with Java Serves Faces 2.2 [duplicate]

This question already has an answer here:
Eclipse won't autocomplete bean methods in EL when I use javax.annotation.ManagedBean
(1 answer)
Closed 6 years ago.
Template Client.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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<body>
<f:view contracts="#{themeSelector.themeName}">
<ui:composition template="/template.xhtml">
<ui:define name="top">
<h:form>
<h:outputLabel value="Theme" for="menu"></h:outputLabel>
<h:selectOneMenu id="menu" label="ThemeMenu" value="#{themeSelector.themeName}">
<f:selectItem itemLabel="Dark" itemValue="dark"></f:selectItem>
<f:selectItem itemLabel="Normal" itemValue="normal"></f:selectItem>
</h:selectOneMenu>
<h:message for="menu"></h:message>
<h:commandButton id="Submit" value="Submit" action="templateClient"></h:commandButton>
</h:form>
</ui:define>
</ui:composition>
</f:view>
</body>
</html>
ThemeSelector.java
package com.rshingha.example;
import javax.annotation.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
/**
*
* #author rshingha
*/
#ManagedBean
#RequestScoped
public class ThemeSelector {
private String themeName="dark";
public ThemeSelector()
{
}
public String getThemeName() {
return themeName;
}
public void setThemeName(String themeName) {
this.themeName = themeName;
}
}
Issue:
Following line in TemplateClient.xhtml
<f:view contracts="#{themeSelector.themeName}">
Here value of "contracts" attribute is not getting resolved, even when I am doing "Ctrl+click" on property name "themeName", its not going to that property in bean file
Interesting thing is when I am hardcoding value for "contracts" attribute then its working.
I also tried with #Named annotation , but same is happening in that case with one exception:
When I am doing "Ctrl+click" on property name "themeName", its navigating to that property in bean file
Please suggest anything , I am stuck
You got wrong import for #ManagedBean. Use this one javax.faces.bean.ManagedBean from JSF. If you want to use #Named (which is CDI technology) then you should also change imports for scopes, request scope would be javax.enterprise.context.RequestScoped.

How many state information is saved when client mode

How to define the max number of component trees in client mode? How to prevent calling #PostConstruct method?
I'm developing an application by JavaEE7 with glassfish 4.1.
If I remember correctly, when javax.faces.STATE_SAVING_METHOD is client, there is no limit of component tree.
But when I opened more than 25 tabs in Chrome and operate the first tab, that tab's managed bean will construct and call #PostConstruct method.
I think this behavior seems losing component tree to me.
The following is source code of my application.
test.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">
<ui:composition
template="/template/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
>
<h:panelGrid columns="5" cellpadding="5" >
<p:commandButton id="returnButton" value="return"
action="#{testEditBean.return()}"
immediate="true"
/>
</h:panelGrid>
</ui:composition>
testBean.java
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
#Named(value = "testBean")
#ViewScoped
public class TestBean implements Serializable {
#PostConstruct
public void index() {
}
public String doReturn() {
String ret = "/content/tmp/testSearch.xhtml";
return ret;
}
}
[postscript 2016/01/28]
I understand that a HTTP session stored view scoped beans (max 25) and view state only stored component tree.
This is my new question.
Unless we change mojjara 2.x to another JSF implementation, we couldn't open more than 25tabs?
"more than 25 tabs" contains this case that "an user open 2 tabs, and 23 transition occur in 2nd tab."

f:param value is returning null both with commandButton and also commandLink

I am doing a simple navigation example in jsf as i am a beginner. i am always getting null when accessing the f:param value in the managedBean using ManagedProperty
home.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/facelets">
<head>
<title>JSF Tutorial!</title>
</head>
<body>
<h3>Using JSF outcome</h3>
<h:form>
<h:commandButton action="#{navigation.show}" value="Page1">
<f:param name="pageId" value="1" />
</h:commandButton>
<h:commandLink action="#{navigation.show}" value="Page2">
<f:param name="pageId" value="2" />
</h:commandLink>
<h:commandLink action="#{navigation.show}" value="Home">
<f:param name="pageId" value="3" />
</h:commandLink>
</h:form>
Navigation.java
package com.jason.jsf;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
#ManagedBean(name = "navigation", eager = true)
#RequestScoped
public class Navigation {
#ManagedProperty(value = "#{param.pageId}")
private String pageId;
public String show() {
System.out.println("page id" + value);
if (pageId == null) {
return "home";
}
if (pageId.equals("1")) {
return "page1";
} else if (pageId.equals("2")) {
return "page2";
} else {
return "home";
}
}
public String getPageId() {
return pageId;
}
public void setPageId(String pageId) {
System.out.println("page id set" + pageId);
this.pageId = pageId;
}
}
How is this caused and how can I solve it? I am using jsf2.2 Mojarra 2.0.3.there are other sample page1.xhtml and page2.xhtml just for navigation with me
Thanks in advance
Look closer at the XML namespace prefix and the URI and compare with whatever is shown in a decent JSF book/tutorial/resource:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/facelets">
Yes, the XML namespace URI for the f: prefix is wrong. You declared it to be the one of Facelets tags which have usually ui: prefix. This basically causes those tags to not be properly interpreted at all. It's being misinterpreted as an <ui:param> which has an entirely different meaning than the real <f:param>.
Fix the taglib URI. It needs to be http://java.sun.com/jsf/core. Here's the complete set:
<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:ui="http://java.sun.com/jsf/facelets">
See also:
Our JSF wiki page - contains a Hello World
Unrelated to the concrete problem, Mojarra 2.0.3 is not JSF 2.2. It's JSF 2.0. And a rather old implementation too, over 5 years already. You can get latest Mojarra 2.2 (currently 2.2.11) at http://javaserverfaces.java.net. After that, you can change the domain in taglib URIs from java.sun.com to xmlns.jcp.org:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

javax.el.PropertyNotFoundException JSF 2.1 Apach Tomcat Eclipse Kepler [duplicate]

This question already has answers here:
Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable
(18 answers)
Closed 7 years ago.
I am following some tutorial about Java EE, trying to do an simple example on my own. Even I type exactly the same in my code, it won't work. The difference is (between mine and the example in the tutorial) that i am using Eclipse Kepler (not NetBeans) and Apache Tomcat (not GlassFish).
HTTP Status 500 - javax.el.PropertyNotFoundException: Target Unreachable, identifier 'greetingManager' resolved to null
Here's the code: of the 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">
<h:head>
<title>Yo Digga Yo</title>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{greetingManager.name}" />
<h:commandButton action="greet" value="click" />
</h:form>
</h:body>
</html>
that's the GreetingManager class:
package com.maja.greeting;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
#Named
#SessionScoped
public class GreetingManager implements Serializable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGreeting() {
return "...";
}
}
ant finally, that's the greet.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">
<h:head></h:head>
<h:body>
<h2>#{greetingManager.greeting}</h2>
</h:body>
</html>
I've already imported the required .jar files for the #Named and #SessionScoped annotations, because tomcat doesn't provide them (?)
Ps. This tutorial is about CDI, so I kinda "have to" do this with the #Named Annotation :) And the code is not finished !
Your greetingManager class should be a managed bean in order to access it from your .xhtml file. To do so, you need to add the
#ManagedBean
annotation.

Target Unreachable, identifier 'helloBean' resolved to null [duplicate]

This question already has answers here:
Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable
(18 answers)
Closed 6 years ago.
am new to Java EE and trying to get a simple JSF 2 tutorial example to work.
I am using the dynamic web project in eclipse and publishing to a Glassfish 3 server (run -> run on server). The first index.xhtml page loads correctly, but when I have to access a managed bean, the following error displays:
/index.xhtml #14,48 value="#{helloBean.name}": Target Unreachable, identifier 'helloBean' resolved to null
I've had a look at the various other discussions on this topic, however the solutions never seem to work for me (e.g. adding beans.xml, giving the managed bean a name etc, following naming conventions).
Any help would be appreciated, as I have been stuck with this for a while.
Here is the code I am currently working with:
Index.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">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:form>
<h:inputText value="#{helloBean.name}"></h:inputText>
<h:commandButton value="Welcome Me" action="response"></h:commandButton>
</h:form>
</h:body>
</html>
response.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:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body bgcolor="white">
<h3>JSF 2.0 Hello World Example - welcome.xhtml</h3>
<h4>Welcome #{helloBean.name}</h4>
</h:body>
</html>
Managed bean :
package java.hello1;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
#ManagedBean
#SessionScoped
public class HelloBean implements Serializable {
private static final long serialVersionUID = 1L;
private String name = "Ricardo";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Change the "#ManagedBean" by "#Named"
In case anyone stumbled on this old thread, I got the code to work by replacing .name with .getName() // the variable is private while getName() is public

Resources