Programmatically instantiate view scoped bean

UI Components for JSF
Post Reply
luigi.toziani
Posts: 55
Joined: 08 Mar 2010, 17:11

21 Apr 2010, 11:11

Hi,

I'm actually trying to pass a view scoped bean to the next page. The first column in datatable(see DATATABLE.xhtml) is an <h:commandLink> with a parameter containing the id I need for making a query to DB. I've defined a Navigation Handler in faces-config.xml define the mechanism for forwarding the request to another xhtml page once the action #{metadataClassTable.edit} has done. The new page (EDIT.xhtml) use the view scoped bean #{metadataClass} that I try to populate into the "edit" method:

The things work only when using session as scope. With view scope the input textfields in the EDIT page are empty and I've some problem for displaying collection data of metadataClass in a datatable using dynamic="true" attribute (With dynamic="false" instead I can see List collection data).

Someone knows what is the correct way for resolving view scoped bean programmatically?

Snippet of 'edit' action: #{metadataClassTable.edit}

Code: Select all

FacesContext context = FacesContext.getCurrentInstance();
ELResolver resolver = context.getApplication().getELResolver();
MetadataClass wsClass = (MetadataClass) resolver.getValue(context.getELContext(), null, "metadataClass");

wsClass.setMetaClassName("Mock Name");
wsClass.setDescription( "Mock Description" );
List<MetaInfo> infoes = new ArrayList<MetaInfo>();
infoes.add(new MetaInfo());
wsClass.setSomeList(infoes);
1° page: DATATABLE.xhtml

Code: Select all

 
<p:dataTable  var="mdClass" value="#{metadataClassTable.list}" 	rows="10" dynamic="true">
	
			       <p:column>
					<f:facet name="header">
						<h:outputText value="" />
					</f:facet>
					<h:commandLink  value="Edit"  id="editLink" action="#{metadataClassTable.edit}" >
						<f:param name="classID" value="#{mdClass.idMetadataClass}"/>
					</h:commandLink>
				</p:column>

                                <p:column  sortBy="#{mdClass.className}" filterBy="#{mdClass.className}">
					<f:facet name="header">
						<h:outputText value="#{bundle.gui_metaClass_name}" />
					</f:facet>
					<h:outputText value="#{mdClass.className}" />
				</p:column>
	
				<p:column filterBy="#{mdClass.description}">
					<f:facet name="header">
						<h:outputText value="#{bundle.gui_metaClass_description}" />
					</f:facet>
					<h:outputText value="#{mdClass.description}" />
				</p:column>
		</p:dataTable>
2° page: EDIT.xhtml

Code: Select all

<h:form prependId="false">  
     <p:panel id="panel" header="Metadata Class">  
         <p:messages id="msgs" showDetail="true"/>
         <h:panelGrid columns="3">  

             <h:outputLabel for="metaName" value="#{bundle.gui_metaClass_name}: *" />  
             <h:inputText id="metaName" value="#{metadataClass.metaClassName}" required="true">  
                 <f:validateLength minimum="2" /> 
                 <p:ajax event="blur" update="metaNameMsg, growl,msgs"/> 
             </h:inputText>
             <p:message for="metaName" id="metaNameMsg" />
             
               
             <h:outputLabel for="description" value="#{bundle.gui_metaClass_description}: " />  
             <h:inputText id="description" value="#{metadataClass.description}" size="50"/>  
             <p:message for="description" />
               
         </h:panelGrid>  
     
     	 <p:commandButton id="delete" value="Delete" update="growl,msgs" 
     	 			async="true" actionListener="#{metadataClass.delete}"
     	 			oncomplete="toggleDisabled()" onstart="toggleDisabled()"/>
      	<h:commandButton id="backButton"  value="Back" action="back"/>
      </p:panel>  
    </h:form>

User avatar
ydarcin
Posts: 258
Joined: 04 Jan 2009, 19:02
Location: Turkey

21 Apr 2010, 13:39

Hi,

did you tried using f:setPropertyActionListener?

Yigit

rene.guenther
Posts: 56
Joined: 19 Oct 2009, 15:56

21 Apr 2010, 14:04

your data table belong to view 1, your edit view is view 2. So view scope only keeps view 1, when you transfer to view 2 your data is lost. The action #{metadataClassTable.edit} belongs still to view 1, so if you user the <f:set.. like Yigit suggested the action method also has store the value somewhere to its not lost while tranfering to view 2.

some code snippets of our application:

Code: Select all

getFacesContext().getExternalContext().getRequestMap().put("id", id);
Then in a getter method accesed by view 2 we added

Code: Select all

 if (getFacesContext().getExternalContext().getRequestMap().get("id") != null) {
            id = (Long) getFacesContext().getExternalContext().getRequestMap().get("id");
...

luigi.toziani
Posts: 55
Joined: 08 Mar 2010, 17:11

21 Apr 2010, 14:51

I'm glad for your answer.

I'm actually trying to do with <f:setPropertyActionListener target="#{metadataClass.metaClassName}" value="view_1 value..."/> where target is the bean in view scope for which I want to set the property for. But once reached "view 2" I cannot show property in h:inputText for that bean. Obviously with session scope I can but manage this kind of bean with this scope is a little mess.

I just need to forward a view scope bean (prefilled with its properties) to the next page.

I'm a little confused. :oops:

rene.guenther
Posts: 56
Joined: 19 Oct 2009, 15:56

21 Apr 2010, 17:38

Whats the name of the action method you are calling? In that action method the value will be available. At that point you have to store the value, eg. in the request map.
Then in getMetaClassName check the request map, take the value, clear request map.

Like (just apply the concept to your case):

Code: Select all

if (getFacesContext().getExternalContext().getRequestMap().get("id") != null) {
            id = (Long) getFacesContext().getExternalContext().getRequestMap().get("id");
    getFacesContext().getExternalContext().getRequestMap().put("id", null);
cheers
rene

raziel
Posts: 48
Joined: 11 Feb 2010, 20:45

21 Apr 2010, 17:39

I just need to forward a view scope bean (prefilled with its properties) to the next page.
you can do that with t:saveState i think. I dont know how Trinidad components are behaving with Faces 2.0 but in Faces 1.2 saveState solved this use case.

luigi.toziani
Posts: 55
Joined: 08 Mar 2010, 17:11

21 Apr 2010, 21:33

Hi Rene,

I'm calling #{metadataClassTable.edit} (1° page view). Anyway I get it work now with the approach you suggest. I'll investigate deeper for this requirement. I'm surprised that JSF 2 doesn't provide anything for this.

Another solution is maybe to build a custom scope for the bean but I still didn't find any useful article about. If you know some good one, I'll appreciate very much. :)

Thanks for your quick answer!

Luigi
raziel wrote: you can do that with t:saveState i think. I dont know how Trinidad components are behaving with Faces 2.0 but in Faces 1.2 saveState solved this use case.
Thanks Raziel for your suggest but I'm actually not using trinidad. Anyway I'll keep it in mind, great! ;)

luigi.toziani
Posts: 55
Joined: 08 Mar 2010, 17:11

22 Apr 2010, 10:09

Hi my friend,

As I mentioned earlier, if in metadataClass bean I've a List<MyObject> to present in a <p:datatable dynamic="true"> the only way to make it work is initialize the list in the constructor.
If the list is initialized in a setter method after bean creation the list doesn't show. The setter method case only works with a dynamic="false" attribute in datatable.

MetadataClass.java

Code: Select all

public void setMetaClassName(String metaClassName) {
		this.metaClassName = metaClassName;
                this.myObjectlist = populateList(metaClassName);  //I need metaClassName for making a DB query
	}
EDIT.xhtml (2° view)

Code: Select all

 
<p:datatable var="myObject" value="metadataClass.myObjectList" dynamic="true"> //With setter method approach dynamic="false" is needed to make it work
....
..

cheers
Luigi

luigi.toziani
Posts: 55
Joined: 08 Mar 2010, 17:11

22 Apr 2010, 15:16

Sorry but I've problem again. In view scope I cannot pass any parameter in my action method...

EDIT COLUMN IN DATATABLE

Code: Select all

<p:commandLink  value="Edit"  ajax="false" id="editLink" action="#{metadataClassTable.edit}" >
	<f:param name="paramClassName" value="#{mdClass.className}"/>
	<h:setPropertyActionListener target="#{metadataClassTable.metadataClassProperty}" value="#{mdClass.className}"/>
</p:commandLink>
MetadataClass.edit()

Code: Select all

   String className = (String) exContext.getRequestParameterMap().get("paramClassName"); //IS NULL!
   metadataClassProperty = null; //Nothing setted  

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 40 guests