I’m trying run jsf page with value from bean:
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">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Simple JSF Facelets page</title>
</h:head>
<h:body>
<p> Text: #{testBean.text} </p>
</h:body>
</html>
TestBean:
#Named
#SessionScoped
public class TestBean implements Serializable {
private String text;
public TestBean(){
text = "Text" + new Date();
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
But when I run this page on JBoss I get only “Text:”. Source of page:
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>Simple JSF Facelets page</title></head><body>
<p> Text: </p></body>
</html>
Where can be problem? I'm using jboss-as-7.1.1.Final and Intellij IDEA.
Thanks!
Related
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 have been trying to print the values from an ArrayList<String> in JSF Facelets, but with no luck. The value is getting stored in an ArrayList but the output is blank on the page.
Bean file:
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
#ManagedBean(name="newCompanyName")
#SessionScoped
public class CompanyNames implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<String> list = new ArrayList<String>();
private String companyName;
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
list.add(companyName);
System.out.println("Size of array list is : "+list.size());
for (String number : list) {
System.out.println("Number = " + number);
}
}
public CompanyNames(String companyName) {
this.companyName = companyName;
}
public CompanyNames() {
}
}
Here is my JSF file from an XHTML 1.0 Transitional file:
<?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 xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
<ui:repeat var="item" value="#{CompanyNames.list}">
<h1>#{item}</h1>
</ui:repeat>
</h:body>
</html>
Instead of
<ui:repeat var="item" value="#{CompanyNames.list}"> <h1>#{item}</h1> </ui:repeat>
use
<ui:repeat var="item" value="#{newCompanyName.list}"> <h1>#{item}</h1> </ui:repeat>
You will also need a getList method for the class CompanyNames
public List<String> getList() {
return this.list;
}
Use companyNames.list instead of CompanyNames.list and you will also need the get() function of list.
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>
I think its a good practice to have an index page (in my case index.xhtml).
I want to pass some action on index page (for example in struts:<c:redirect url="list.do" /> and I go to struts action class without any links and buttons) I know if I want to use navigation I should use commandLink-s or buttons). I can write <h:commandButton> with onclick javascript function, but I don't feel this is the best option.
I'm totally new to JSF (using JSF 2.0) and I need your advice. What are the best practices for redirecting from index page to an action in controller?
///new version
<!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">
<f:view>
<ui:insert name="metadata"/>
<f:viewParam name="action" value="listItems.xtml"/>
<f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" />
<h:body></h:body>
</f:view>
</html>
public class ForwardBean {
private String action;
// getter, setter
public void navigate(PhaseEvent event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
String outcome = action;
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome);
}
}
You can use JSF preRenderView event to redirect to another page in following manner,
In your index.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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<ui:insert name="metadata"/>
<f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" />
<h:body></h:body>
</f:view>
</html>
In managed bean,
1st way is
public class yourClass{
FacesContext fc = FacesContext.getCurrentInstance();
ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)fc.getApplication().getNavigationHandler();
public void methodInManagedBean() throws IOException {
nav.performNavigation("list.do");//add your URL here, instead of list.do
}
}
or you can use 2nd way
public class yourClass{
public void methodInManagedBean() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect("list.do");//add your URL here, instead of list.do
}
}
I have main page that looks like this:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:panel id="panel">
<ui:include src="#{bean.page}">
</ui:include>
</p:panel>
<p:commandButton actionListener="#{bean.changePage}" value="Push" ajax="true" update="panel"/>
</h:form>
</h:body>
</html>
What I want to do is have a bean like this and change dynamically which page is included. This is how bean looks like:
#ManagedBean
#SessionScoped
public class Bean {
private String page = "";
public Bean() {
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public void changePage(ActionEvent e) {
page = "Page.xhtml";
}
}
When I click button Push I want this page to be included:
<?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:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<ui:component>
Hello from facelets
</ui:component>
</h:form>
</h:body>
Problem is that I have to press the button twice for page to be included. How can this be done so when I press the button the first time page is included?
This kind of templating is normally achieved using the <ui:insert> and <ui:define> tags. Check out this page of the Java EE tutorial.