0

I download a project from: http://wwu-pi.github.io/tutorials/lectures/eai/020_tutorial_jboss_project.html

This seemed like a great example I only needed to modify the Library-Persistence to look at tables I created in Oracle. I am using WildFly 8.0 to deploy my app. The page comes up and when I click submit I get the error in the subject line. I guess it just means it can not find the bean but for the life of me I can not figure out why. Here is some of my code:

Library-Persistence Book.java

@Entity
public class Book extends Medium implements java.io.Serializable {
    private static final long serialVersionUID = 4965400399083190672L;  
    // only basic checks
    @Pattern(regexp="[0-9X]*",message="ISBN - illegal character (only digits or X allowed)")
    @Size(min=10, message="ISBN too short (at least 10 characters required)")
    @Column(unique=true)
    protected String isbn;
    protected String author;

    public String getIsbn() {
        return this.isbn;
    }

    public void setIsbn(String nr) {
        this.isbn = nr;
    }

    public String getAuthor() {
        return this.author;
    }

    public void setAuthor(String a) {
        this.author = a;
    }

    @Override
    public String toString() {
        return getTitle() + " (ID=" + getId() +  (getAuthor()!=null?", "+getAuthor():"") + (getIsbn()!=null?", "+getIsbn():"") + ")";
    }
}

Library-EJB BookServiceBean.java

package de.unimuenster.pi.library.ejb;

import java.util.Collection;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.ConstraintViolationException;
import de.unimuenster.pi.library.jpa.Book;



/**
 * Session Bean implementation class BookService.
 * 
 * Also provides an exemplary web service with two operations.
 * @author Henning Heitkoetter
 */
@Stateless
@WebService(name = "BookWebService", serviceName = "BookService", portName = "BookServicePort", 
        targetNamespace = "http://ws.library.pi.unimuenster.de/")
public class BookServiceBean implements BookService {
    @PersistenceContext
    private EntityManager em;

    @Override
    public Book createBook(@WebParam(name = "name") String name,
            @WebParam(name = "author") String author,
            @WebParam(name = "isbn") String isbn) {
        Book newBook = new Book();
        newBook.setTitle(name);
        newBook.setAuthor(author);
        newBook.setIsbn(isbn);
        return createBook(newBook);
    }

    @Override
    @WebMethod(exclude = true)
    public Book createBook(Book book) {
        // Normalize ISBN
        if (book.getIsbn() != null)
            book.setIsbn(book.getIsbn().replaceAll("[\\- ]", ""));

        if (em.createQuery("SELECT COUNT(*) FROM Book WHERE ISBN=:isbn", Long.class).setParameter("isbn", book.getIsbn())
                .getSingleResult() > 0)
            throw new EJBException(new ConstraintViolationException(
                    "ISBN already in database", null));

        em.persist(book);
        return book;
    }

    @Override
    public Book getBook(int bookId) {
        Book book = em.find(Book.class, bookId);
        if (book == null)
            throw new IllegalArgumentException(String.format(
                    "Book with ID %s not found", bookId));
        return book;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.SUPPORTS)
    public Collection<Book> getAllBooks() {
        return em.createQuery("FROM Book", Book.class).getResultList();
    }
}

Library-Web CreateBook.java

 package de.unimuenster.pi.library.web.beans;


import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import de.unimuenster.pi.library.ejb.BookService;
import de.unimuenster.pi.library.jpa.Book;
import de.unimuenster.pi.library.web.Util;

/**
 * Backing bean for the create book page.
 * @author Henning Heitkoetter
 *
 */
@ManagedBean
public class CreateBook {
    private Book book;
    private Book lastBook;

    private boolean batch;

    private String errorMessage;

    @EJB
    private BookService bookService;

    public Book getBook() {
        if(book == null)
            book = new Book();
        return book;
    }

    public boolean isBatch() {
        return batch;
    }

    public void setBatch(boolean batch) {
        this.batch = batch;
    }

    public String persist(){
        // Action
        try{
            lastBook = bookService.createBook(getBook());
            book = null;
            errorMessage = null;
        }
        catch(EJBException e){
            errorMessage = "Book not created: " + Util.getConstraintMessage(e);
        }

        //Navigation
        if(isBatch() || errorMessage != null)
            return null;
        else
        return "listBooks";

    }

    public String getLastResult(){
        if(lastBook != null){
            return "Book created: " + lastBook.toString();
        }
        return errorMessage!=null?errorMessage:"";
    }

    public String getSuccess(){
        return errorMessage!=null?"error":"success";
    }
}

Library-Web createBook.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="/resources/master.xhtml">
    <ui:define name="title">New book</ui:define>
    <ui:define name="content">
    <div class="inputForm"><h:form>
        <h:panelGrid columns="3" columnClasses="form-label,form-input,form-message error" footerClass="form-footer">
            <h:outputLabel value="Title" for="title" />
            <h:inputText id="title" value="#{createBook.book.title}"/>
            <h:message for="title"/>
            <h:outputLabel value="Author" for="author" />
            <h:inputText id="author" value="#{createBook.book.author}" />
            <h:message for="author"/>
            <h:outputLabel value="ISBN" for="isbn" />
            <h:inputText id="isbn" value="#{createBook.book.isbn}">
                <f:ajax render="isbn-message" />
            </h:inputText>
            <h:message id="isbn-message" for="isbn"/>
            <h:outputLabel value="Batch input?" for="batch" title="Create another book after this one?"/>
            <h:selectBooleanCheckbox id="batch" value="#{createBook.batch}" />
            <f:facet name="footer"><h:commandButton value="Submit" action="#{createBook.persist}" /></f:facet>
        </h:panelGrid>
    </h:form></div>
    <div class="result"><h:outputText rendered="#{not empty createBook.lastResult}" styleClass="#{createBook.success}" value="#{createBook.lastResult}" /></div>
    </ui:define>
</ui:composition>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Library-Web</display-name>

    <welcome-file-list>
        <welcome-file>listBooks.xhtml</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
</web-app>

faces_config.xml

<?xml version="1.0"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 <converter>
  <converter-for-class>java.lang.String</converter-for-class>
  <converter-class>de.unimuenster.pi.library.web.EmptyStringToNullConverter</converter-class>
 </converter> 

</faces-config>

beans.xml

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

           bean-discovery-mode="all"
    </beans>

Any help would be so great! Thank you

GMan1973
  • 169
  • 2
  • 3
  • 18
  • Apart from `CreateBook` not having a scope, nothing immediately seems out of place – kolossus Dec 04 '14 at 02:15
  • `CreateBook` defaults to `@NoneScoped`. Try giving it an appropriate scope. Are you using JAX-WS? What is the point of injecting `BookService` using the `@EJB` annotation then? You should be using service reference instead. Don't you? – Tiny Dec 05 '14 at 14:48

0 Answers0