繁体   English   中英

JSF Action Method未被调用

[英]JSF Action Method not being called

我有一个带有Primefaces数据表和命令按钮的JSF视图,作为休闲:

<p:messages id="statusMessages" showDetail="true" />
    <h:form id="listForm">
        <p:panel header="Wellsite List">
            <br />
            <h:outputLabel value="Welcome, #{wellsiteController.loggedUser.login}" />
            <br />
            <br />

            <p:dataTable id="dataTable" var="wellsite" value="#{wellsiteController.wellsiteDataTableModel}"
                         paginator="true" rows="10" selection="#{wellsiteController.wellsite}">

                <p:column selectionMode="single" style="width:18px" id="radioSelect" />

                <p:column sortBy="#{wellsite.reference}" headerText="Wellsite ID">
                    <h:outputText value="#{wellsite.reference}" />
                </p:column>

                <p:column headerText="Allowed Groups">
                    <h:outputText value="#{wellsite.allowedGroups.toString()}" />
                </p:column>

                <f:facet name="footer">
                    <h:panelGrid columns="3">
                        <p:commandButton id="addWellsite" value="Add New Wellsite" icon="ui-icon-flag" ajax="false" action="#{wellsiteController.showAddWellsite}"/>
                        <p:commandButton id="editWellsite" value="Edit Selected Wellsite" icon="ui-icon-wrench" ajax="false" action="#{wellsiteController.showEditWellsite}"/>

                        <p:commandButton id="deleteWellsiteButton" value="Remove Selected Wellsite" icon="ui-icon-trash" onclick="confirmation.show()" type="button"/>

                    </h:panelGrid>
                </f:facet>
            </p:dataTable>
            <p:spacer height="20" />
        </p:panel>
        <p:confirmDialog id="confirmDialog" message="Are you sure you want to remove the selected Wellsite along with all it's data?" header="Confirmation" severity="alert" widgetVar="confirmation">  
            <p:commandButton id="confirm" value="Yes" ajax="false" oncomplete="confirmation.hide()" action="#{wellsiteController.deleteWellsite}" />
            <p:commandButton id="decline" value="Cancel" onclick="confirmation.hide()" type="button" />   

        </p:confirmDialog>
    </h:form>

这是控制器:

@ManagedBean(name = "wellsiteController")
@RequestScoped
public class WellsiteController implements Serializable {
private static final long serialVersionUID = 1L;

@ManagedProperty("#{wellsiteDao}")
private WellsiteDao wellsiteDao;

@ManagedProperty("#{userDao}")
private UserDao userDao;

@ManagedProperty("#{groupDao}")
private GroupDao groupDao;

@ManagedProperty("#{userController.loggedUser}")
private UserEnt loggedUser;

private WellsiteEnt wellsite;
private List<WellsiteEnt> wellsiteList;
DualListModel<GroupEnt> pickGroupsModel;

public WellsiteController(){
}

@PostConstruct
public void build(){
    wellsite = new WellsiteEnt();
    wellsite.setAllowedGroups(new ArrayList<GroupEnt>());
}

/*some getters & setters*/

public WellsiteDataTableModel getWellsiteDataTableModel(){
    return new WellsiteDataTableModel(getWellsiteList());
}

public void setPickGroupsModel(DualListModel<GroupEnt> model){
    pickGroupsModel = model;
}

public DualListModel<GroupEnt> getPickGroupsModel() {
    if(pickGroupsModel == null){
        List<GroupEnt> allGroups = groupDao.getAll();
        List<GroupEnt> currentGroups = wellsite.getAllowedGroups();
        for(GroupEnt g : currentGroups){
            allGroups.remove(g);
        }
        pickGroupsModel = new DualListModel<GroupEnt>(allGroups, currentGroups);
    }
    return pickGroupsModel;
}

public String listWellsites(){
    getWellsiteList();
    return "listWellsites";
}

public String showAddWellsite(){
    FacesContext context = FacesContext.getCurrentInstance();
    setWellsite(new WellsiteEnt());
    wellsite.setAllowedGroups(new ArrayList<GroupEnt>());
    pickGroupsModel = null;
    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                                            "Fields annotated with a ' * ' are mandatory",""));
    return "addWellsite";
}

public String addWellsite(){
    FacesContext context = FacesContext.getCurrentInstance();

    wellsite.setDate(new Date());
    wellsite.setLastUpdate(wellsite.getDate());
    try {
        wellsiteDao.addWell(wellsite);

        for(GroupEnt g : pickGroupsModel.getTarget()){
            GroupEnt group = groupDao.getOne(g.getGroupId());
            group.getGroupWellsites().add(wellsite);
            groupDao.update(group);
        }
        return listWellsites();

    } catch (Exception ex) {
        Logger.getLogger(WellsiteController.class.getName()).log(Level.SEVERE, null, ex);
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                            ex.getMessage(),""));
        return null;
    }
}
}

此视图可正确呈现。 数据表和按钮看起来很好。 问题是,当我第一次单击“addWellsite”commandButton时,没有任何反应。 页面似乎刷新了。 如果我再次点击它,异常发生:

java.lang.NumberFormatException: For input string: "null"

使用调试器,我发现“addWellsite”的动作不是第一次调用,因此,不会生成结果(因此,页面刷新)。

异常可能是由于当前视图或目标视图中缺少初始化(因为两个视图都是从页面刷新中未调用的操作方法中显示)

问题是: 为什么第一次不调用动作方法?

这个回答

每当UICommand组件无法调用关联的操作时,请验证以下内容:

  1. UICommand组件必须放在UIForm组件内(例如h:form )。

我有啊:表格

  1. 您不能将多个UIForm组件嵌套 UIForm (注意包含文件!)。

只有一个。

  1. 不应该发生验证/转换错误(使用h:messages来获取它们)。

我有啊:没有显示任何错误的消息。

  1. 如果将UICommand组件放在UIData组件中,请确保UIData完全相同的DataModelUIDatavalue属性后面的对象)。

commandButton在dataTable中,但目标视图不需要dataModel。 正如我的控制器代码所示,该对象是在视图尝试检索它时构建的。 下一个请求不使用此dataTable,我不再处理它了。

  1. 在应用请求值阶段,组件和所有父组件的rendereddisabled属性不应评估为false

没有rendereddisbled属性。

  1. 确保请求 - 响应链中没有PhaseListener或任何EventListener更改了JSF生命周期以跳过调用操作阶段。

没有定义phaseListener。

  1. 确保同一请求 - 响应链中没有FilterServlet以某种方式阻止FacesServlet的请求。

没有定义其他Servlet。 我甚至都不知道过滤器是什么。

为什么第一次不调用动作方法?

当此<h:form>的父级由之前由另一个 <h:form>启动的ajax请求呈现时,可能会发生这种情况。 渲染/更新的<h:form>将失去其视图状态。 这是由JSF问题790中描述的JavaScript API中的错误引起的,该问题已经针对即将推出的JSF 2.2进行了修复。

同时,使用JSF 2.0 / 2.1,您需要在另一种形式的动作的render (或者对于PrimeFaces, update )属性中明确指定<h:form>的客户端ID。

例如

<h:commandButton ...>
   <f:ajax ... render=":listForm" />
</h:commandButton>

要么

<p:commandButton ... update=":listForm" />

或者只是让它成为普通(非ajax)请求。

也可以看看:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM