Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't transfer from bean to jsp

 
Turian Vlad
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic