Проблемы со связью между сервлетом и JSF - PullRequest
0 голосов
/ 15 мая 2018

Пожалуйста, помогите мне в этом, это предложенное мной упражнение, которое состоит в том, чтобы сделать страницу электронной коммерции, конечно, платежи, все, но я застрял в этой проблеме., Поскольку я уже использую JSP относительно, яЯ хотел использовать JSF и предлагать задачи. Я знаю, что это может быть сделано непосредственно с управляемым компонентом при взаимодействии с базой данных, но я хотел бы добавить еще один слой к упражнению, где появляется сервлет, изаставить мой bean-компонент соединяться между JSF и сервлетом, но он не перестает работать, он выдает ошибку NULL.

DP: Я бы хотел, чтобы вы порекомендовали мне книгу на JavaEE,пожалуйста, извините за мой плохой английский, это не мой язык.

Это сервлет.

    enter code here
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

         System.out.println("---------------------Begin-----------------------------------------------");
         try {
            Section_Model sm = (Section_Model) request.getSession().getAttribute("section_Model");
            sm.getSection().setNameAlias(request.getParameter("nameAlias"));
            sm.getSection().setCodeSS((Sections) sm.findSpecific(Integer.parseInt(request.getParameter("codeSS"))));
            sm.getSection().setVisible(Boolean.getBoolean(request.getParameter("visible")));
            sm.create();
            request.getRequestDispatcher("WEB-INF/testJSF.xhtml").forward(request, response);
        } catch (Exception ex) {
            Logger.getLogger(inputSections.class.getName()).log(Level.SEVERE, null, ex);
        }


    }


this is Managed Bean. 

@Named(value = "section_Model")
@SessionScoped


public class Section_Model implements Serializable {

    @Inject
    private Section_Model section_Model;

    @EJB
    private SectionsFacadeLocal sectionsFacade;

    @PersistenceContext(unitName = "ShopOnlinePU")
    private EntityManager em;
    @Resource
    private javax.transaction.UserTransaction utx;
    Sections section;
    SectionsJpaController controller;

    /**
     * Creates a new instance of Section_Model
     */
    public Section_Model() {
        section= new Sections();
    }

    public Sections getSection() {
        return section;
    }

    public void setSection(Sections section) {

        this.section = section;
    }

    public void init(){
        controller=new SectionsJpaController(utx, em.getEntityManagerFactory());
    }

    public void create () throws Exception{
        init();
        controller.create(section);
    }







    public List<Sections> select_all(){
        init();
        return controller.findSectionsEntities();
    }


    public void create2(){
        sectionsFacade.create(section);
    }

    public List<Entity.Sections> subSections (Sections s){

        return sectionsFacade.subSection(s);
    }

    public List<Entity.Sections> findSpecific(int id){
        return sectionsFacade.findSpecificS(1);
    }

    public Collection<Entity.Sections> topSection (){
        return sectionsFacade.TopSection();
    }

    public int countSection(){
        return sectionsFacade.count();
    }

This is JSF

<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
    <h:head>
        <title>TEST Title</title>
    </h:head>
    <h:body>

        <form action="inputSections" method="post">

                    <input  type="text" name="nameAlias"  title="NameAlias" required="true" requiredMessage="The NameAlias field is required."/>

                    <input  type="text" name="codeSS" title="CodeSS"  />

                    <input type="text" name="visible"  title="Visible" />

                    <input type="submit"  value="Ok"/>
            </form> 

        <c:forEach items="#{section_Model.findSpecific(1)}" var="item">
                     <f:view>
                    <h:form>
                <h:dataTable value="#{section_Model.subSections(item)}" var="items">
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="CodeS"/>
                        </f:facet>
                        <h:outputText value="#{items.codeS}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="NameAlias"/>
                        </f:facet>
                        <h:outputText value="#{items.nameAlias}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Visible"/>
                        </f:facet>
                        <h:outputText value="#{items.visible}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="CodeSS"/>
                        </f:facet>
                        <h:outputText value="#{items.codeSS}"/>
                    </h:column>
                </h:dataTable>
                         </h:form>
                        </f:view>

            </c:forEach>

    </h:body>

</html>

The error is

    GRAVE:   java.lang.NullPointerException
        at View.inputSections.doPost(inputSections.java:79)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:706)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1622)
        at `org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:824)`
...