java - JSF how to store, with Controller, in session without persist in DB -
i'm trying make page user can insert 1 or more providers, check providers inserted, confirm. when user confirms, providers inserted stored in database. how can that? seems if use managedbean controller, list store providers, strange error
http status 500 - root cause java.lang.nullpointerexception it.myproject.model.provider.equals(provider.java:71) org.apache.myfaces.shared.util.selectitemsiterator.next(selectitemsiterator.java:275) org.apache.myfaces.shared.util.selectitemsiterator.next(selectitemsiterator.java:49) org.apache.myfaces.shared.renderkit.rendererutils.internalgetselectitemlist(rendererutils.java:800) org.apache.myfaces.shared.renderkit.rendererutils.getselectitemlist(rendererutils.java:764) org.apache.myfaces.shared.renderkit.html.htmlselectablerendererbase.internalrenderselect(htmlselectablerendererbase.java:74) org.apache.myfaces.shared.renderkit.html.htmlmenurendererbase.rendermenu(htmlmenurendererbase.java:91) org.apache.myfaces.shared.renderkit.html.htmlmenurendererbase.encodeend(htmlmenurendererbase.java:76) javax.faces.component.uicomponentbase.encodeend(uicomponentbase.java:665) javax.faces.component.uicomponentbase.encodeall(uicomponentbase.java:545) javax.faces.component.uicomponentbase.encodeall(uicomponentbase.java:541) javax.faces.component.uicomponentbase.encodeall(uicomponentbase.java:541) org.apache.myfaces.shared.view.jspviewdeclarationlanguagebase.actuallyrenderview(jspviewdeclarationlanguagebase.java:364) org.apache.myfaces.shared.view.jspviewdeclarationlanguagebase.renderview(jspviewdeclarationlanguagebase.java:201) org.apache.myfaces.application.viewhandlerimpl.renderview(viewhandlerimpl.java:285) javax.faces.application.viewhandlerwrapper.renderview(viewhandlerwrapper.java:59) javax.faces.application.viewhandlerwrapper.renderview(viewhandlerwrapper.java:59) org.apache.myfaces.lifecycle.renderresponseexecutor.execute(renderresponseexecutor.java:116) org.apache.myfaces.lifecycle.lifecycleimpl.render(lifecycleimpl.java:241) javax.faces.webapp.facesservlet.service(facesservlet.java:199)
here's code of providercontroller
@managedbean public class providercontroller{ private long id; private string vat; private string address; private list<product> products; private provider provider; private list<provider> providers; public providercontroller() { this.products= new linkedlist<>(); this.providers= new linkedlist<>(); } //adds provider in c:foreach of view newprovider.jsp public string addprovider() { this.provider= new provider(vat, address, products); this.providers.add(this.provider); return "newprovider"; }
-i omitted facade because have nothing store in case, want add provider in list , then, method, add providers of list in db
-provider model uses annotation @entity, doubt can't use provider if don't use entitymanager
if i'm right, can store in session providers? have create class provider? have create providercontroller?
thank answers.
keeping apart hypothetical persistence layer, can store information in session. however, have differ between information belongs session , 1 current view.
for case, think want have stored providers session variable, while information send in form when adding new 1 should in view scope. way divide logic of application in proper way. here you've got more information view scope, introduced in jsf 2.
as you're in jsf 2, choose between jsp , facelets view technology. if it's new project, encourage go facelets, standard jsf 2:
providercontroller.java
/* * manages providers in session */ @managedbean @sessionscoped public class providercontroller{ private list<provider> providers; private list<product> products; public providercontroller() { this.products= new linkedlist<>(); this.providers= new linkedlist<>(); } public list<provider> getproviders(){ return providers; } }
providers.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:c="http://java.sun.com/jsp/jstl/core"> <h:head /> <h:body> <!-- display providers session --> <ul> <ui:repeat value="#{providercontroller.providers}" var="provider"> <li>#{provider.vat}-#{provider.address}</li> </ui:repeat> </ul> </h:body> </html>
provideradd.java
/* * manages view add new provider */ @managedbean @viewscoped public class provideradd{ private long id; private string vat; private string address; //injects session scoped bean @managedproperty(value="#{providercontroller}") private providercontroller providercontroller; //getter , setters //adds new provider session , returns provider list public string addprovider() { this.provider= new provider(this.vat, this.address, this.products); providercontroller.getproviders.add(this.provider); return "providers"; } }
addprovider.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:c="http://java.sun.com/jsp/jstl/core"> <h:head /> <h:body> <h2>vat:</h2><h:inputtext value="#{provideradd.vat}" /> <h2>address:</h2><h:inputtext value="#{provideradd.address}" /> <h:commandbutton action="#{provideradd.addprovider}" value="add provider" /> </h:body> </html>
Comments
Post a Comment