• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Set database value into another page .

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeetu Das
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear,
The 1st way is working fine .


I have tried by 2nd way its not working :

The code as :

page2.xhtml:
<tr>
<td><h:outputLabel value="Welcome"/>
<h:outputText 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
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic