Build your professional network on facebook via our app Go to app
 
<< Prev  7 of 9 in Topic  Next >>
Topic : Java enterprise system
  Rate : 
 
Industry : IT Products Functional Area : Programming Languages
Activity:  2 comments  357 views  last activity : 07 06 2010 20:18:04 +0000
Share
 
 
 

Hi friends,
after JPA & JPQL I decided merge this nice guy with Mr. JSF, but I think it's better talk about some JSF tips that may helpful for next post
these 2 tips may are helpful for beginners like me.
as always as ever lets talk about these with examples, with 2 class user.java & book.java

Okay lets talk about how can we play with 'faces-config.xml' stuff?
#1: preset (pass) managed beans attributes
for example we have this managed bean
package pkg;
public class user{
private email,password;
public void setEmail(String email){this.email=email;}
public String getEmail(){return this.email;}
public void setPassword(String password){this.password=password;}
public String getPassword(){return this.password;}
}

now I can set attributes when I'm creating my managed bean for page.jsp
so just a little change in 'faces-config.xml'
...
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>pkg.user</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property> <!--[0]-->
<property-name>email</property-name> <!--[1]-->
<value>del@icio.us</value> <!--[2]-->
</managed-property>

</managed-bean>
...

in above code
as you see we preset the new managed bean instance's attributes, then I f you get the property in a JSP page ('<h:outputText value="#{user.email}" />') the return will be 'del@icio.us'
[0]: start to setting attributes with <managed-property> element, don't forget for setting each attribute use one of this guy, for example this won't work
...
<managed-property>
<property-name>att1</property-name>
<value>value1</value>
<property-name>att2</property-name>
<value>value2</value>
</managed-property>
...

[1]: specify the attribute name witch do we want set it with <property-name> element
Note: remember attributes in managed bean (for setting) should have 'setter' method
[2]: and set the value with <value>
Note: by default if you put for example 'X', system behaves with X as String (or numeral) object, but how do we can preset (for example) 'pkg.class object' attribute?
for example, imagine we have an attribute in class user with pkg.book datatype
...
private pkg.book theBook;
public setTheBook(pkg.book theBook){this.theBook=theBook;}
...

now, 1:we should have something that return (current/new) instance of pkg.book, for example something like this in class book
package pkg
public class book{
...
public pgk.book getInstance(){return this;} //return current instance
...

now all we have to do, is get the instance and set (pass) it to class user, then
...
<managed-property>
<property-name>theBook</property-name>
<value>#{book.instance}</value> <!--[0]-->
</managed-property>
...

in above code
[0]: you can get the attributes same as getting in a JSP page (#{} factory)
so now you can get the current (if exists) book instance
task:try to do something like this

#2:get current managed bean instance
as we saw in tip#1 we can preset attributes with any data type, get from another and set(pass) to other
okay, sometime (for example) you need 'X' attribute value witch located in pkg.beanClass managed bean, but you want it temporary, something like get the current managed instance or attribute
for example, get the user bean's name attribute from a method located in book bean, then
...
public String getName(){
String name=(String)FacesContext.getCurrentInstance().getApplication().createValueBinding("#{user.name}").getValue(FacesContext.getCurrentInstance()); //[0]
if(us==null){return "no name";}else{return name;} //[1]
}
...

in above code
[0]: createValueBinding().getValue() returns Object, so remember cast it
[1]: if you don't have instance of user bean, the return object is always null, so check this guy
we just got an String value, like the tip#1, yes we can get another data type, I mean any data type, then
...
public boolean getAccess(){
pkg2.users us=(pkg2.users)FacesContext.getCurrentInstance().getApplication().createValueBinding("#{user.stuff}").getValue(FacesContext.getCurrentInstance());
if(us==null){return false;}else{return us.getAdmin();}
}
...

in above code, the 'stuff' attribute in user backbean is a pkg2.users object, it can be an Entity bean

I hope this article were helpful for you, so please try to do and develop it yourself for understanding for ever

 Top Comment : Raji S   | 05 04 2009 07:34:39 +0000
Arash nice articles from you its good that you are sharing these things on the platform...i have seen your previous posts also on JPA and JPQl..thanks for the post...and will look forward for your posts....
 
TrackBack URL:
2 comments on "preset (pass) managed beans attributes"
  Commented by  Radhakrishna Marar, Business Analyst, Oracle    | 05 05 2009 08:46:37 +0000
Rating : +1 
Very informative article.hope to see more like these in future also.
  Commented by  Raji S, Business Analyst, SAP    | 05 04 2009 07:34:39 +0000
Rating : +1 
Arash nice articles from you its good that you are sharing these things on the platform...i have seen your previous posts also on JPA and JPQl..thanks for the post...and will look forward for your posts....
Add your comment on "preset (pass) managed beans attributes"

Rate:
Submit
 
Viewers also viewed
Good relation with top management vs Performing consistently in the job
 
1374 referals 13 arguments, 165 views
How egg yolk and soya bean oil finally got pregnancy when there was no hope left This is a true...
 
1083 referals 16 comments, 195 views
Education is nothing but giving knowledge to individuals, some grab it quickly some may take...
 
1780 referals 37 arguments, 1206 views
more...  
Recent Knowledge (242)
Once upon a time a tortoise and a hare had an argument about who is faster. They decided to...
 
2174 referals 32 comments, 328 views
Understanding client’s needs holds in itself the key to run a successful and sustained business....
 
1 referals 3 comments, 18 views
Review these typical interview questions and think about how you would answer them. Read the...
 
566 referals 91 comments, 3048 views
more...  
More From Author
because they don't want
each project had got a primary goal, in toostep the sharing knowledge is important, however toostep has some problem still, such as commens word-wrapping, editing comments, and Also heavy JQuery-ajax pages, it's okay to having many futures here, but...
I don't know, but I think it doesn't matter
more...