I am try to develop a web application where I need the Conversation scope to carry on with the same String value in multiple xhtml pages with Primefaces 3.5.
When I begin the conversation with conversation.begin(), it throws null pointer exception with conversation being as null.
I know I can't create a instance of Conversation using 'new'. But I can't figure out where am I going wrong and why is it coming to be null.
Please guide.
Snippet of the xhtml where the bean method is called:
<p:column style="text-align: left" headerText="Deal ID"
width="30">
<p:commandLink value="#{selectedDealBean.getDealID()}"
action="#{SearchBean.action(selectedDealBean.getDealID())}"
process="#this" >
</p:commandLink>
</p:column>
Snippet of bean:
#Named()
#ManagedBean
#ConversationScoped
public class SearchBean implements Serializable {
#Inject
private Conversation conversation;
private DealBean selectedDealBean;
private String selectedID;
private SearchObject searchObj = new SearchObject();
public void start() {
this.conversation.begin();
}
public void end() {
conversation.end();
}
public void submit() {
System.out.println(selectedDealBean);
}
public String action(String selectedID) {
String actionstatus = null;
setSelectedID(selectedID);
actionstatus = "/common/dealDisplay.xhtml?faces-redirect=true" ;
start();
return actionstatus;
}
public String onFinish() {
end();
return "/common/create.xhtml";
}
}
You're mixing annotations. Since you're using CDI you should remove #ManagedBean annotation which is possibly the reason why your injection isn't working.
Related
I have a datatable where huge data sets need to be displayed.So i decided to go with primefaces live scrolling.I also got to know that to improve datatable performance with huge data sets we need to implement lazy loading.With reference to the primefaces showcase example
here,what i observed that there in the statement
, a LazyDataModel needs to be implemented to query the datasource when pagination, sorting, filtering or live scrolling happens
live scrolling is mentioned but i cannot see the code to handle scrolling.It only has the code to handle sorting,filtering,row count and pagination.Can someone please clarify my confusion if both live scolling and lazy datamodel implementation are compatible with each other or should i add some code to handle live scrolling also.
It works exactly the same as if you were having a pagination.
Here's a small example:
xhtml
<p:dataTable var="user"
value="#{bean.lazyDataModel}"
scrollRows="20"
liveScroll="true"
scrollHeight="500"
lazy="true"
scrollable="true">
<p:column headerText="name">
<h:outputText value="#{user.name}" />
</p:column>
</p:dataTable>
Bean
#ManagedBean
#ViewScoped
public class Bean {
private LazyDataModel<User> lazyDataModel;
#EJB
UserEJB userEJB;
#PostConstruct
public void init() {
lazyDataModel = new LazyUserModel(userEJB);
}
public LazyDataModel<User> getLazyDataModel() {
return lazyDataModel;
}
public void setLazyDataModel(LazyDataModel<User> lazyDataModel) {
this.lazyDataModel = lazyDataModel;
}
//setters and getters for userEJB
}
LazyUserModel
public class LazyUserModel extends LazyDataModel<User> {
private Integer findAllCount;
#EJB
private UserEJB userEJB;
public LazyUserModel(UserEJB userEJB) {
this.userEJB = userEJB;
}
#Override
public List<User> load(int first, int pageSize, String sortField, SortOrder sortOrder,
Map<String, String> filters) {
List<User> data = new ArrayList<User>();
// pageSize is scrollRows="20" in the datatable
data = userEJB.findAll(first, pageSize);
// findAll is using query.setFirstResult(first).setMaxResults(pageSize).getResultList()
// rowCount
if (findAllCount == null) {
findAllCount = userEJB.findAllCount();
this.setRowCount(findAllCount);
}
return data;
}
}
Hope this helps.
Looks like there's a new attribute for it in 6.1, liveScroll="true"
https://github.com/primefaces/primefaces/issues/2105
This question already has answers here:
Validation Error: Value is not valid
(3 answers)
Closed 7 years ago.
I am currently working on a small Voting-Tool with JSF, Primefaces, CDI etc... The main purpose of that is to improve my programming skills and learning a bit about Java EE.
In the tool, one can collect PC-games and create Game-Votings, where people (Lan-Party) can (or should be able to) vote for a game.
For the Voting-Form I want to use Primefaces "SelectOneListBox". It gets the List of Games from a #ApplicationScoped Bean that handels the Voting ("votingListProducer") and looks as follows:
<h:form id="votingFormID">
<p:selectOneListbox value="#{votingListController.selectedGame}" converter="#{gameConverter}"
var="g" showCheckbox="true" style="width:500px;" >
<f:selectItems value="#{votingListProducer.voting.gameList}" var="game" itemLabel="#{game.name}" itemValue="#{game}" />
<p:column>
<p:graphicImage value="resources/images/#{g.imgSrc}" width="40"/>
</p:column>
<p:column>
#{g.name}
</p:column>
<p:column>
#{g.version}
</p:column>
</p:selectOneListbox>
<p:commandButton value="Submit" action="#{votingListController.doAddVoteAktion}" />
<p:commandButton value="Zeige selektierte List" action="#{votingListController.doPrintSelectedGame}"/>
</h:form>
When I call this view, the correct List of Games is displayed. But when I select one and click on "Submit" the converter converts it right, because it returns the correct Game, but in the Controller the attribute "selectedGame" will not be set to this Game. And also the Methods are not called.
Here is the Controller:
#SessionScoped
#Named
public class VotingListController implements Serializable{
private static final long serialVersionUID = -7222543653854660316L;
private Game selectedGame;
#Inject #AddedChoice
private Event<Game> addChoiceEventSrc;
public void doAddVoteAktion (){
if (selectedGame!=null){
addChoiceEventSrc.fire(selectedGame);
}
}
public void doPrintSelectedGame(){
if (selectedGame!=null){
System.out.println(selectedGame.getName());
}
}
public Game getSelectedGame() {
return selectedGame;
}
public void setSelectedGame(Game selectedGame) {
this.selectedGame = selectedGame;
}
}
Do you have any Idea why this does not work?
Thank You!
Edit: Converter-Code
#RequestScoped
#Named
public class GameConverter implements Converter{
#Inject
private GameListManager gameListManager;
#Override
public Object getAsObject(FacesContext fc, UIComponent ui, String value) {
for (Game g : gameListManager.getGameList()){
if (g.getGameId().toString().equalsIgnoreCase(value)){
System.out.println ("I receive: "+g.getName());
return g;
}
}
return null;
}
#Override
public String getAsString(FacesContext fc, UIComponent ui, Object value) {
if (value!=null){
Game game = (Game) value;
String res = game.getGameId().toString();
System.out.println(res);
return res;
}
return null;
}
}
Try:
<p:commandButton value="Submit" actionListener="#{votingListController.doAddVoteAktion}"
process=":votingFormID"/>
Maybe it can be a error when your converter calls getAsObject, can you post the converter code?
Change your action Listener definition to
public void doAddVoteAktion (ActionEvent event){
if (selectedGame!=null){
addChoiceEventSrc.fire(selectedGame);
}
}
And
<p:commandButton value="Submit" actionListener="#votingListController.doAddVoteAktion}" ajax="true" process="#this"/>
I'm trying to implement on JSF data table a binding to a backed bean value with a nested data table with a binding to a value of the current data row, see this code:
<h:form prependId="false">
<h:dataTable binding="#{tableBean.mainDataTable}" var="row" >
<h:column>
<h:dataTable binding="#{row.nestedDataTable}" />
</h:column>
</h:dataTable>
</h:form>
And these are the backed beans and data:
#ManagedBean
#SessionScoped
public class TableBean implements Serializable{
private HtmlDataTable mainDataTable;
private List<TableBeanData> tableBeanDataLst;
public TableBean() {
tableBeanDataLst = new ArrayList<TableBeanData>();
DataModel<TableBeanData> mainDataModel =new ListDataModel<TableBeanData>(tableBeanDataLst);
mainDataTable = new HtmlDataTable();
mainDataTable.setValue(mainDataModel);
}
public HtmlDataTable getMainDataTable() {
return mainDataTable;
}
public void setMainDataTable(HtmlDataTable mainDataTable) {
this.mainDataTable = mainDataTable;
}
public List<TableBeanData> getTableBeanDataLst() {
return tableBeanDataLst;
}
public void setTableBeanDataLst(List<TableBeanData> tableBeanDataLst) {
this.tableBeanDataLst = tableBeanDataLst;
}
}
public class TableBeanData implements Serializable{
private HtmlDataTable nestedDataTable;
public TableBeanData (){
nestedDataTable = new HtmlDataTable();
}
public HtmlDataTable getNestedDataTable() {
return nestedDataTable;
}
public void setNestedDataTable(HtmlDataTable nestedDataTable) {
this.nestedDataTable = nestedDataTable;
}
}
But I get a property not found exception:
/index.xhtml #17,69 binding="#{row.nestedDataTable}": Target Unreachable, indentified 'row' resolved to null
I don't understand well, because I'm Initializing the datatables in the constructors.
How can I resolve this type of error to allow this configuration of table, nested table and data bindings?
I'm implementing a PrimeFaces picklist in a dialog. Everytime the dialog is shown the contents of the target of the picklist should change depending on a list entry shown before. Before I open the dialog I fill the target of the picklist in ProdukteBean.onEditProdukt(..) with the appropriate values. Unfortunately these target values do not show up in the target container. Here are the relevant code pieces:
list.xhtml:
<p:commandLink id="editButton" update=":dialogForm:editDialogPanel" title="Edit"
oncomplete="produktDialog.show();"
process="#this" partialSubmit="true"
action="#{produkteBean.onEditProdukt}">
<h:outputText value="Bearbeiten" />
<f:setPropertyActionListener value="#{p}" target="#{produkteBean.produkt}" />
</p:commandLink>
dialog.xhtml:
<!-- ... -->
<p:dialog id="dialog" header="Produkt-Details" widgetVar="produktDialog" appendToBody="true" showEffect="explode" hideEffect="explode" modal="true" width="500">
<p:messages id="msgs"/>
<h:form id="dialogForm">
<!-- ... -->
<p:pickList id="produkteDatenList" var="proddat" value="#{produkteBean.datenList}"
itemLabel="#{proddat.bezeichnung}" itemValue="#{proddat}"
converter="produktDatConverter"/>
<!-- ... -->
</h:form>
</p:dialog>
ProdukteBean.java:
#Named("produkteBean")
#ViewScoped // #SessionScoped // #ViewScoped
public class ProdukteBean implements Serializable {
#Inject #Transient private ProdukteService produkteService;
#Inject #Transient private DatenService datenService;
#Inject()
private ProdukteDatenBean produkteDatenBean;
private DualListModel<Dat> datenList = new DualListModel<Dat>();
private Dat dat = null;
public ProdukteBean() {
}
#PostConstruct
private void init() {
getAll();
}
private void getAll() {
logger.debug("getAll()");
getAllProdukte();
getAllDaten();
}
private void getAllDaten() {
logger.debug("getAllDaten()");
List<Dat> source = new ArrayList<Dat>();
source.addAll(datenService.list());
List<Dat> target = new ArrayList<Dat>();
if (produkt.getDaten() != null) {
logger.debug("adding " + produkt.getDaten().size() + " daten to produkt " + produkt.getName());
target.addAll(produkt.getDaten());
}
DualListModel<Dat> datenList = new DualListModel<Dat>();
datenList.setSource(source);
datenList.setTarget(target);
setDatenList(datenList);
}
public List<Produkt> getAllProdukte() {
logger.debug("getAllProdukte()");
return produkteService.list();
}
public void onEditProdukt() {
onEditProdukt(null);
}
public void onEditProdukt(ActionEvent actionEvent) {
logger.debug("onEditProdukt: " + ReflectionToStringBuilder.toString(produkt));
if (produkt != null) {
setSelectedEinheit(produkt.getEinheit());
getAllDaten();
}
FacesMessage msg = new FacesMessage("Produkt ausgewählt", produkt.getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
/**
* #return the einheitList
*/
public List<Einheit> getEinheitList() {
if (einheitList == null) {
einheitList = produkteService.getEinheiten();
}
return einheitList;
}
}
ProduktDatConverter.java:
#FacesConverter(forClass=Dat.class,value="produktDatConverter")
#ViewScoped
public class ProduktDatConverter implements Converter {
#Inject
#Transient DatenService datenService;
#Transient
protected final Logger logger = Logger.getLogger(getClass().getName());
// gets never called (of course)
public Object getAsObject(FacesContext arg0, UIComponent arg1, String str) {
logger.debug("getAsObject(): " + str);
return null;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object object) {
if (object instanceof Dat) {
// logger.debug(" returning id " + String.valueOf(((Dat) object).getId()));
return Long.toString(((Dat) object).getId());
}
return null;
}
}
Any ideas? Many thanks in advance!
You are using #ViewScoped along CDI Managed Bean. Change it to JSF Managed Bean. Better, use CODI instead
Also see:
View Scope in CDI Weld
CDI missing #ViewScoped and #FlashScoped
I suppose it's converter + injection problem. Please, click here and refer to the BalusC answer. You can also try to replace:
#FacesConverter(forClass=Dat.class,value="produktDatConverter")
#ViewScoped
with
#ManagedBean
#RequestScoped
and call the converter like this:
converter="#{produktDatConverter}"
This is my first post after many research on this problem.
This example is running under Jboss 7.1 with seam 3.1 (solder + persistence + faces) with seam managed persistence context
I'm facing a problem, the classical failed to lazily initialize a collection, no session or session was closed: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed when using a converter on Entity beans. The aim is to stay 100% Object oriented, by reusing the JPA model.
in beans.xml, org.jboss.seam.transaction.TransactionInterceptor is activated
Entity beans :
#Entity
public class Member implements Serializable {
#Id
#GeneratedValue
private Long id;
private String name;
private String email;
#Column(name = "phone_number")
private String phoneNumber;
#ManyToMany
private List<Statut> listeStatut = new ArrayList<Statut>();
// getters, setters, hashcode, equals
}
#Entity
public class Statut implements Serializable {
#Id
#GeneratedValue
private Long id;
private String name;
#ManyToMany(mappedBy="listeStatut")
private List<Member> members = new ArrayList<Member>();
// getters, setters, hashcode, equals
}
The JSF page :
<h:form>
<h:selectManyCheckbox id="stat" value="#{memberModif.member.listeStatut}">
<f:converter converterId="statutConverter"/>
<f:selectItems value="#{memberModif.statutsPossibles}" var="statut" itemValue="#{statut}" itemLabel="#{statut.name}" />
</h:selectManyCheckbox>
<h:commandLink id="register" action="#{memberModif.modifier()}" value="Modifier">
<f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
</h:commandLink>
</h:form>
The backing bean (I tried with ConversationScoped after SessionScoped --> same problem)
#ConversationScoped
#Named
public class MemberModif implements Serializable {
private static final long serialVersionUID = -291355942822086126L;
#Inject
private Logger log;
#Inject
private EntityManager em;
#Inject Conversation conversation;
private Member member;
#SuppressWarnings("unused")
#PostConstruct
private void init() {
if (conversation.isTransient()) {
conversation.begin();
}
}
public String modifier() {
em.merge(member);
}
public Member getMember() {
if (member == null) {
member = em.createQuery("from Member m where m.id=:id",Member.class).setParameter("id", new Long(0)).getSingleResult();
}
return member;
}
public List<Statut> getStatutsPossibles() {
return em.createQuery("from Statut", Statut.class).getResultList();
}
}
And the converter (strongly inspired by seam ObjectConverter) :
#FacesConverter("statutConverter")
public class StatutConverter implements Converter, Serializable {
final private Map<String, Statut> converterMap = new HashMap<String, Statut>();
final private Map<Statut, String> reverseConverterMap = new HashMap<Statut, String>();
#Inject
private transient Conversation conversation;
private final transient Logger log = Logger.getLogger(StatutConverter.class);
private int incrementor = 1;
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (this.conversation.isTransient()) {
log.warn("Conversion attempted without a long running conversation");
}
return this.converterMap.get(value);
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (this.conversation.isTransient()) {
log.warn("Conversion attempted without a long running conversation");
}
if (this.reverseConverterMap.containsKey(value)) {
return this.reverseConverterMap.get(value);
} else {
final String incrementorStringValue = String.valueOf(this.incrementor++);
this.converterMap.put(incrementorStringValue, (Statut)value);
this.reverseConverterMap.put( (Statut)value, incrementorStringValue);
return incrementorStringValue;
}
}
}
Please note that I put this converter here to avoid you searching over the net for the seam implementation, but it is the same as using <s:objectConverter/> tag instead of <f:converter converterId="statutConverter"/>
Any help would be greetly appreciated.
You should access the objects in the same transaction. If you are sure you are doing that already, you could try getting the entitymanager by looking it up in the context instead of injecting it. Ive had a simular problem which was resolved that way. You can also initialize the collection in the transaction when you first got your reference to it.
Hibernate.initialize(yourCollection);
Take a look at this: selectManyCheckbox LazyInitializationException on process validation
Try:
<f:attribute name="collectionType" value="java.util.ArrayList" />;
on your <h:selectManyCheckbox>