1

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>
Zoe
  • 27,060
  • 21
  • 118
  • 148
user1035910
  • 125
  • 1
  • 8
  • did you try putting it in an `` instead of `
    `
    – Lucas Nov 08 '11 at 16:23
  • You don't need to post links to WAR files. No one is going to download it. Just post the relevant code straight in the question (as you did). – BalusC Nov 08 '11 at 16:25
  • Don't put "SOLVED" in the title. This makes no sense. Stack Overflow is not a discussion forum. It's a Question & Answer site. Just mark the most helpful answer accepted. The question will then appear with a yellow instead of white answer count in the listing. Also, the Stack Overflow search function is able to distingiush accepted ("solved") questions from unaccepted ("unsolved") questions this way. – BalusC Nov 09 '11 at 16:33

1 Answers1

2

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

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • he he he... i beat you to it, though i wasn't confident enough to actually make it an _answer_ – Lucas Nov 08 '11 at 16:25
  • @Lucas: The described symptoms definitely confirms this. – BalusC Nov 08 '11 at 16:26
  • Putting in h:form solved the problem. Thanks! I am a programmer in training mainly learning java. I had 1 teacher, 2 class mates and 1 experienced programmer look at this and none of them could tell me what was wrong. Beeing acustomed to Eclipses compiling errors JSF feels quite unforgiving. Kinda left this here expecting some voodoo code to be nessesary. The mystery is solved. I am surprised that the button actually did something at all if the missing h tag should exclude it. Thats probably why I and the others spend so much time looking for the problem elsewhere. Need to do some reading. – user1035910 Nov 09 '11 at 16:22