JSF commandButtons操作值似乎被忽略-重定向到frontpage

JSF commandButtons操作值似乎被忽略-重定向到frontpage,jsf,navigation,command,action,redirect,Jsf,Navigation,Command,Action,Redirect,我在JSF2.0中有一个commandButton,它带有一个actionvalue,我想在bean中调用一个方法,否则什么都不做。但是,当我点击它时,它会重定向到上一页 这是在eclipse的Java1.7中使用glassfish 3.1.1编程的 下面是commandButton所在的welcome.xhtml文件的代码(showDetails),但是还有许多其他文件,因此我添加了一个指向WAR文件的链接,以便可以查看所有内容。如果我将action属性更改为指向不存在的方法,则不会出现错误,

我在JSF2.0中有一个commandButton,它带有一个actionvalue,我想在bean中调用一个方法,否则什么都不做。但是,当我点击它时,它会重定向到上一页

这是在eclipse的Java1.7中使用glassfish 3.1.1编程的

下面是commandButton所在的welcome.xhtml文件的代码(showDetails),但是还有许多其他文件,因此我添加了一个指向WAR文件的链接,以便可以查看所有内容。如果我将action属性更改为指向不存在的方法,则不会出现错误,我将再次重定向到frontpage

该程序从index.html文件启动。导入后,请记住左键单击该项目,转到properties>project facets并勾选JavaServerFaces 2.0

有关守则:

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>
和用户类

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;
}

}
导航规则,这些规则都与重定向到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>

/index.xhtml
成功
/welcome.xhtml
失败
/error.xhtml

正如每本像样的JSF书籍/教程所示,您应该使用
而不是


#{userBean.user.name}
#{userBean.user.password}
另请参见

  • #1

您是否尝试将其放在
而不是
中,您不需要发布指向WAR文件的链接。没有人会下载它。只需在问题中直接发布相关代码(就像你所做的那样)。不要在标题中添加“已解决”。这毫无意义。堆栈溢出不是一个讨论论坛。这是一个问答网站。只要标出最有用的答案就行了。然后,问题将以黄色而不是白色答案计数出现在列表中。此外,堆栈溢出搜索功能能够通过这种方式区分接受(“已解决”)问题和未接受(“未解决”)问题。。。我打败了你,虽然我没有足够的信心让它成为现实answer@Lucas:所描述的症状肯定证实了这一点。输入h:表格解决了问题。谢谢我是一名接受培训的程序员,主要学习java。我有一个老师,两个同学和一个经验丰富的程序员。看看这个,他们谁也不能告诉我出了什么问题。习惯于日蚀编译错误的比因JSF感觉非常不可原谅。把这个放在这里,希望有巫毒密码。谜团解开了。我感到惊讶的是,如果丢失的h标记应该排除它,那么按钮实际上做了一些事情。这可能就是我和其他人花这么多时间在别处寻找问题的原因。我需要读点书,不客气。既然你是新来的,请别忘了在答案对解决问题最有帮助的时候标上“接受”。还可以看到你所看到的症状;该按钮呈现一个简单的
。纯HTML
默认提交到当前页面。但是如果没有
JSF就找不到实际按下的按钮,因此其操作将被忽略。
<?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>
<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>