JSF Output not coming as expected by using commandLink, ViewScope in dataTable - java

Below are the files I have.
manageStaff.xhtml
<h:dataTable var="c" value="#{newStaffMemberServiceBean.newStaffMemberDataBeanList}"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
border="1" id="staffListDataTable" width="100%">
<h:column>
<f:facet name="header">
Staff Member Name
</f:facet>
<h:form>
<h:commandLink action="viewStaffMemberProfileData" value="#{c.firstName}">
<f:setPropertyActionListener target="#{newStaffMemberServiceBean.userId}" value="XXXYYYZZZZ"/>
</h:commandLink>
</h:form>
</h:column>
</h:dataTable>
viewStaffMemberProfileData.xhtml
My Name is -<h:outputText value="#{newStaffMemberServiceBean.userId}" />-
NewStaffMemberServiceBean.java
#ManagedBean(name = "newStaffMemberServiceBean")
//#RequestScoped
#ViewScoped
//#SessionScoped
public class NewStaffMemberServiceBean {
private String userId;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
In output what I get is in below format
Staff Member Name
++++++++++++++++++
Name 1
Name 2
Name 3
.
.
.
.
On Clicking Name 1, I get re-directed to viewStaffMemberProfileData.xhtml.
BUT problems are
To get re-directed on viewStaffMemberProfileData.xhtml page, I have to click link (Name 1, Name 2, etc) twice :(
When I get re-directed to viewStaffMemberProfileData.xhtml I only see output as
My Name is --
What I want is My Name is -XXXYYYZZZZ-.
Suggest me where I am going wrong.
NOTE
I have all beans in ViewScope.
Update 1
#prajeeshkumar
Getter setter are as below
public List<NewStaffMemberDataBean> getNewStaffMemberDataBeanList() {
return newStaffMemberDataBeanList;
}
public void setNewStaffMemberDataBeanList(List<NewStaffMemberDataBean> newStaffMemberDataBeanList) {
this.newStaffMemberDataBeanList = newStaffMemberDataBeanList;
}

The reason for this behavior is the view scope of the newStaffMemberServiceBean , its being destroyed and created again... so the value is being nullified... here an alternative solution:
haven't tried this one, But worth trying
change
<h:commandLink action="viewStaffMemberProfileData" value="#{c.firstName}">
<f:setPropertyActionListener target="#{newStaffMemberServiceBean.userId}" value="XXXYYYZZZZ"/>
</h:commandLink>
into
<h:link value="#{c.firstName}" outcome="nameOfXhtmlGoesHere">
<f:param name="id" value="XXXYYYZZZZ" />
</h:link>
and in viewStaffMemberProfileData.xhtml
add this before the <h:head>
<f:metadata>
<f:viewParam name="id" value="#{newStaffMemberServiceBean.userId}" />
</f:metadata>
you can also add <f:event type="preRenderView" listener="#{newStaffMemberServiceBean.init}" /> to the f:metadata in order to do some init...
based on this BalusC great article - Communication in JSF 2.0
OR
you can turn the scope of newStaffMemberServiceBean into SessionScope...

If you have newStaffMemberServiceBean in view scope, then when you are going to a different page (view) will that bean be available in that scope ? I mean manageStaff.xhtml will have one instance and viewStaffMemberProfileData.xhtml will have another one. Your solution here will be using the session scope or using the h:link or h:outputLink

Related

Using JPA and Eclipselink to display data from existing database table in a JSP file

Unsure if I am missing a step in this process somewhere or if its just syntax as I am new to this structure. I am only going to back to my bean for now but if needed I can put up my logical class, model class and anything else.
This is what my JSP looks like:
<h:dataTable
id="viewExampleTable"
value="#{ExampleBean.exampleList}"
binding="#{ExampleBean.exampleTable}"
var="example"
styleClass="dataTable" border="1"
style="width: 500px; word-wrap: break-word;"
columnClasses="colWidth80,colWidth80,colWidth80,colWidth80">
<h:column>
<h:outputText value="example.Id"></h:outputText>
</h:column>
<h:column>
<h:outputText value="example.importDate"></h:outputText>
</h:column>
<h:column>
<h:outputText value="example.serviceId"></h:outputText>
</h:column>
<h:column>
<h:outputText value="example.rowTimestamp"></h:outputText>
</h:column>
</h:dataTable>
This is the Bean:
private HtmlDataTable exampleTable;
private ArrayList exampleList = new ArrayList();
public HtmlDataTable getExampleTable(){
return this.exampleTable;
}
public void setExampleTable(HtmlDataTable dataTable){
this.exampleTable = dataTable;
}
public ArrayList<Example> getExample(){
ArrayList<Example> list = ExampleLogic.getExampleRecords();
this.exampleList = list;
return this.exampleList
}
public void setExample(ArrayList<Example> list){
this.exampleList = list;
}
This issue I'm having is that whenever this is deployed to the server I am getting this error:
javax.faces.el.PropertyNotFoundException: Error getting property 'exampleList' from bean of type examplePath.view.ExampleBean
And just a blank screen. Any help is appreciated as I've done my best researching and am at a loss on how to proceed.
This issue was resolved by renaming the getter in the Bean to what was being called in the value parameter in the jsp data table.
Bean -> getExampleList()
jsp -> value="#{bean.ExampleList}"
This is only if I want to keep the ArrayList private as the error I was receiving was because the variable was private and the gettter didn't match what I was calling.

JF2 Dynamic form element doesn't remember input and page refreshes every time I add element

I am trying to make a form with options to add rows. However, after I type in some input and click the add button again, the input I enter disappears. I'm not sure what is wrong with my code. In addition, when I click the add button, the page refreshes. Is there way to stop this page refresh?
Person.java
public class Person{
private List<String> guys = new ArrayList<String>();
public List<String> getGuys() {
return guys;
}
public void setGuys(List<String> guys) {
this.guys = guys;
public void addNewItem(){
guys.add("");
}
}
form.xhtml
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Guys: " />
<h:dataTable value="#{person.guys}" var="men">
<h:column>
<p:inputText value="#{men}" />
</h:column>
</h:dataTable>
<h:commandButton name="add" value="Add" action="#{person.addNewItem}" />
</h:panelGrid>
<br />
<h:commandButton name="submit" type="submit" value="Submit"></h:commandButton>
</h:form>
Provided that the bean is placed in the right scope for the functional requirement, the view scope, the only major mistake left is that you're expecting that the String class has some magic setter method.
It hasn't. The String class is immutable. The following will never work on a String:
<p:inputText value="#{men}" />
You have 2 options:
Create a real model class. You can find complete examples in the following answers:
How to dynamically add JSF components
Recommended JSF 2.0 CRUD frameworks
Set the value by row index instead:
<h:dataTable binding="#{table}" value="#{person.guys}">
<h:column>
<p:inputText value="#{person.guys[table.rowIndex]}" />
</h:column>
</h:dataTable>
(note: no additional bean property necessary for the table! the code is as-is)
This does basically a person.getGuys().add(table.getRowIndex(), submittedValue). I.e. the setter is invoked on the List itself, which works perfectly fine. See also the following related answers concerning ui:repeat:
Using <ui:repeat><h:inputText> on a List<String> doesn't update model values
How map multiple inputText to an array property?
You never update your list, you are just adding empty items. You should do something like this:
Person.java (viewscoped)
public class Person implements Serializable {
private List<String> guys = new ArrayList<String>();
private HtmlDataTable dtGuys;
public void addNewItem() {
guys.add("");
}
public void addToList(ValueChangeEvent e) {
guys.set(dtGuys.getRowIndex(), e.getNewValue().toString());
}
public String save() {
System.out.println("saving...");
for (String item : guys) {
System.out.println("item= " + item);
}
return null;
}
//gettes and setters
}
form.xhtml
<h:form id="frmPrincipal">
<h:panelGrid columns="2">
<h:outputText value="Guys: " />
<h:dataTable value="#{person.guys}" var="men" binding="#{person.dtGuys}" >
<h:column>
<p:inputText value="#{men}" valueChangeListener="#{person.addToList}" />
</h:column>
</h:dataTable>
<h:commandButton name="add" value="Add" action="#{person.addNewItem}" />
</h:panelGrid>
<br />
<h:commandButton id="submit" name="submit" value="Submit" action="#{person.save}"/>
</h:form>
Using jsf 2.0.10 and primefaces 3.5
Its because you don't have an scope for that bean, so its request scoped, so when you call the action the bean is created again, you can fix this using a sessionScope or conversationScope

How to send/recieve data to/from bean function using JSF Richfaces AJAX?

I'm trying to get some code working in an XHTML/JSF/Spring application through which I send an ID to a bean function and expect a string in return. I haven't found an understandable tutorial on this nor any answered question here on SO.
XHTML:
<h:form>
<h:inputText id="inputId" value="#{npBean.idString}"/>
<a4j:commandButton value="get def" render="out">
<f:param value="#{npBean.idString}" name="id" />
<f:setPropertyActionListener target="#{npBean.definition}"/>
</a4j:commandButton>
<a4j:outputPanel id="out">
<h:outputText id="outputId" value="#{npBean.def}"
rendered="#{not empty npBean.def}"/>
</a4j:outputPanel>
</h:form>
Java:
public String getDefinition(int id)
{
def = this.getXService().getXData(id).getDefinition();
return def;
}
All values shown have their getters and setters in the bean.
What we basically do:
Map the value of the <h:inputText> component to a property (with getter/setter) in the managed-bean (which is called myBean)
By using the reRender attribute of the <a4j:commandButton> component, we point which component on the page to be re-rendered (refreshed) when the button is clicked.
When clicking on the button, the invokeService() method from the managedBean is executed and it updates the other property of the managedBean.
In the <h:panelGroup> below, we have several <h:outputText> components and with the rendered attribute we specify when a component has to be displayed on the page.
Exploring the managed-bean, the only thing that is required, are the accessors for the property, which holds the result from the service invoke.
Here's the *.xhtml sample:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<a4j:form>
<h:panelGrid columns="3">
<h:outputText value="String value:" />
<h:inputText value="#{myBean.value}" />
<a4j:commandButton value="Click" reRender="out">
<a4j:actionListener listener="#{myBean.invokeService}" />
</a4j:comandButton>
</h:panelGrid>
</a4j:form>
<rich:spacer height="7"/>
<br />
<h:panelGroup id="out">
<h:outputText value="Service returned: " rendered="#{not empty myBean.result}" />
<h:outputText value="#{myBean.result}" />
</h:panelGroup>
</ui:composition>
Managed-bean:
#ManagedBean(name = "myBean")
#SessionScoped //for example
public class MyBean {
private String value;
private String result;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getResult() {
return result;
}
public void invokeService(ActionEvent actionEvent) {
this.result = "Hello, " + value + "!";
}
}
As #Luiggi mentioned, the accessor methods MUST meet the following conventions (if we assume you have a private <some-type> property; in the managed-bean.)
public <some-type> getProperty {
return property;
}
public void setProperty(<some-type> property) {
this.property = property:
}
In order to learn how the RichFaces components work, combined with good code examples, I suggest you open this address and play around with the components.

primefaces tabview onChange not fired

i wish to display 2 different datatables in each tab of the tabview of primefaces 3.2.
there datatables will fetch data based on the 'type' variable set in the onChange event. but my problem is that the onChange event does not fire at all.
pls check my tabview code to check why this is happening:
<h:form id="frm">
<p:tabView activeIndex="#{equityBean.activeIndex}">
<p:ajax event="tabChange" listener="#{equityBean.onChange}" update=":frm"/>
<p:tab title="NSE" binding="#{equityBean.tbn}">
<p:dataTable binding="#{equityBean.dt}" value="#{equityBean.scripList}" var="scrip">
<f:facet name="header">
Scrip Symbol
</f:facet>
<h:outputText value="#{scrip.scripSymbol}"/>
<f:facet name="header">
Company Name
</f:facet>
<h:outputText value="#{scrip.companyName}"/>
<f:facet name="header">
Volume
</f:facet>
<h:outputText value="#{scrip.totalTradedVolume}"/>
</p:dataTable>
</p:tab>
<p:tab title="BSE" binding="#{equityBean.tb}">
</p:tab>
</p:tabView>
</h:form>
bean:
public void onChange(TabChangeEvent event) {
type=event.getTab().getTitle();
}
edited:
bean code to get datatable value:
public List<MasterScrip> getScripList() {
scripList=new ArrayList<MasterScrip> ();
scripList=getScripByVolumeType(type);
return scripList;
}
private java.util.List<service.MasterScrip> getScripByVolumeType(java.lang.String type) {
service.StatelessWebService port = service.getStatelessWebServicePort();
return port.getScripByVolumeType(type);
}
edited : jpa query
public Collection<MasterScrip> getScripByVolumeType(String type)
{
Collection sm=null;
sm=em.createQuery("select m from MasterScrip m where m.type = :type order by m.totalTradedVolume").setParameter("type", type).setMaxResults(2).getResultList(); // retuens no records
return sm;
}
records are returned but not displayed..
why does this happen? where am i wrong?
Your mistake is here.
listener="#{equityBean.onChange(event)}"
The event object does not exist in EL scope. Remove the argument. You don't need to specify it at all. JSF will supply the necessary action listener argument itself.
listener="#{equityBean.onChange}"
The same is true for all other listener and actionListener attributes. Only in action attribute of UICommand components you can specify custom arguments (which should be passed with real values, not with some random and non-existent variable name).

How to display and update a "sub-page" within a condition (using primefaces)

I have a scenario to achieve but I don't know how to do it(I'am using primefaces)
The scenario is:
I have a selectOneMenu that contain options that the user can choose one of them,
when the user choose an option ,a "form" or a "Sub-page" should appear below the selectOneMenu , the user should fill it.
The "Sub-page" change according to the choosen option
eg :option1 ---> Sub-Page1
option2 --->Sub-Page2 etc..
JSF
<h:selectOneMenu style="width:200px" immediate="true" value="#{ToolsKPI.myChoice}" onchange="submit()" valueChangeListener="#{BeanTest.selectChangeHandler}" required="true" >
<f:selectItems value="#{ToolsKPI.getMyListKPI()}" />
</h:selectOneMenu>
<p:panel rendered="#{BeanTest.showPanelBool}" >
<h1>KPI1</h1>
<br></br>
<h:inputText value="test1" />
</p:panel>
<p:panel rendered="#{BeanTest.showPanelBool1}" >
<br></br>
<h1>KPI2</h1>
<h:inputText value="test2" />
<h:inputText value="test3" />
</p:panel>
My Bean
public class BeanTest implements java.io.Serializable {
private String myChoice;
private boolean showPanelBool;
private boolean showPanelBool1;
public BeanTest() {
}
//getters ande setters
public void selectChangeHandler(ValueChangeEvent event){
myChoice = (String) event.getNewValue(); // Must however be the exact page URL. E.g. "contact.jsf".
FacesContext.getCurrentInstance().getExternalContext();
if(myChoice.equals("Number Of Closed issues") ){
this.showPanelBool = true;
} else{
this.showPanelBool = false;
this.showPanelBool1 = true;
}
FacesContext.getCurrentInstance().renderResponse();
}
}
You should really post some semblance of the code: a picture something. What you're probably looking for is a "p:outputPanel" component or something similar.
http://www.primefaces.org/showcase/ui/outputPanel.jsf
You can do something with a ValueChangeListener akin to
public void onSelectItem(ValueChangeEvent evt)
{
if("someValue".equals(evt.getNewValue())
showPageOne = true;//boolean
else
showPageTwo = true;//boolean
}
//JSF
<h:form id="myForm">
<h:selectOneMenu valueChangeListener="#{myBean.onSelectItem}" update="myForm">
<f:selectItems value="items">
</h:selectOneMenu>
<p:outputPanel rendered="#{myBean.showPageOne}">
hi I'm page one
</p:outputPanel>
<p:outputPanel rendered="#{myBean.showPageTwo}">
hi I'm page two
</p:outputPanel>
</h:form>
Sorry for the pseudo code, the examples on Primefaces will really help you here. OutputPanel has saved my bacon often. You'll need to specify an update target, but the example is really clear and easy to run. Play with it.
(Also Primefaces is fantastic you'll likely be very happy with their component suite)

Categories

Resources