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