I'm completely new in JSF. I'm using this tutorial: https://www.tutorialspoint.com/jsf
According to it I created a first project. Here's the Java code:
package com.tutorialspoint.test;
import javax.faces.bean.ManagedBean;
#ManagedBean(name = "helloWorld", eager = true)
public class HelloWorld {
public HelloWorld() {
System.out.println("HelloWorld started!");
}
public String getMessage() {
return "Hello World!";
}
}
this is home.xhtml file:
<!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">
<head>
<title>JSF Tutorial!</title>
</head>
<body>
#{helloWorld.getMessage()}
</body>
</html>
when I enter this address in my browser: http://localhost:8080/helloworld/home.jsf the tab's title "JSF Tutorial!" is displayed but the content (the text "Hello World" not. Could I ask you for some hints what can be missing?
If you need some more information/code from other files, just let me know.
BTW, I'm using Wildfly 10.1.0 as an application server where I deploy the updated .war file.
Thanks in advance.
Related
This question already has answers here:
Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable
(18 answers)
Closed 4 years ago.
After digging through links such as the following, I'm unable to solve the problem: Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable
I am getting the following error: javax.servlet.ServletException: /Project9.xhtml #13,55 value="#{ProjectBean.income}": Target Unreachable, identifier [ProjectBean] resolved to null
My form:
<!DOCTYPE html>
<html xmlns="http://wwww3.org/1999/xhtml"
xmlns:a="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
>
<h:head>
<title>Project9</title>
</h:head>
<h:body>
<h:form>
Income: <h:inputText value="#{ProjectBean.income}"/>
Number of people: <h:inputText value="#{ProjectBean.numPeople}"/>
<h:commandButton value= "Submit" action= "Project9response"/>
</h:form>
</h:body>
</html>
I am not sure whether the right convention is wwww3.org or www.3.org but I have tried them both.
My Response page:
<!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:h="http://xmlns.jcp.org/jsf/html"
>
<h:head>
<title><ui:insert name="title">Project 9 response</ui:insert></title>
</h:head>
<h:body>
Am I above the poverty level: #{ProjectBean.abovePovertyLevel()}
</h:body>
</html>
My Bean:
#ManagedBean
#SessionScoped
public class ProjectBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private double income;
private double numPeople;
//constructor is no arg
public ProjectBean() {
}
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
public double getNumPeople() {
return numPeople;
}
public void setNumPeople(double numPeople) {
this.numPeople = numPeople;
}
public boolean abovePovertyLevel() {
if ( income < (16460.00 + 4320.00) * (numPeople - 2)){
return false;
}
else {
return true;
}
}
}
My faces-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
version="2.3">
</faces-config>
I have my faces-config.xml and my javax-faces.2.2.8.jar file in the lib folder of WEB-INF
I have my bean in the src folder of Java Resources
I have another project called helloworld with several little JSF projects that all work, and I have tried copying project9 into helloworld and running it, but I get the same error only on that project
I have tried cleaning my project as people have suggested
This is a student project and my first introduction to JSF and Tomcat. I'm using a Mac and Eclipse photon.
As I stated in a comment in my code, if I try to bypass the response xhtml file and go straight to the javabean method in my html form, I get an underline under ProjectBean.abovePovertyLevel() and the error "the action value does not match a navigation case outcome"
I'm not sure of some of the answers in the link above regarding CDI, etc, that all is above my head at this point.
Nevermind, I JUST solved this
Added a name to my annotation as such: #ManagedBean(name="ProjectBean")
Also I realized that my method was wrong if I didn't declare poverty level first
int povertyLevel = (16460 + 4230 * (numPeople-2));
if(income > povertyLevel)
return true;
else{
return false;
}
This seemed to work
Replace all the #{ProjectBean. by #{projectBean. (make the first character lowercase).
This is because you need to refer to the managed bean by name, not by the classname. By default, jsf will generate this name for you, as per the docs at https://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedBean.html :
If the value of the name attribute is unspecified or is the empty String, the managed-bean-name is derived from taking the unqualified class name portion of the fully qualified class name and converting the first character to lower case. For example, if the ManagedBean annotation is on a class with the fully qualified class name com.example.Bean, and there is no name attribute on the annotation, the managed-bean-name is taken to be bean.
I have a page containing a p:commandLink that calls a very simple method on a session-scoped backing bean. The backing bean method simply logs a message to the console and redirects to a welcome page.
When run, the link does nothing. There are no errors reported in h:messages and no stack traces on the server console. However, if I use the exact same code on another page then the link works fine.
The main difference between the pages is that the page containing the 'dead' link is protected by a login filter (the page is in the 'restricted' folder) whereas the page that contains the 'working' link is not in the restricted folder.
I've followed the advice in several other threads about action links that don't work. I don't have nested forms or rendering problems, etc. I'm wondering if the login filter may be having some effect on the link.
Here's the page containing the link that doesn't work (it resides in location: restricted/secret.xhtml). I've deliberately minimised the contents to eliminate other potential causes.
<?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"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Restricted Page</title>
</h:head>
<h:body>
<h:form>
<h:messages />
<br/>
<p:commandLink action="#{loginBean.testMethod}" value="Test" />
</h:form>
</h:body>
</html>
Here's the method in the backing bean (which DOES work if called using the exact same commandLink code on an unprotected page:
public String testMethod() {
logger.info("xxxxxxxxxxxxxx Test method called xxxxxxxxxxxxxx");
return("WelcomeMember.xhtml");
}
The login filter is as follows:
package com.mymato.coop;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebFilter("/restricted/*")
public class AuthenticationFilter implements Filter {
#SuppressWarnings("unused")
private FilterConfig config;
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
if (((HttpServletRequest) req).getSession().getAttribute(
LoginBean.AUTH_KEY) == null) {
((HttpServletResponse) resp).sendRedirect("../login.xhtml");
} else {
chain.doFilter(req, resp);
}
}
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
public void destroy() {
config = null;
}
}
For future reference, is there a good way to debug this type of problem? I've run into the dead link problem a few times now. It's very difficult to solve because it fails silently. At least if there was an error message then that would be a clue.
I need to convert a JSP file to an equivalent JSF file. The JSP is as follows:
Step 1: Class Import:
<%# page import="org.keycloak.constants.ServiceUrlConstants" %>
<%# page import="org.keycloak.common.util.KeycloakUriBuilder" %>
<%# page session="false" %>
<html>
Step 2: Define a variable:
<%
String logoutUri = KeycloakUriBuilder.fromUri("/auth").path(ServiceUrlConstants.TOKEN_SERVICE_LOGOUT_PATH).queryParam("redirect_uri", "/customer-portal").build("demo").toString(); %>
Step 3: Then refers to this variable:
logout
The imported library is an external library into the project. In JSF, I know how to do Step 3. But I don't know how to import the classes in Step 1 and how to define a variable as shown in Step 2 in JSF.
Is there an equivalent way of performing Step 1-3 in JSF? Thank you.
You can't call Methods directly in JSF or create variables, therefore you don't need imports.
The only way is to use EL-Expressions.
Since calling static Methods is not possible with EL, you'll have to create yourself a Bean, that makes the call of KeycloakUriBuilder.fromUri...
With a Named Bean you call its Methods:
Example:
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
//This Bean is available by default under the Name 'myBean', you can change it in the #Named Annotation if desired
#Named
#RequestScoped
public class MyBean implements Serializable {
public String myMethod(String inupt){
return "Hello " + input;
}
}
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core" >
<head></head>
<body>
<h:outputText value ="#{myBean.myMethod('world')}"/>
</body>
</html>
Will give you this HTML:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>Hello world
</body>
</html>
The preferred way to show Something on the page is to use getters and setters, if you have a field with getter and Setter
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
You can just use
#{myBean.name}
JSF will call the getter if it needs the value (for output) or call the Setter if there is a value to set (from input-fields)
The <%# page session="false" %> is also neither needed nor possible.
In JSF the Bean has a Scope, this example with RequestScoped should be a good match for <%# page session="false" %> - a Request Scoped Bean lives only for one Request, after that the Bean is disposed. There are many other scopes, e.g. #SessionScoped (from javax.enterprise.context) for a Bean that lives as long as the Session is active.
As mentioned by another User, those Scopes exists in the CDI-Variant (Package javax.enterprise.context) and a JSF-variant (package javax.faces.bean). You should use the CDI-Variant, since the JSF-Variant might be deprecated soon (see here).
Explanation of the Scopes see here.
In a backing bean I declared following method
public boolean hasPermission(Object... objects) {
...
}
And I'm trying to call it from JSF 2.0 as follows:
<c:set var="hasPermission" scope="view" value="#{restrictions.hasPermission(entity)}" />
And it throws
javax.el.ELException: Cannot convert Entity of class com.testing.Entity to class [Ljava.lang.Object;
If I pass two arguments, then it throws
Method hasPermission not found
Can I somehow call varargs methods from JSF 2.0?
Varargs is not officially supported in EL. At least, it's nowhere specified in EL specification. There does also not seem to be any plans to introduce it in the upcoming EL 3.0.
You need to look for a different solution. As the functional requirement is unclear, I can't suggest any one.
Update it seems that the Apache EL parser as supplied in Tomcat supports this. It at least isn't supported by Sun/Oracle EL parser as supplied in Glassfish.
On Tomcat 7 JSF 2.1.4 following works
<?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"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h:form>
<h:commandButton value="click 1"
action="#{test.var('a','b',1,test.i,test.d,test.s,test.ss)}"/>
<h:commandButton value="click 2"
action="#{test.var('a','b',1)}"/>
<h:commandButton value="click 3"
action="#{test.var(test.i,test.d,test.s,test.ss)}"/>
</h:form>
</body>
</html>
The Bean:
#ManagedBean
public class Test {
private Integer i = 10;
private Double d = 10.0;
private String s = "varargs";
private String[] ss = new String[]{"1","2","3"};
public Integer getI() {
return i;
}
public void setI(Integer i) {
this.i = i;
}
public Double getD() {
return d;
}
public void setD(Double d) {
this.d = d;
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
public String[] getSs() {
return ss;
}
public void setSs(String[] ss) {
this.ss = ss;
}
public void var(Object...objects){
System.out.println(Arrays.toString(objects));
}
}
Output : on click 1,2,3
[a, b, 1, 10, 10.0, varargs, [Ljava.lang.String;#4fa9cba5]
[a, b, 1]
[10, 10.0, varargs, [Ljava.lang.String;#26b923ee]
Is this what you are looking for.... as the way you are trying to call in question is blank.
In the following form, we try to return a user's input to JSF's h:inputText or PrimeFaces' p:inputText.
We experience strange behavior when non-Latin characters (Japanese, Hebrew, etc. ) are entered:
On first request we get unrecognized character set, but on the second request - we get a correct result.
Input/Output Examples (first run only):
Japanese:
input = 日
output = æ¥
Hebrew:
input = א
output = ×
JSF:
<?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"
xmlns:p="http://primefaces.prime.com.tr/ui">
<body>
<h:form>
<h:outputLabel value="Name:"/>
<h:inputText value="#{newTestController.registeredCustomerFirstName}"/>
<h:commandButton value="Continue" action="#{newTestController.RegisteredNewCustomer(actionEvent)}"/>
</h:form>
</body>
</html>
Backing Bean:
#ManagedBean(name = "newTestController")
#SessionScoped
public class NewTestController {
private String registeredCustomerFirstName;
public String getRegisteredCustomerFirstName() {
return registeredCustomerFirstName;
}
public void setRegisteredCustomerFirstName(String registeredCustomerFirstName) {
this.registeredCustomerFirstName = registeredCustomerFirstName;
}
public void RegisteredNewCustomer(ActionEvent actionEvent) throws Exception {
}
}
As commented above - it is needed to define a default-charset for the application server.
For glassfish: add <parameter-encoding default-charset="UTF-8" /> to glassfish-web.xml.
For other application servers see BalusC's blog regarding this issue.
This is related to < http://java.net/jira/browse/GLASSFISH-18007 >. That fix was made to prevent a warning message when we unconditionally set the encoding to UTF-8, which would seem to be what we want, but in this case we felt it safer to not do it.
I've created a related issue in Mojarra, < http://java.net/jira/browse/JAVASERVERFACES-2217 >. Bottom line: setting the encoding explicitly in the app configuration is the right solution. The implementation is already doing the right thing.
Specifying charset in the config file might be not enough.
Try using p:commandButton instead of h:commandButton. The p:commandButton by default uses ajax, while the h:commandButton does non-ajax submit.