Unable to pass boolean in URL - jsf

I have 2 pages, inputForm.xhtml and outputPage.xhtml.
In inputForm.xhtml, there are 1 textfield and 1 checkbox. When user clicks submit button, the values are passed to outputPage.xhtml in URL.
The value of textfield can be passed correctly. However, no matter the checkbox is checked or not, the parameter in URL is always false.
e.g. http://localhost:8080/jsf-web/outputPage.xhtml?c=false&n=franz
I am using myFaces 2.1.13. I really have no idea how to fix that. Thanks.
inputForm.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<f:metadata>
<f:viewParam name="n" value="#{inputFormBean.name}" />
<f:viewParam name="c" value="#{inputFormBean.nameUppercase}" />
</f:metadata>
<h:head>
</h:head>
<h:body>
<h:form>
<p>Name: <h:inputText value="#{inputFormBean.name}" /></p>
<p>Name uppercase: <h:selectBooleanCheckbox value="#{inputFormBean.nameUppercase}"/></p>
<p><h:commandButton action="output_page" value="Submit" /></p>
</h:form>
</h:body>
</html>
outputPage.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<f:metadata>
<f:viewParam name="n" value="#{outputPageBean.name}" />
<f:viewParam name="c" value="#{outputPageBean.nameUppercase}" />
</f:metadata>
<h:head>
</h:head>
<h:body>
<h3>Welcome #{outputPageBean.nameUppercase ? outputPageBean.name.toUpperCase() : outputPageBean.name}!!</h3>
</h:body>
</html>
faces-config.xml
<?xml version="1.0"?>
<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>*</from-view-id>
<navigation-case>
<from-outcome>output_page</from-outcome>
<to-view-id>/outputPage.xhtml</to-view-id>
<redirect include-view-params="true"/>
</navigation-case>
</navigation-rule>
</faces-config>
InputFormBean.java
package com.franzwong.jsfweb;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean
#RequestScoped
public class InputFormBean {
private String name;
private boolean nameUppercase;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isNameUppercase() {
return nameUppercase;
}
public void setNameUppercase(boolean nameUppercase) {
this.nameUppercase = nameUppercase;
}
}
OutputPageBean.java
package com.franzwong.jsfweb;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean
#RequestScoped
public class OutputPageBean {
private String name;
private boolean nameUppercase;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isNameUppercase() {
return nameUppercase;
}
public void setNameUppercase(boolean nameUppercase) {
this.nameUppercase = nameUppercase;
}
}

Finally I tried primitive int and it is passed as 0 no matter what value I input.
Therefore, I tried Boolean, Integer and found that the value I input can be passed correctly in URL.

Related

on click action of a button null data is getting stored in database when i try to add it through java bean [duplicate]

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>

OutputLink not working in Flow Scope

Confirmation.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">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2" columnClasses="rightalign,leftalign">
<h:outputText value="Salutation: "></h:outputText>
#{registrationBean.salutation}
<h:outputText value="First Name: "></h:outputText>
#{registrationBean.firstname}
<h:outputText value="Age: "></h:outputText>
#{registrationBean.age}
<h:outputText value="Email: "></h:outputText>
#{registrationBean.email}
<!--
<h:panelGroup/>
<h:commandButton value="continue" action="register-return"></h:commandButton>
<h:commandButton value="back" action="register"></h:commandButton>-->
<h:outputLink value="register.xhtml">
<h:outputText value="back"></h:outputText>
</h:outputLink>
</h:panelGrid>
</h:form>
</h:body>
</html>
RegistrationBean.java
import java.io.Serializable;
import javax.faces.flow.FlowScoped;
import javax.inject.Named;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author rshingha
*/
#Named
#FlowScoped("register")
public class RegistrationBean implements Serializable {
private int age;
private String firstname;
private String salutation;
private String email;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getSalutation() {
return salutation;
}
public void setSalutation(String salutation) {
this.salutation = salutation;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
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:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:commandLink action="register">
<h:outputText value="Click here to register"></h:outputText>
</h:commandLink>
</h:form>
</h:body>
</html>
Functionality:
Here we are navigating from index.xhtml to "register" flow scope, containing register.xhtml, register-flow.xml, confirmation.xhtml
From register.xhtml we navigate to confirmation.xhtml.
Issues:
1) I want to make a "back" output link in confirmation.xhtml to navigate back to register.xhtml
<h:outputLink value="register.xhtml">
<h:outputText value="back"></h:outputText>
</h:outputLink>
But this is not working, its giving following error when I click on "back" on UI
"WELD-001303: No active contexts for scope type javax.faces.flow.FlowScoped"
Interesting thing is <h:commandButton> is working instead.
2) Why can't we directly run a file in directory ("register").
Its giving following error:
WELD-001303: No active contexts for scope type javax.faces.flow.FlowScoped
I am confused. Please help me?

commandButton action method not invoked in Liferay

I've the below portlet view.xhtml:
<?xml version="1.0"?>
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core"
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:body>
<h:form>
<h:commandButton value="TESTButton" action="#{navigationViewBean.submit}" />
<h:outputText value="TESTGetter: #{navigationViewBean.testField}" />
</h:form>
</h:body>
</f:view>
And this managed bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name = "navigationViewBean")
#RequestScoped
public class NavigationViewBean {
private String testField;
public boolean lol = false;
public void submit() {
System.out.print("TEST BUTTON INVOKED");
}
public String getTestField() {
System.out.print("TEST GETTER INVOKEDx");
return testField;
}
public void setTestField(String testField) {
this.testField = testField;
}
}
The only thing I try to do, is to call a method which prints something to my console. The problem is that no matter what I do, the action method is never invoked. The getter method is properly called.
What am I doing wrong?
Im not sure why, but after adding this line to my liferay-portlet.xml it fixed it.
<requires-namespaced-parameters>false</requires-namespaced-parameters>
And here the whole block:
<portlet>
<portlet-name>Test1</portlet-name>
<icon>/icon.png</icon>
<requires-namespaced-parameters>false</requires-namespaced-parameters>
<header-portlet-css>/css/main.css</header-portlet-css>
</portlet>

ManagedBean with ArrayList, how to add elements?

I'm trying to do SIMPLE webapp which show partyguest list and allow me to add new guest. I want to store guests in ArrayList. I don't know where and how to invoke party.addGuest() method.
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:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Big Party</title>
</h:head>
<h:body>
<h2>Add new guest to Big Party: </h2>
<h:form>
<h:inputText id="guestName" value="#{guest.name}"/>
<h:commandButton value="Add guest" action="guests" />
</h:form>
<h:link value="GuestList" outcome="guests" />
</h:body>
</html>
guests.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:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Super Party</title>
</h:head>
<h:body>
<h2>New guest:</h2>
<h:outputLabel value="#{guest.name}" />
<h2>Guests:</h2>
<ul>
<ui:repeat value="#{party.guests}" var="curr">
<li>#{curr}</li>
</ui:repeat>
</ul>
<h2>Guests count:</h2>
<h:outputLabel value="#{party.cnt}"/>
</h:body>
</html>
Party.java
package managedBeans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
#ManagedBean(name = "party")
#ApplicationScoped
public class Party implements Serializable {
private List<String> guests;
private int cnt;
public Party() {
}
#PostConstruct
public void init() {
guests = new ArrayList<>();
guests.add("Guest A");
guests.add("Guest B");
}
public List<String> getGuests() {
return guests;
}
public void addGuest(String guest) {
guests.add(guest);
}
public int getCnt() {
cnt = guests.size();
return cnt;
}
}
Guest.java
package managedBeans;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name = "guest")
#RequestScoped
public class Guest implements Serializable{
private String name;
public Guest() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
If you are using JSF 2, you can have something like this in your index.xhtml (but it can be easily converted to earlier JSF version):
...
<h:form>
<h:inputText id="guestName" value="#{party.newGuest}"/>
<h:commandButton value="Add guest" action="#{party.addGuest()}" />
</h:form>
...
And, in Party.java:
private String newGuest;
....
public String getNewGuest() {
return this.newGuest;
}
public void setNewGuest(String guest) {
this.newGuest = guest;
}
....
public void addGuest() {
guests.add(newGuest);
newGuest = null;
}
No need for Guest.java in this use case. Could be done in a nicer way, though.

JSF commandButtons action value seems to be ignored - redirects to frontpage

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

Resources