Simple java question...  
Author Message
gbattine





PostPosted: 2006-8-21 21:11:00 Top

java-programmer, Simple java question... Hi guys,
please help me with this simple java question.
I'm developing a jsf application but i have a java question.
Suppose i have an authentication bean with 3 attributes,login,password
and team that load the values inserted in a login page.
In another bean i have a method that performs inserting an experiment
into a db.
My prepared statement is like...

pst2.setString(1, name);
pst2.setString(2, team);
pst2.setString(3, platform);
etc...

but i want name is the login attribute and team the team attribute of
authenticationBean.
How can i pass these values to my pst.2setString?

Please help me

 
M.J. Dance





PostPosted: 2006-8-21 21:30:00 Top

java-programmer >> Simple java question... gbattine wrote:
> Hi guys,
> please help me with this simple java question.
> I'm developing a jsf application but i have a java question.
> Suppose i have an authentication bean with 3 attributes,login,password
> and team that load the values inserted in a login page.
> In another bean i have a method that performs inserting an experiment
> into a db.
> My prepared statement is like...
>
> pst2.setString(1, name);
> pst2.setString(2, team);
> pst2.setString(3, platform);
> etc...
>
> but i want name is the login attribute and team the team attribute of
> authenticationBean.
> How can i pass these values to my pst.2setString?

Your "authentication bean with 3 attributes", say it's named auth, probably has
the means of aquireing such data. Either a public field or, more likely, a
getter of some sort. Like that: auth.name or auth.getName(). Stick that into
pst2.setString().

BTW: this is really basic stuff. You should do some RTFM on the subject (Java as
well as JSF).
 
Moiristo





PostPosted: 2006-8-21 22:45:00 Top

java-programmer >> Simple java question... M.J. Dance wrote:
> gbattine wrote:
>> Hi guys,
>> please help me with this simple java question.
>> I'm developing a jsf application but i have a java question.
>> Suppose i have an authentication bean with 3 attributes,login,password
>> and team that load the values inserted in a login page.
>> In another bean i have a method that performs inserting an experiment
>> into a db.
>> My prepared statement is like...
>>
>> pst2.setString(1, name);
>> pst2.setString(2, team);
>> pst2.setString(3, platform);
>> etc...
>>
>> but i want name is the login attribute and team the team attribute of
>> authenticationBean.
>> How can i pass these values to my pst.2setString?
>
> Your "authentication bean with 3 attributes", say it's named auth,
> probably has the means of aquireing such data. Either a public field or,
> more likely, a getter of some sort. Like that: auth.name or
> auth.getName(). Stick that into pst2.setString().
>
> BTW: this is really basic stuff. You should do some RTFM on the subject
> (Java as well as JSF).

I think he means that he wants to know how to use a backing bean in
another class. Although it's true that some RTFM would not be a bad
thing (:D). On topic, I use a static function to evaluate registered
javabeans (not my own idea). Create a class ViewUtils.java with this method:

public static Object eval(String expr) {
FacesContext context = FacesContext.getCurrentInstance();
ValueBinding binding
= context.getApplication().createValueBinding(expr);
return binding.getValue(context);
}

noy you can retrieve a managed bean in any class by calling:
Object bean = ViewUtils.eval("#{managed_bean_name}");
 
 
gbattine





PostPosted: 2006-8-21 23:00:00 Top

java-programmer >> Simple java question... thanks guys,
but my problem isn't resolved!
I've created this class

package giu;

import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

public class ViewUtils {
public static Object eval(String expr) {
FacesContext context = FacesContext.getCurrentInstance();
ValueBinding binding
= context.getApplication().createValueBinding(expr);
return binding.getValue(context);
}
}

and in my experiment class i have the add method that is like(i post
the important code)


if (conn != null) {
Object bean = ViewUtils.eval("#{authenticationBean}");
PreparedStatement pst2 = null;
pst2 = conn.prepareStatement("INSERT INTO
esperimento(username,nometeam,piattaforma,links_url,tipoesperimento,controlliqualita)
VALUES (?,?,?,?,?,?)");
pst2.setString(1, bean.loginName);
pst2.setString(2, bean.teamName);
pst2.setString(3, platform);

...................................etc
where authenticationBean is the bean from which i want extracting
loginName and teamName fields..
my compiler says loginName and teamName can't be resolved...please help
me!!!

 
 
Moiristo





PostPosted: 2006-8-21 23:46:00 Top

java-programmer >> Simple java question... gbattine wrote:
> package giu;
>
> import javax.faces.context.FacesContext;
> import javax.faces.el.ValueBinding;
>
> public class ViewUtils {
> public static Object eval(String expr) {
> FacesContext context = FacesContext.getCurrentInstance();
> ValueBinding binding
> = context.getApplication().createValueBinding(expr);
> return binding.getValue(context);
> }
> }
>
> and in my experiment class i have the add method that is like(i post
> the important code)
>
>
> if (conn != null) {
> Object bean = ViewUtils.eval("#{authenticationBean}");
> PreparedStatement pst2 = null;
> pst2 = conn.prepareStatement("INSERT INTO
> esperimento(username,nometeam,piattaforma,links_url,tipoesperimento,controlliqualita)
> VALUES (?,?,?,?,?,?)");
> pst2.setString(1, bean.loginName);
> pst2.setString(2, bean.teamName);
> pst2.setString(3, platform);
>
> ...................................etc
> where authenticationBean is the bean from which i want extracting
> loginName and teamName fields..
> my compiler says loginName and teamName can't be resolved...please help
> me!!!
>

Now this is *really* basic stuff. How much experience do you have in
basic java programming? Anyway, you have to cast bean to the appropriate
class.
 
 
gbattine





PostPosted: 2006-8-22 2:49:00 Top

java-programmer >> Simple java question... thanks,
but can you help me with code?
thanks

 
 
Moiristo





PostPosted: 2006-8-22 6:47:00 Top

java-programmer >> Simple java question... gbattine wrote:
> thanks,
> but can you help me with code?
> thanks
>

Just like: AuthenticationBean bean = (AuthenticationBean)
ViewUtils.eval("#{authenticationBean}");
 
 
gbattine





PostPosted: 2006-8-22 15:05:00 Top

java-programmer >> Simple java question... thanks very much Moiristo!!!!!!!!
Excuse my inexperience.....
thanks thanks