Servlet和JSF之间的连接问题

时间:2018-05-15 02:25:46

标签: java jsf servlets java-ee javabeans

请帮助我,这是我提出的一个练习,它是建立一个电子商务页面,当然,支付,一切,但我坚持这个问题。,因为我已经相对使用了JSP,我想要开始使用JSF并提出挑战,我知道可以直接使用Managed Bean与数据库进行所有交互,但我想在练习中添加另一层,这就是servlet出现的地方,以及让我的bean连接JSF和Servlet,但它不会停止工作,它给我一个null的错误。

DP:我希望你能向我推荐一本JavaEE书籍,对不起我的英语不好,这不是我的语言。

这是servlet。

    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)`

0 个答案:

没有答案