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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top