I have made a Login.xhtml by using Apache Myfaces 2.1 (using JSF 1.x)
When I have given my username and password on login.xhtml and one submit button is there.
When I click on goodbye button, the control goes to wsListing.xhtml but the URL in the browser remain as
http://hostid:8080/TestClient/faces/login.xhtml
I want it to be changed as
http://hostid:8080/TestClient/faces/wsListing.xhtml
I have attached the code for the same, please suggest me some solution, I am new to JSF.
Login.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"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" xmlns:lang="en">
<f:view>
<head>
<title>Test Client Login</title>
</head>
<h:form id="loginForm">
<table>
<tr>
<h:outputLabel for="username">
<h:outputText id="usernameLabel" value="Enter Username:" />
</h:outputLabel>
<h:inputText id="username" value="#{loginBean.username}"
required="true">
<f:validateLongRange minimum="1" maximum="500" />
</h:inputText>
</tr>
<tr>
<h:outputLabel for="password">
<h:outputText id="passwordLabel" value="Enter Password:" />
</h:outputLabel>
<h:inputText id="password" value="#{loginBean.password}"
required="true">
<f:validateLongRange minimum="1" maximum="500" />
</h:inputText>
</tr>
<tr>
<h:commandButton id="goodbyeCommand" type="submit" value="Goodbye"
action="#{loginBean.goodbye}" immediate="true" />
</tr>
</table>
</h:form>
</f:view>
</html>
LoginBean.java
package com.example;
/**
* Managed Bean for Login
*
*/
public class LoginBean {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String goodbye() {
return "success";
}
}
faces-config.xml
<?xml version="1.0"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<managed-bean>
<description>Login Bean</description>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>com.example.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<description>Navigation from the hello page.</description>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/wsListing.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
You need to send a redirect. Add <redirect /> to the <navigation-case />.
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/wsListing.xhtml</to-view-id>
<redirect />
</navigation-case>
Unrelated to the concrete problem, MyFaces 2.1 is a JSF 2.1 implementation. Why are you forcing the webapp to run in JSF 1.2 modus? You miss so many JSF 2.x advantages.
Related
I am trying to pass the values between xhtml pages using the Request Scoped JSF Managed Bean, GendersManager.java in jsf as follows:
Page Flow:
index.xhtml ---hyper link---> GendersInsertCreateKeyin.xhtml ---Confirm---> GendersInsertCreateConfirm.xhtml ---Commit---> GendersInsertCreateCommitted.xhtml ---Committed---> index.xhtml
Issue:
The field vaues are shown in "GendersInsertCreateConfirm.xhtml" (ie.during 1st navigation) but field values become null/empty string in GendersInsertCreateCommitted.xhtml (ie. during 2nd navigation) during the page flows.
Update:
a) Converting the user interfaces, h:outputText to h:inputText in GendersInsertCreateConfirm.xhtml retains field values during the page flows.
b) After Converting the user interfaces, h:outputText to h:inputText and setting its parameter readonly=true, field values become null/empty string during the page flows.
c) tried to set the values to the member variables of jsf managed bean,GendersManager with the following javascript code from GendersInsertCreateConfirm.xhtml:
<h:commandButton id="insertCreateConfirmBtnUi" value="Genders Insert Create Confirm" action="#{gendersManager.insertCreateConfirm}"/></td>
<f:verbatim>
<script type="text/js" language="javascript">
var genderSlNoUiText = document.getElementByid("genderSlNoUi").getValue();
</script>
<f:setPropertyActionListener target="#{gendersManager.genderSlNo}" value=genderSlNoUiText/>
<f:verbatim>
</h:commandButton>
, but throws error that value should have quotes
Please guide me in passing values between xhtml pages using the jsf managed bean.
References:
faces-config.xml:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
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_2.xsd">
<navigation-rule>
<navigation-case>
<from-action>#{gendersManager.insertCreateKeyin}</from-action>
<from-outcome>Confirm</from-outcome>
<to-view-id>GendersInsertCreateConfirm.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>GendersInsertCreateConfirm.xhtml</from-view-id>
<navigation-case>
<from-action>#{gendersManager.insertCreateConfirm}</from-action>
<from-outcome>Commit</from-outcome>
<to-view-id>GendersInsertCreateCommitted.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>GendersInsertCreateCommitted.xhtml</from-view-id>
<navigation-case>
<from-action>#{gendersManager.insertCreateCommitted}</from-action>
<from-outcome>Committed</from-outcome>
<to-view-id>index.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
JSF Managed Bean:
GendersManager.java:
package com.practice.web;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ManagedBean;
#ManagedBean
#RequestScoped
public class GendersManager {
private Long genderSlNo;
public String insertCreateKeyin(){
Genders genders = new Genders ();
genders.setGenderSlNo(getGenderSlNo());
return "Confirm";
}
public String insertCreateConfirm(){
Genders genders = new Genders ();
genders.setGenderSlNo(getGenderSlNo());
return "Commit";
}
public String insertCreateCommitted(){
Genders genders = new Genders ();
genders.setGenderSlNo(getGenderSlNo());
return "Committed";
}
public Long getGenderSlNo() {
return genderSlNo;
}
public void setGenderSlNo(Long genderSlNo) {
this.genderSlNo = genderSlNo;
}
}
XHTML Pages:
GendersInsertCreateKeyin.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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Genders Insert Create Keyin</title>
</h:head>
<h:body>
<h:form>
<table border="1">
<tr>
<td><h:outputLabel for="genderSlNoUi" id="genderSlNoLbl" value="Gender Sl No"/></td>
<td><h:inputText id="genderSlNoUi" value="#{gendersManager.genderSlNo}"/></td>
</tr>
<tr>
<td align="right"><h:commandButton id="insertCreateKeyinBtnUi" value="Insert Genders" action="#{gendersManager.insertCreateKeyin}"/></td>
</tr>
</table>
</h:form>
</h:body>
</html>
GendersInsertCreateConfirm.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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Genders Insert Create Confirm</title>
</h:head>
<h:body>
<h:form>
<table border="1">
<tr>
<td><h:outputLabel for="genderSlNoUi" id="genderSlNoLbl" value="Gender Sl No"/></td>
<td><h:outputText id="genderSlNoUi" value="#{gendersManager.genderSlNo}"/></td>
</tr>
<tr>
<td align="right"><h:commandButton id="insertCreateConfirmBtnUi" value="Genders Insert Create Confirm" action="#{gendersManager.insertCreateConfirm}"/></td>
</tr>
</table>
</h:form>
</h:body>
</html>
GendersInsertCreateCommitted.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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Genders Insert Create Committed</title>
</h:head>
<h:body>
<h:form>
<table border="1">
<tr>
<td><h:outputLabel for="genderSlNoUi" id="genderSlNoLbl" value="Gender Sl No"/></td>
<td><h:outputText id="genderSlNoUi" value="#{gendersManager.genderSlNo}"/></td>
</tr>
<tr>
</tr>
</table>
</h:form>
</h:body>
</html>
This question already has answers here:
How to choose the right bean scope?
(2 answers)
Closed 5 years ago.
When I click on the button I'm getting "record saved" message on another page named "page1.html" as mentioned in JSF faces configuration file, but the record is null for string and 0 for integers. Where am I getting wrong?
my managed bean code(Inventory.java):-
import java.awt.Frame;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.swing.JOptionPane;
/**
*
* #author Pritam
*/
#Named(value = "inventory")
#Dependent
public class Inventory {
String item;
int price, qty;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public Inventory() {
}
public String addItem()
{
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/practice");
Statement ps=con.createStatement();
ps.executeUpdate("insert into inv_table values('"+getItem()+"',"+getPrice()+","+getQty()+")");
return "Success";
}
catch(Exception e){
return "Failed";
}
}
}
this is my jsf page(MyIndex.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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<pre>
Item Name: <h:inputText id="t1" value="#{inventory.item}" size="10">
</h:inputText>
Item Price: <h:inputText id="t2" value="#{inventory.price}" size="10">
<f:convertNumber type="number"></f:convertNumber>
</h:inputText>
Item Quantity: <h:inputText id="t3" value="#{inventory.qty}" size="10">
<f:convertNumber type="number"></f:convertNumber>
</h:inputText>
<h:commandButton id="btn" value="add item" action="#{inventory.addItem()}">
</h:commandButton>
</pre>
</h:form>
</h:body>
</html>
JSF faces configuration file:-
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
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_2.xsd">
<navigation-rule>
<from-view-id>MyIndex.xhtml</from-view-id>
<navigation-case>
<from-action>#{inventory.addItem()}</from-action>
<from-outcome>Success</from-outcome>
<to-view-id>page1.html</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{inventory.addItem()}</from-action>
<from-outcome>Failed</from-outcome>
<to-view-id>page2.html</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
try your codes with binding="" instead of value=""
Item Name: <h:inputText id="t1" binding="#{inventory.item}" size="10">
</h:inputText>
Item Price: <h:inputText id="t2" binding="#{inventory.price}" size="10">
<f:convertNumber type="number"></f:convertNumber>
</h:inputText>
Item Quantity: <h:inputText id="t3" binding="#{inventory.qty}" size="10">
<f:convertNumber type="number"></f:convertNumber>
</h:inputText>
I have a button, when i click a modal panel opened - it's work fine. Now i tried to add a button to hide the panel - it's work also, but the problem is when i tried to show a text "panel closed" after button click it doesn't work. I use Jsf 1.2 and richfaces 3.3.3.
I have the following error message:
org.apache.jasper.el.JspELException: /index.jsp(35,7) 'javascript:Richfaces.hideModalPanel('myModalPanel');#{welcomeBean.showText(true)}' Method not found: class com.firstjsf.backingbeans.WelcomeBean.showText(java.lang.Boolean) at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:123)
above the code:
index.jsp
<%#taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%#taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%# taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%# taglib uri="http://richfaces.org/rich" prefix="rich"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<f:view>
<h:form>
<rich:panel>
<f:facet name="header">
<h:outputText value="Product"></h:outputText>
</f:facet>
<h:form>
<a4j:commandButton
id="newWid"
value="New Widget..."
immediate="true" ajaxSingle="true"
reRender="text"
oncomplete="javascript:Richfaces.showModalPanel('myModalPanel');"
styleClass="verboseButton noprint" />
</h:form>
<rich:modalPanel id="myModalPanel">
<f:facet name="header">
<h:outputLabel value="123" />
</f:facet>
From Modal Panel
<a4j:commandButton value="Hide" id="btn_hide"
oncomplete="javascript:Richfaces.hideModalPanel('myModalPanel');#{welcomeBean.setShowText(true)}" />
</rich:modalPanel>
<h:outputText id="text"
value="Panel closed"
rendered="#{helloMessage.showText eq true}">
</h:outputText>
</rich:panel>
</h:form>
</f:view>
</html>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>welcomeBean</managed-bean-name>
<managed-bean-class>com.firstjsf.backingbeans.WelcomeBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>helloMessageBean</managed-bean-name>
<managed-bean-class>com.firstjsf.backingbeans.HelloMessageBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<description>Welcome page to message page</description>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>helloMessage</from-outcome>
<to-view-id>/message.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<description>Welcome page to message page</description>
<from-view-id>/message.jsp</from-view-id>
<navigation-case>
<from-outcome>back</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
BeanAction
public class WelcomeBean {
private boolean showText =false;
public String sayHello(){
return "helloMessage";
}
public boolean isShowText() {
return showText;
}
public void setShowText(boolean showText) {
this.showText = showText;
}
}
You're mixing JavaScript and EL expressions, #oncomplete is for executing JavaScript, if you want to do something on the server use #action or #actionListener. Otherwise the expression will be evaluated and the browser will try to execute the return value as if it was JavaScript.
By the way, your code shows you're using setShowText(true) (which is correct), but the exception says you're using just showText(true), which is it then?
Im trying to test a sample JSF project but for some reason the command button are not being displayed that is given in the below page,
<?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:body>
<h2>Implicit Navigation</h2>
<hr />
<h:form>
<h3>Using Managed Bean</h3>
<h:commandButton action="#{navigationController.moveToPage1}"
value="Page1" />
<h3>Using JSF outcome</h3>
<h:commandButton action="page2" value="Page2" />
</h:form>
<br/>
<h2>Conditional Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.showPage}"
value="Page1">
<f:param name="pageId" value="1" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Page2">
<f:param name="pageId" value="2" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Home">
<f:param name="pageId" value="3" />
</h:commandLink>
</h:form>
<br/>
<h2>"From Action" Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.processPage1}"
value="Page1" />
<h:commandLink action="#{navigationController.processPage2}"
value="Page2" />
</h:form>
<br/>
<h2>Forward vs Redirection Navigation</h2>
<hr />
<h:form>
<h3>Forward</h3>
<h:commandButton action="page1" value="Page1" />
<h3>Redirect</h3>
<h:commandButton action="page1?faces-redirect=true"
value="Page1" />
</h:form>
</h:body>
</html>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtm</url-pattern>
</servlet-mapping>
</web-app>
faces.config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
<from-view-id>home.xhtml</from-view-id>
<navigation-case>
<from-action>#{navigationController.processPage1}</from-action>
<from-outcome>page</from-outcome>
<to-view-id>page1.jsf</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{navigationController.processPage2}</from-action>
<from-outcome>page</from-outcome>
<to-view-id>page2.jsf</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
view source html file
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h2>Implicit Navigation</h2>
<hr />
<h:form>
<h3>Using Managed Bean</h3>
<h:commandButton action="#{navigationController.moveToPage1}"
value="Page1" />
<h3>Using JSF outcome</h3>
<h:commandButton action="page2" value="Page2" />
</h:form>
<br/>
<h2>Conditional Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.showPage}"
value="Page1">
<f:param name="pageId" value="1" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Page2">
<f:param name="pageId" value="2" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Home">
<f:param name="pageId" value="3" />
</h:commandLink>
</h:form>
<br/>
<h2>"From Action" Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.processPage1}"
value="Page1" />
<h:commandLink action="#{navigationController.processPage2}"
value="Page2" />
</h:form>
<br/>
<h2>Forward vs Redirection Navigation</h2>
<hr />
<h:form>
<h3>Forward</h3>
<h:commandButton action="page1" value="Page1" />
<h3>Redirect</h3>
<h:commandButton action="page1?faces-redirect=true"
value="Page1" />
</h:form>
</h:body>
</html>
Navigationcontroller
package com.tutorialspoint.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
#ManagedBean(name = "navigationController", eager = true)
#RequestScoped
public class NavigationController implements Serializable {
private static final long serialVersionUID = 1L;
#ManagedProperty(value="#{param.pageId}")
private String pageId;
public String moveToPage1(){
return "page1";
}
public String moveToPage2(){
return "page2";
}
public String moveToHomePage(){
return "home";
}
public String processPage1(){
return "page";
}
public String processPage2(){
return "page";
}
public String showPage(){
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) {
this.pageId = pageId;
}
}
page1.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:body>
<h2>This is Page1</h2>
<h:form>
<h:commandButton action="home?faces-redirect=true"
value="Back To Home Page" />
</h:form>
</h:body>
</html>
page2.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:body>
<h2>This is Page2</h2>
<h:form>
<h:commandButton action="home?faces-redirect=true"
value="Back To Home Page" />
</h:form>
</h:body>
</html>
It's already been answered above, but I'll be specific. In your web.xml the <url-pattern> is specified as *.xhtm and your jsf files have a .xhtml extension. Note the missing letter in the pattern specification. Because of that your files aren't recognized and routed through the faces servlet. Just add the 'l' to the url-pattern in the web.xml and it should start working.
I have a commandButton in JSF 2.0 with a actionvalue that I want to call a method in a bean and otherwise do nothing. However when I click on it, it redirects to the previous page.
This was programmed in java 1.7 in eclipse using glassfish 3.1.1.
Here is the code for the welcome.xhtml file where the commandButton is (showDetails) but there are many other files, so I have included a link to a WAR file so it is possible to check everything out. If i change the action attribute to point to a method that does not exist I will not get an error, I will again be redirected to the frontpage.
The program is started from the index.html file. After importing remember to leftclick on the project, go to properties>project facets and tick of JavaServerFaces 2.0
The relevant code:
welcome.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 xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Insert title here</title>
</h:head>
<h:body>
<h3>#{userBean.user.name}</h3>
<hr />
<form>
<h:commandButton value="Back" action="index" />
</form>
<form>
<h:commandButton value="Show details" action="#{userBean.changeRenderDetails}" />
<h:panelGrid rendered="#{userBean.renderDetails}" columns="2">
<h:outputText value="Brugernavn:" /> #{userBean.user.name}
<h:outputText value="Password:" /> #{userBean.user.password}
</h:panelGrid>
</form>
</h:body>
</html>
userBean
package model;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
#Named("userBean")
// or #ManagedBean(name="user")
#SessionScoped
public class UserBean implements Serializable {
// Oprettes når en bean oprettes
private User user = new User("", "");
#Inject
// Det nuværende service objekt initialiseres automatisk
private Service service;
public String validateUser() {
return service.validateUser(user);
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
//Lektion 10 - opg1
private boolean renderDetails;
public void changeRenderDetails(){
setRenderDetails(!isRenderDetails());
}
public boolean isRenderDetails() {
return renderDetails;
}
public void setRenderDetails(boolean renderDetails) {
this.renderDetails = renderDetails;
}
}
and the user class
package model;
public class User {
private String name;
private String password;
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
navigation rules, none of which should have anything to do with the button which redirects to index.html:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/welcome.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/error.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
As shown in every decent JSF book/tutorial, you should be using <h:form> instead of <form>.
<h:form>
<h:commandButton value="Back" action="index" />
</h:form>
<h:form>
<h:commandButton value="Show details" action="#{userBean.changeRenderDetails}" />
<h:panelGrid rendered="#{userBean.renderDetails}" columns="2">
<h:outputText value="Brugernavn:" /> #{userBean.user.name}
<h:outputText value="Password:" /> #{userBean.user.password}
</h:panelGrid>
</h:form>
See also
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated #1