commandButton动作不在JSF中调用bean方法

时间:2014-04-07 09:54:14

标签: java jsf jpa ejb

我是JSF的新手,所以我正在制作一个学习目的的项目。一切都工作正常,但当我尝试编辑记录时,我陷入了我的项目。

首先,我正在制作一个CRUD示例,为此我使用两页如下: -

  1. publishers.xhtml - 从数据库显示发布者。它工作正常。
  2. add_publisher.xhtml - 要添加和修改发布商,当我添加发布商时,它可以正常工作,但是当我编辑发布商时,它甚至不会调用该方法。
  3. 这是我的代码:

    publishers.xhtml:

    <h:dataTable
    styleClass="table table-striped table-bordered table-hover"
    value="#{publisherManager.publishers}" 
    var="publisher"
    id="dataTables-example">
        <h:column>
            <f:facet name="header">S.No.</f:facet>
            <h:outputText value="#{serialNumber.sn}"></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">Name</f:facet>
            <h:outputText value="#{publisher.name}"></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">Publish</f:facet>
            <h:outputText value="No"></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">Acion</f:facet>
            <h:form>
                <h:commandLink action="add_publisher" id="edit_link">
                    <img src="images/edit.png" width="20" />
                    <f:setPropertyActionListener target="#{publisherManager.currentId}"
                        value="#{publisher.id}" />
                    <f:setPropertyActionListener target="#{publisherManager.newName}"
                        value="#{publisher.name}" />
                    <f:setPropertyActionListener target="#{publisherManager.newDescription}"
                        value="#{publisher.description}" />
                    <f:setPropertyActionListener target="#{publisherManager.operation}"                         value="update" />
                </h:commandLink>
            </h:form>
            <h:form>
                <h:commandLink id="delete_link" action="publishers">
                    <img src="images/delete.png" width="20" />
                </h:commandLink>
            </h:form>
        </h:column>
    </h:dataTable>
    

    add_publisher.xhtml:

    <form method="post" jsf:name="publisher_form" action="">
        <div class="form-group">
            <label>Name</label>
            <h:inputText styleClass="form-control" id="name" value="#{publisherManager.newName}"></h:inputText>
        </div>
        <div class="form-group">
            <label>Description</label>
            <h:inputTextarea styleClass="form-control" rows="3" id="description" value="#{publisherManager.newDescription}"></h:inputTextarea>
        </div>
        <h:commandButton action="#{publisherManager.addPublisher}" value="Submit" styleClass="btn btn-default" />
        <input type="reset" name="sbmt" id="sbmt" value="Reset" class="btn btn-default"/> 
    </form>
    

    PublisherManager.java:

    @ManagedBean
    @RequestScoped
    public class PublisherManager implements Serializable {
        private static final long serialVersionUID = 2142383151318489373L;
        @EJB
        private PublisherBean publisherBean;
        private static final Logger logger = Logger.getLogger("bookstore.web.PublisherManager");
        private List<Publisher> publishers;
        private String newName;
        private String newDescription;
        private Integer currentId;
        private String operation = "add";
    
        public String getOperation() {
            return operation;
        }
        public void setOperation(String operation) {
            this.operation = operation;
        }
        public String getNewName() {
            logger.log(Level.INFO,"Getname called");
            return newName;
        }
        public void setNewName(String newName) {
            logger.log(Level.INFO,"Setname called");
            this.newName = newName;
        }
        public String getNewDescription() {
            logger.log(Level.INFO,"GetDescription called");
            return newDescription;
        }
        public void setNewDescription(String newDescription) {
            logger.log(Level.INFO,"SetDescription called");
            this.newDescription = newDescription;
        }
        public Integer getCurrentId() {
            logger.log(Level.INFO,"getCurrentId called");
            return currentId;
        }
        public void setCurrentId(Integer currentId) {
            logger.log(Level.INFO,"setCurrentId called");
            this.currentId = currentId;
        }
    
        public List<Publisher> getPublishers(){
            try{
                this.publishers = publisherBean.getAllPublishers();
            }catch(Exception e){
                logger.warning("Can't get publishers");
            }
            return publishers;
        }
        public void addPublisher(){
            logger.log(Level.INFO,"addPublisher called");
            try{
                publisherBean.createPublisher(currentId, newName, newDescription, operation);
                logger.log(Level.INFO,"Publisher added successfully");
                this.currentId = null;
                this.newName = null;
                this.newDescription = null;
                FacesContext.getCurrentInstance().getExternalContext().redirect("publishers.xhtml");
            }catch(Exception e){
                e.printStackTrace();
                logger.warning("Problem when adding publisher");
            }
        }
    }
    

    PublisherBean.java:

    @Stateful
    public class PublisherBean {
        @PersistenceContext
        private EntityManager em;
    
        private static final Logger logger = Logger.getLogger("bookstore.ejb.RequestBean");
    
        public void createPublisher(Integer id,String name, String description, String operation){
            logger.log(Level.INFO,"createPublisher called");
            logger.log(Level.INFO,"operation = "+operation);
            if(operation.equals("add")){
                try{
                    Publisher publisher = new Publisher(name, description);
    
                    logger.log(Level.INFO, "Created Publisher {0}-{1}", new Object[]{name, description});
                    em.persist(publisher);
                    logger.log(Level.INFO, "Persisted Publisher {0}-{1}", new Object[]{name, description});
                }catch(Exception e){
                    throw new EJBException(e.getMessage());
                }
            }
            if(operation.equals("update")){
                try{
                    Publisher publisher = em.find(Publisher.class, id);
                    publisher.setName(name);
                    publisher.setDescription(description);
                    em.merge(publisher); 
                    logger.log(Level.INFO, "Updates Publisher {0}-{1}", new Object[]{name, description});
                }catch(Exception e){
                    throw new EJBException(e.getMessage());
                }
            }
    
        }
    
        public List<Publisher> getAllPublishers(){
            List<Publisher> publishers = (List<Publisher>) em.createNamedQuery("Publisher.findAll").getResultList();
            return publishers;
        }
    
    }
    

    当我在添加或编辑时点击add_publisher.xhtml页面上的提交按钮时调用相同的方法,当我添加发布者方法调用并插入记录但是当我编辑产品时,相同的方法应该是但这次是为了编辑产品。 但是PublisherManager.java的addPublisher()方法不会调用。

    请帮我弄清楚我的错误。

    提前致谢...

0 个答案:

没有答案