【问题标题】:Not reactive JSF commandButton. Why?非响应式 JSF 命令按钮。为什么?
【发布时间】:2013-11-07 00:10:23
【问题描述】:

我在客户端相当粗鲁,所以对于在 JSF 中比我更有基础的人来说,这可能是一个非常容易的问题(非常非常容易!:D)但我有些天失去了理智.. 幸运的是在我空闲的时间里。 但我在那个(死)点需要帮助。请帮忙,否则我会整夜粘在键盘上!

我制作了一个带有两个表单的 .xhtml 页面,第二个指向一个会话 Bean,它初始化要创建的对象并重定向到带有表单的页面,以便为其变量赋值。

这是第一个 .xhtml 页面:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Modify A Cafeteria Element</title>        
    </h:head>
    <h:body>

        <f:view>
            <h:form>
                <h1><h:outputText value="Modify A Cafeteria Element"/></h1>
                <h:panelGrid columns="2">
                    <h:outputLabel value="Description:" for="description" />
                    <h:inputText id="description" value="#{cafeteriaElementBean.cafeteriaElement.description}" title="Description" />                   

                </h:panelGrid>

                <p:commandButton action="#{cafeteriaElementBean.saveOrEdit()}" value="Save" />
                <p:commandButton action="#{cafeteriaElementBean.edit()}" value="Edit" />
                <p:button outcome="cafeteriaElementList" value="Back" />
            </h:form>
        </f:view>
        <br/>
        #{cafeteriaElementBean.cafeteriaElement.description} periodicity list:
        <br/>

        <f:view>
            <h:form>
                <h1><h:outputText value="List"/></h1>
                <h:dataTable value="#{elementPeriodBean.filteredPerElement(cafeteriaElementBean.cafeteriaElement)}" var="item">
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="MaxAbsoluteForPeriod"/>
                        </f:facet>
                        <h:outputText value="#{item.maxAbsoluteForPeriod}"/>
                    </h:column>                   
                    <!-- code omissed for brevity-->
                </h:dataTable>

                <p:commandButton action="#{elementPeriodBean.create(cafeteriaElementBean.cafeteriaElement)}"  value="Add a new Periodicity"/> 

            </h:form>
        </f:view>



    </h:body>
</html> 

这是它的打印屏幕

点击“新的周期性”按钮驱动无效:调试器没有感知它,服务器终端没有写任何东西。浏览器都不会重定向到第二页。 :(

Session Bean如下:(有一些删减)

package com.cafeteria.business;

import com.cafeteria.facades.ElementPeriodFacade;
import com.cafeteria.model.CafeteriaElement;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import com.cafeteria.model.ElementPeriod;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;

@Named
@SessionScoped
public class ElementPeriodBean implements Serializable, CRUD <ElementPeriod>{

    private ElementPeriod elementPeriod;

    @Inject
    ElementPeriodFacade elementPeriodFacade;

    public List <ElementPeriod> getAll(){
        return elementPeriodFacade.findAll();
    }

    public List <ElementPeriod> filteredPerElement(CafeteriaElement cafeteriaElement){
        return elementPeriodFacade.findForElement(cafeteriaElement);
    }

    public String create(){
       this.elementPeriod = new ElementPeriod();
        return "newElementPeriod";
    }

    public String create(CafeteriaElement cafeteriaElement){
        this.elementPeriod = new ElementPeriod();
        this.elementPeriod.setCafeteriaElement(cafeteriaElement);
        return "newElementPeriod";
    }

    public String modify(ElementPeriod elementPeriod){
        this.elementPeriod = elementPeriod;
        return "newElementPeriod";
    }

    public String saveOrEdit(){
        elementPeriodFacade.create(elementPeriod);
        return "editCafeteriaElement";
    }

      public String edit(){
        elementPeriodFacade.edit(elementPeriod);
        return "editCafeteriaElement";
    }

    public String remove(ElementPeriod elementPeriod){
        elementPeriodFacade.remove(elementPeriod);
        return "editCafeteriaElement";
    }

    public ElementPeriod find(Long id){
        return elementPeriodFacade.find(id);
    }

    public ElementPeriod getElementPeriod() {
        if(this.elementPeriod== null) this.elementPeriod = new ElementPeriod();
        return this.elementPeriod;
    }  

    public void setElementPeriod(ElementPeriod elementPeriod) {
        this.elementPeriod = elementPeriod;
    }
}

这是插入数据的表单页面:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Create periodicity for Cafeteria Element</title>        
    </h:head>
    <h:body>

        <f:view>
            <h:form>
                <h1><h:outputText value="Create/Edit Periodicity for #{cafeteriaElementBean.cafeteriaElement.description}"/></h1>

                <h:panelGrid columns="2">                 
                    <h:outputLabel value="Periodicity:" for="periodicity" />
                    <h:selectOneMenu id="periodicity" value="#{elementPeriodBean.elementPeriod.periodicity}" title="Periodicity" >
                        <!-- TODO: update below reference to list of available items-->
                        <f:selectItems value="#{periodicityBeam.all}"/>
                    </h:selectOneMenu>

                    <h:outputLabel value="MaxAbsoluteForPeriod:" for="maxAbsoluteForPeriod" />
                    <h:inputText id="maxAbsoluteForPeriod" value="#{elementPeriodBean.elementPeriod.maxAbsoluteForPeriod}" title="MaxAbsoluteForPeriod" />

                    <h:outputLabel value="MaxPercentageForPeriod:" for="maxPercentageForPeriod" />
                    <h:inputText id="maxPercentageForPeriod" value="#{elementPeriodBean.elementPeriod.maxPercentageForPeriod}" title="MaxPercentageForPeriod" />

                    <h:outputLabel value="FixValue:" for="fixValue" />
                    <h:inputText id="fixValue" value="#{elementPeriodBean.elementPeriod.fixValue}" title="FixValue" />   
                </h:panelGrid>

                <p:commandButton value="Save" action="#{elementPeriodBean.saveOrEdit()}"/>

                <!--p:button outcome="editCafeteriaElement" value="Back" /-->
            </h:form>
        </f:view>
    </h:body>
</html>

注意事项: - newElementPeriod 正确地从 faces-config.xml 的第一页向上指向 - newElemelPeriod.xhtml 是否可以直接从浏览器调用 - 其他按钮有效:并非无处不在,因为有些按钮的行为与此类似

请帮忙!我快疯了!

提前谢谢你, 安德烈亚

【问题讨论】:

  • 你收到什么问题,方法create()是否被调用。
  • 这里有很多误解。我建议在这里停下来,尝试掌握 JSF 和 CDI 的基础知识。此外,如果您正在使用 JSF 2.0 或 JSF 2.1 和 CDI,我建议添加 OmniFaces 以使 CDI 具有 @ViewScoped 注释的功能,但如果您正在使用 JSF 2.2,那么您可以使用 @ViewScoped你的 CDI bean 也是如此。
  • 顺便说一句,要检测 JSF 代码中的问题,请在每个表单中添加一个&lt;h:messages&gt;,以便在那里显示转换等错误消息,您(和未来的读者)可以检测到原因为什么这不起作用。
  • 我通过复制/粘贴/改编 OP 的代码并使用 &lt;f:view&gt;s(如 OP 的当前代码)进行了测试,只是为了测试导航问题和工作原理。在我的适应过程中,我只是生成了必要的接口和类,如ElementPeriodFacadeCafeteriaElementElementPeriod 和其他具有必要字段的类。看起来问题出在 为简洁而省略的代码 cmets 中的其他地方。您可以在这里寻找更多原因:stackoverflow.com/q/2118656/1065197
  • 您是否也查看了浏览器的 HTTP 流量监视器和 JS 控制台以获取线索?

标签: jsf ejb-3.0 commandbutton


【解决方案1】:

你可以在链接上查看可能是commandButton的进程属性 CommandButton not firing

【讨论】:

    【解决方案2】:

    为什么你试图给 create 方法一个来自会话范围 bean 的参数?尝试删除参数并注入其他会话范围的 bean...

    【讨论】:

    • 这更适合作为评论而不是答案。而且,这也解决不了问题。由于您还不能发布 cmets,请等到至少有 50 个代表,然后再发布 cmets 询问更多信息或给出一些您认为不应作为答案的建议。
    • 好吧,这些东西解决了我的网络应用程序中按钮和链接的许多问题,因为参数效果不佳!您是否尝试删除它?为什么你确定它不能解决问题......?我发现对试图帮助主题作者的人的所有答案投反对票并不是很有成效的。
    • 因为我进行了测试并使用当前 OP 的代码,所以问题出在其他地方。我知道它可以有更好的设计(从错误使用 @SessionScoped bean 开始),但您的选择根本不能解决问题。
    • 因此,如果您进行了测试,请这样评论。我的回答是尝试帮助她,而不是发表评论以获取更多信息。另一件可能是错误的事情:如果这是一个 EJB SessionBean,那么注入的 elementPeriodFacade 应该被注入 @EJB。
    猜你喜欢
    • 1970-01-01
    • 2011-07-09
    • 2017-10-04
    • 2015-02-10
    • 2012-09-29
    • 2014-08-29
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多