I would like to work with icefaces, but as soon as I import the libraries of icefaces, my webapplication crashes after clicking a button on the form which worked properly before the import.
What could go wrong?
The source Java:
package test,
import javax.faces.bean.ManagedBean;
#ManagedBean
public class Student {
private String firstName;
// create no-arg constructor
public Student() {
}
// define getter/setter methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
index.xhtml:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Student Registration Form</title>
</h:head>
<h:body>
<h:form>
First name: <h:inputText value="#{student.firstName}" />
<br/><br/>
<h:commandButton value="Submit" action="student_response" />
</h:form>
</h:body>
student_response.xhtml
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Student Confirmation</title>
</h:head>
<h:body>
The student is confirmed: #{student.firstName}
</h:body>
</html>
The error I get:
you have 2 javax.faces. jars in classpath according to your first screenshot.
Please remove ONE of the faces jars in your classpath.
Related
I need to use <p:poll /> in some other work. So I was trying out the PrimeFaces ShowCase code:-
<!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"
xmlns:rich="http://richfaces.org/rich"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:a4j="http://richfaces.org/a4j">
<h:form>
<h:outputText id="txt_count" value="#{counterView.number}" />
<p:poll interval="3" listener="#{counterView.increment()}" update="txt_count" />
</h:form>
</html>
And the backing bean is as below:-
package com.poll;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean (name="counterView")
#ViewScoped
public class CounterView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int number;
public int getNumber() {
return number;
}
public void increment() {
number++;
System.out.println(number);
}
}
It works as such: in browser number shows as 0 and doesn't change. In console I can see it printing as 1 once and then nothing.
What is wrong here? I am using PrimeFaces 3.4.2 on JSF 2.1
The p:poll tag does work for me with the following content, and I am on Primefaces 5.2.
NOTE: <h:head/> tag is needed and without that it does load primefaces related js files that are needed. I believe that is the reason why the refresh is not working in your case.
<!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"
xmlns:p="http://primefaces.org/ui">
<h:head/>
<h:body>
<h:form>
<h:outputText id="txt_count" value="#{counterView.number}" />
<p:poll interval="3" listener="#{counterView.increment}" update="txt_count" />
</h:form>
</h:body>
</html>
And the managed bean:
#ManagedBean(name="counterView")
#ViewScoped
public class CounterView implements Serializable {
private int number = 100;
public int getNumber() {
return number;
}
public void increment() {
System.out.println("Incrementing....");
number++;
}
}
Can you try with these and compare if it works.
UPDATE: Final Solution
To summarize the solution, along the <h:head/> tag, upgrading to newer version (5.x) of Primefaces helped resolve the issue, as indicated in the comments section.
I want to parse two parameters from the URL but when I include two f:viewParam in header of page i get an error:
The metadata component needs to be nested within a f:metadata tag. Suggestion: enclose the necessary components within f:metadata
An example of a URL:
http://domain.com?email=blah#dom.com&key=4793e258-518f-432d-9af2-8d639a13757d
I'm using JSF 2.2.6 and I tried swapping the name space (below) from xmlns.jcp.org to java.sun.com but that made no difference.
My simplified page is:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:head>
<f:metadata>
<f:viewParam name="email" value="#{credentialsBacking.returnedEmail}"/>
<f:viewParam name="key" value="#{credentialsBacking.returnedActivationKey}"/>
</f:metadata>
</h:head>
<h:body>
</h:body>
</f:view>
</html>
Thanks
This works for me with no problem in Mojarra 2.2.6 and Tomcat 7:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<h:head>
<f:metadata>
<f:viewParam name="email" />
<f:viewParam name="key" />
</f:metadata>
</h:head>
<h:body>
#{email} - #{key}
</h:body>
</f:view>
</html>
Also this one does:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<h:head>
<f:metadata>
<f:viewParam name="email" value="#{bean1.email}" />
<f:viewParam name="key" value="#{bean1.key}" />
</f:metadata>
</h:head>
<h:body>
#{bean1.email} - #{bean1.key}
</h:body>
</f:view>
</html>
#ManagedBean
#ViewScoped
public class Bean1 {
public Bean1() {
System.out.println("Bean created");
}
private String email;
private String key;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
System.out.println(email);
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
System.out.println(key);
}
}
It doesn't matter where I place the <f:metadata /> inside the <html />, parameters are properly set.
Having seen your last comments, the issue seems to be related with your development tool not properly deploying resources to the server. Clean up your workspace and check the proper content is being copied.
Well, I don't know why, but I recreated the project from scratch and the issue went away. Curiously I had to use the name space of xmlns:f="http://java.sun.com/jsf/core" instead of xmlns:f="http://xmlns.jcp.org/jsf/core" to get the parameters to save to the bean.
i have my xhtml page below:
<?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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title> Portal Financeiro </title>
<link rel="icon" type="image/png" href="" />
</h:head>
<h:body>
<h:form id="form">
<h:inputText value="#{testeBean.name}" />
<h:commandButton value="Enviar" action="#{testeBean.enviar}" />
</h:form>
</h:body>
</html>
Behind my bean:
package br.com.teste.controller;
import javax.faces.bean.RequestScoped;
import javax.inject.Named;
#Named
#RequestScoped
public class TesteBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void enviar(){
System.out.println("NAME " + name );
}
}
The problem is: When i submit the form the value of the field name returns null.
My configuration:
I have the file beans.xml inside WEB-INF
I'm working with Jboss 7.1.0 or Glassfish 4.0 whatever gives the same problem.
You have a bad import, CDI annotations are in javax.enteprise.context package. Thus you should import javax.enteprise.context.RequestScoped. Or for this particular combination (#RequestScoped + #Named) you can use built-it stereotype called #Model.
I am using facelets in my views with JSF 2.0. I have two backing beans, two xhtml view files and another xhtml template file. When I have the second view in the webapp directory and I change the language all is Ok but when I put mi second view into the WEB-INF folder and I change the language I have the following error in the chrome network console:
POST http://localhost:8080/Languages/WEB-INF/inventory.xhtml 404 (No Encontrado)
These are my backings beans:
LanguageBean.java:
package com.testapp;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
#ManagedBean(name = "language")
#SessionScoped
public class LanguageBean implements Serializable {
private static final long serialVersionUID = 1L;
private String localeCode;
private static final String DEFAULT_LOCAL_CODE = "es";
private static Map<String, Object> countries;
static {
countries = new LinkedHashMap<String, Object>();
countries.put("English", Locale.ENGLISH); // label, value
countries.put("Français", Locale.FRENCH);
countries.put("Español", new Locale("es"));
}
public Map<String, Object> getCountriesInMap() {
return countries;
}
public String getLocaleCode() {
if(localeCode == null) {
localeCode = DEFAULT_LOCAL_CODE;
}
return localeCode;
}
public void setLocaleCode(String localeCode) {
this.localeCode = localeCode;
}
public void changeLanguage() {
// loop a map to compare the locale code
for (Map.Entry<String, Object> entry : countries.entrySet()) {
if (entry.getValue().toString().equals(localeCode)) {
FacesContext.getCurrentInstance().getViewRoot().setLocale((Locale) entry.getValue());
}
}
}
}
InventoryBean.java:
package com.testapp;
import java.io.Serializable;
import java.util.Locale;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
#ManagedBean(name = "inventory")
#ViewScoped
public class InventoryBean implements Serializable {
private static final long serialVersionUID = 4576404491584185639L;
Logger logger = LoggerFactory.getLogger(InventoryBean.class);
// Failing path
private static final String INVENTORY_MAIN_VIEW = "/WEB-INF/inventory";
// Working path
// private static final String INVENTORY_MAIN_VIEW = "/views/inventory";
public String findProducts() {
try {
System.err.println("In inventory backing bean");
} catch (Exception exception) {
}
return INVENTORY_MAIN_VIEW;
}
public void changeLanguage() {
FacesContext.getCurrentInstance().getViewRoot().setLocale((Locale.ENGLISH));
}
}
These are the view files:
default.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/header.xhtml">
<ui:define name="contentBody">
<f:view locale="#{language.localeCode}">
<h:form id="contentForm">
<h:outputText value="#{msg['internationalization.test']}" />
<p:commandButton id="gettingProducts"
value="Getting the products..." action="#{inventory.findProducts}" />
</h:form>
</f:view>
</ui:define>
</ui:composition>
inventory.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/header.xhtml">
<ui:define name="contentBody">
<f:view locale="#{language.localeCode}">
<h:form id="contentForm">
<h:outputText value="#{msg['internationalization.test']}" />
</h:form>
</f:view>
</ui:define>
</ui:composition>
And this is the template xhtml file (header.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"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view contentType="text/html">
<h:head>
</h:head>
<h:body>
<div id="content">
<h:form id="headerForm">
<h:panelGrid columns="2">
<p:outputLabel value="Language :" for="languages" />
<h:selectOneMenu required="true" id="languages"
value="#{language.localeCode}">
<f:selectItems value="#{language.countriesInMap}" />
<p:ajax event="change" update="#all"
listener="#{language.changeLanguage}" />
</h:selectOneMenu>
</h:panelGrid>
</h:form>
<ui:insert name="contentBody" />
</div>
</h:body>
</f:view>
</html>
What is happening?
A HTTP 404 error simply means "Page Not Found". And indeed, files inside /WEB-INF folder are not publicly accessible. Put files which are intented to be publicly accessible outside /WEB-INF folder.
Note that this has further completely nothing to do with internationalization. You'd have had exactly the same problem when not doing anything with regard to internationalization (e.g. a simple navigation).
See also:
Which XHTML files do I need to put in /WEB-INF and which not?
Here is my project structure:
eclipse project structure
this is my HelloWorld.java
package com.tutorialspoint.test;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name = "helloWorld")
#RequestScoped
public class HelloWorld
{
public HelloWorld()
{
System.out.println("HelloWorld started!");
}
public String getMessage()
{
return "JSF2!";
}
}
and this is my index.xhtml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
HEllo form <h:outputLabel value="#{helloWorld.getMessage()}" />
</h:body>
</h:body>
</html>
the output that i get after entering localhost:8080/demojsf/ is HEllo form and not
HEllo from jsf2.
What is wrong here?
Below is the way of using h:outputLabel using for attribute of h:outputLabel
public class HelloWorld
{
public String message= "JSF2!";
public HelloWorld()
{
System.out.println("HelloWorld started!");
}
public String getMessage()
{
return message;
}
}
<h:outputLabel for="msgID" value="HEllo form " />
<h:outputText id="msgID" value="#{helloWorld.message}"/>
The h:outputLabel will be calling the getter method of the attribute. So change the code as below,
<h:outputLabel value="#{helloWorld.message}" />
Please find below a working code sample for me,
<!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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<head>
<title>JSF Hello World</title>
</head>
<body>
<f:view>
<h:form>
<h2>
<h:outputLabel value="#{jsfHelloWorldBean.message}" />
</h2>
</h:form>
</f:view>
</body>
</html>
And my bean
package devmanuals;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name="jsfHelloWorldBean")
#RequestScoped
public class JsfHelloWorldBean {
//String message;
public String getMessage(){
return "JSF!2";
}
}
your code works fine on my side. I used JSF2.1, JDK7, and Netbeans 7.3, well except for the double </h:body>