File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Set database value into another page . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Set database value into another page ." Watch "Set database value into another page ." New topic
Author

Set database value into another page .

Jeetu Das
Greenhorn

Joined: May 12, 2012
Posts: 12
If I am navigating from page one to page two, When I click on Page one's submit button, control goes to the managed bean written corresponding to page one, I want to fetch some result from database and show them to page two, I fetched the data from the data base . i want to show the values in another web page . for this i have initialized bean2 from the bean1 method and set all the values .
when i am going to second page the value is not displaying in the page .
t dont know this is the proper way to do it or not .
please hel[p me .

Thank you
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

You can do this in 2 ways.

1. You can keep bean1, save the value in bean1 as a property and then have page2 reference bean1's property. There's no law that says that each page can have exactly one and only 1 backing bean or that each page must have a distinct backing bean; share beans if it works better for you.

2. You can inject bean2 into bean1 as a Managed Property and make bean1 inject the database value into bean2 using a bean2 property set method. This is generally best done in the action method that causes navigation to bean2.

In either of the above cases, it's important to pay attention to bean scope. Whichever way you do it requires session scope for the bean that's holding the database values referenced by page2, since any shorter-lived scope (View Scope, Request Scope) will destroy the bean and create a new, uninitialized bean for the second page.

Customer surveys are for companies who didn't pay proper attention to begin with.
Jeetu Das
Greenhorn

Joined: May 12, 2012
Posts: 12
Thanks a lot sir.

I have a question , cant it possible by direct setting the values in the bean2 .
I have tried the same its giving the proper result .
Can i know the reason if its not possible .


i will try the solution given by you and reply .
Regards,
Jitendra
Jeetu Das
Greenhorn

Joined: May 12, 2012
Posts: 12
Dear,
The 1st way is working fine .


I have tried by 2nd way its not working :

The code as :

page2.xhtml:
<tr>
<td><hutputLabel value="Welcome"/>
<hutputText value="#{loginBean.ulogin.username}"/>
</td>
</tr>


LoginBean.java:
private UserLoginDetails ulogin;



public UserLoginDetails getUlogin() {
return ulogin;
}

public void setUlogin(UserLoginDetails ulogin) {
this.ulogin = ulogin;
}


UserLoginDetails.java:

package com.jitu.soft.userBean;

public class UserLoginDetails {

private String username ;

public UserLoginDetails() { }

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}


Set Variable :

public String validateUserDetails(){
HibernateUtil hibernateUtl = new HibernateUtil();
Session sesson = hibernateUtl.sessionFactory();
UserProfileMaintaince mnt = null;


Query qry = sesson.createQuery("select id, name,password from UserProfileMaintaince where id =:id");
qry.setParameter("id", getUsername());
List list = qry.list();

//Object [] obj = new Object[]{list.get(0)};
Object[] obj = (Object[])list.get(0);
String storedId = obj[0].toString();
String storedName = obj[1].toString();
String actPassword = obj[2].toString();

LoginBean lb = new LoginBean();
UserLoginDetails uld = new UserLoginDetails();
uld.setUsername("Jitu");
lb.setUlogin(uld);


if(storedId.equalsIgnoreCase(getUsername()) && actPassword.equals(getPassword())){
UserLoginDetails userLoginDetails = new UserLoginDetails();
userLoginDetails.setUsername("Jitendra");
return "CustomerDashboardTemplete";
}else{
return "Failure";
}
}



Please suggest if i have done any wrong .


Regards,
Jitendra
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

The first thing you did wrong was you wrote your own login code. Stay around me for very long and you'll quickly learn I'm very stridently against that. The reason for that is because I've never actually seen a user-created security system that was very secure. J2EE has a built-in security system, and most webapps should be using it, not supplying their own.

The second thing you did wrong was that you didn't use the "Code" button on the JavaRanch message editor. The Code tags that this button creates can be used to wrap code, XML, and other preformatted text and present them in a way that's much more readable than regular message text - and doesn't lose all your formatting.

Because your sample was large and not well-formatted, I couldn't read all the details, but you do not appear to be using the JSF injection mechanism. I do recommend you get a good book on JSF and read what it has to say about Managed Properties.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set database value into another page .