aspose file tools*
The moose likes JSF and the fly likes can't transfer from bean to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "can Watch "can New topic
Author

can't transfer from bean to jsp

Turian Vlad
Greenhorn

Joined: Oct 17, 2009
Posts: 12
Hy guys,
i was trying to build an application to communicate with a database.
the connection is not the problem.
the problem is that in the bean i have the method below

public static String cauta()
{
PreparedStatement pstmt = null;
ResultSet rs=null;
String numm=null;
String prenumm=null;

try{
// Create a statement
//Statement stmt = conn.createStatement();


String sql = "Select * FROM Users WHERE Username=? ";
pstmt = con.prepareStatement(sql);
// Set the value
pstmt.setString(1, username);
rs= pstmt.executeQuery();
if (rs.next()) {

numm=rs.getString("Name");
prenumm=rs.getString(3);

System.out.println(numm+" "+prenumm);
System.out.println("SQL statement is executed!");
rez= "S-a gasit userul cautat. Se numeste: "+numm+" "+prenumm;
return rez;
}
else {
rez= "User-ul cautat nu a fost gasit";
return rez;
}

}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
s.printStackTrace();
}

catch (Exception e){
e.printStackTrace();
}
return null;
}

the method works just fine and return the appropriate string

but in the jsp file i can't manage to acces this method in order to display that string in my page.
i've tried even javascript but it won't work

<hutputText value="Username"/>
<h:inputText id="user" value="#{Bean.username}" autocomplete="on"/>

<h:commandButton onclick="alert(Core.JSPBean.getRez().toString())" action="#{Bean.cauta}" id="submit" value="Cauta" />


are there any other ways in which i can display this damn message on my page?

Thanks for your help!


Freedom cannot be bought! You say the price is small?!... do not fool yourselves... It will get bigger and bigger until one day when the price of freedom will be freedom itself! (Vlad The Impailer)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15646
    
  15

Yes there is, but the fact that you're asking the question means that you need to learn a LOT more about JSF. Welcome to the JavaRanch, by the way!

A JSF action processor doesn't return a display string, it returns a dispatching directive. This directive - which is often a simple word like "success", "failure", or "not authorized" is used as a key into the navigations rules in the jsf config file to determine what view will be displayed when the action returns to the client. If there is no match, the previous view is displayed.

To return a value, make that value a property in a backing bean (typically the same bean that defines the action processor method). Then supply standard JavaBean setter/getter methods for it. You can then reference the returned value as a backing bean property in your displayed view. For example:


I recommend NOT capitalizing bean ID names, by the way. Leave that for classnames.

Note that although I used the same name (rez) as you did in your sample code, it's not the same "rez". That one would have gone out of scope when the method returned. That's why you need a "private String rez;" property in your backing bean.

Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't transfer from bean to jsp
 
Similar Threads
A Very little problem in h:dataTable implementation
Session variable returns null value
JSP problem
put arraylist in session?
Help needed in login page