JSF Action Method未被调用

Ped*_*oto 4 methods jsf action

我有一个带有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>
Run Code Online (Sandbox Code Playgroud)

这是控制器:

@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;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

这个回答:

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

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

我有啊:表格

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

只有一个.

  1. 不应该发生验证/转换错误(用于h:messages获取所有错误).

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

  1. 如果UICommand部件被放置在内部UIData部件中,确保完全一样的DataModel(在后面的对象UIDatavalue属性)被保留.

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

  1. rendereddisabled组件的属性,并且父组件的所有不应该求false期间应用请求值阶段.

没有rendereddisbled属性.

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

没有定义phaseListener.

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

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

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

Bal*_*usC 5

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

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

例如

<h:commandButton ...>
   <f:ajax ... render=":listForm" />
</h:commandButton>
Run Code Online (Sandbox Code Playgroud)

要么

<p:commandButton ... update=":listForm" />
Run Code Online (Sandbox Code Playgroud)

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

也可以看看: