我從https://stackoverflow.com/a/3180885/242042的例子中爲我的需要調整了它。未調用JSF CommandButton動作(另一個示例)
這裏是我的豆:
@ManagedBean
@ViewScoped
public class ParticipantBean implements
Serializable {
private boolean edit;
private List<Participant> list;
private Participant participant = new Participant();
@Inject
private transient ParticipantDAO participantDAO;
public void add() {
System.out.println("Calling add");
participantDAO.save(participant);
init();
}
public void delete(final Participant participant) {
participant.getAudit().cancel();
participantDAO.save(participant);
init();
}
public void edit(final Participant participant) {
this.participant = participantDAO.get(participant.getId());
edit = true;
}
public void fire() {
System.out.println("fired");
}
public List<Participant> getList() {
return list;
}
public Participant getParticipant() {
return participant;
}
@PostConstruct
public void init() {
list = participantDAO.getAll();
participant = new Participant(); // Reset placeholder.
}
public boolean isInEdit() {
return edit;
}
public void saveParticipant() {
System.out.println("Calling save");
participantDAO.save(participant);
System.out.println("Done Calling save");
init();
edit = false;
}
}
我的JSF文件
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Really simple CRUD</title>
</h:head>
<h:body>
<h3>List items</h3>
<h:form rendered="#{not empty participantBean.list}">
<h:dataTable value="#{participantBean.list}" var="item">
<h:column><f:facet name="header">ID</f:facet>#{item.id}</h:column>
<h:column><f:facet name="header">Name</f:facet>#{item.name}</h:column>
<h:column><h:commandButton value="edit" action="#{participantBean.edit(item)}" /></h:column>
<h:column><h:commandButton value="delete" action="#{participantBean.delete(item)}" /></h:column>
<h:column><h:commandButton value="fire" action="#{participantBean.fire}" /></h:column>
</h:dataTable>
</h:form>
<h:panelGroup rendered="#{empty participantBean.list}">
<p>Table is empty! Please add new items.</p>
</h:panelGroup>
<h:panelGroup rendered="#{!participantBean.inEdit}">
<h3>Add item</h3>
<h:form>
<p>Name: <h:inputText value="#{participantBean.participant.name}" /></p>
<p><h:commandButton value="add" action="#{participantBean.add}" /></p>
<p><h:commandButton value="fire" action="#{participantBean.fire}" /></p>
</h:form>
</h:panelGroup>
<h:panelGroup rendered="#{participantBean.inEdit}">
<h3>Edit item #{participantBean.participant.id}</h3>
<h:form>
<p>Name: <h:inputText value="#{participantBean.participant.name}" /></p>
<p><h:commandButton value="save" action="#{participantBean.saveParticipant}" /></p>
<p><h:commandButton value="add" action="#{participantBean.add}" /></p>
<p><h:commandButton value="fire" action="#{participantBean.fire}" /></p>
</h:form>
</h:panelGroup>
</h:body>
</html>
所以這是非常相似的,但我不明白的是,爲什麼「編輯」它不希望來調用這些動作。 (即我沒有看到SystemOut.log上的任何東西)
我正在看答案https://stackoverflow.com/a/13327382/242042,看看是否有什麼值得的,但我發現「System.out.println()」事件甚至沒有被解僱。控制權在那裏。
有一件事我沒有通知是commandButtons
重裝而inEdit
我已經消除了「總理面臨」還有,我在WebSphere 9.0.0.3測試屏幕。代碼是在https://github.com/trajano/jee/tree/no-prime-faces
我也嘗試重新排序窗體,使編輯塊的形式像這樣,但它仍然是行動不會觸發。
<h:form rendered="#{not empty participantBean.list}">
<h:dataTable value="#{participantBean.list}" var="item">
<h:column><f:facet name="header">ID</f:facet>#{item.id}</h:column>
<h:column><f:facet name="header">Name</f:facet>#{item.name}</h:column>
<h:column><h:commandButton value="edit" action="#{participantBean.edit(item)}" /></h:column>
<h:column><h:commandButton value="delete" action="#{participantBean.delete(item)}" /></h:column>
</h:dataTable>
<h:panelGroup rendered="#{participantBean.inEdit}">
<h3>Edit item #{participantBean.participant.id}</h3>
<p>Name: <h:inputText value="#{participantBean.participant.name}" /></p>
<p><h:commandButton value="save" action="#{participantBean.saveParticipant}" /></p>
<p><h:commandButton value="add" action="#{participantBean.add}" /></p>
</h:panelGroup>
</h:form>
我也嘗試已經edit()
寫這種方式來獲得,這是原來名單上的參與者這樣,它就會有正確的樂觀鎖@Version
public void edit(final Participant participant) {
this.participant = participant;
edit = true;
}
「編輯」方法中沒有'System.out.println'。作爲一個調試參考,你可以更好地使用http://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value –
它保存在編輯面板組中不會被解僱。 –
我也看過那篇文章,但是我看不到任何應用。 –