button text does not change
Hello,
I want to change the button text to "Log in"
if the user is not logged in.
The code bellow does not change the button text.
publicvoid prerender(){
if (getSessionBean1().getUserId() ==null){
button1.setText("Login");
staticText1.setText("Please log in.");
}
else{
button1.setText("Logoff");
staticText1.setText(getSessionBean1().getUserName()+" is logged in.");
}
}
What am I doing wrong?
Frank
[924 byte] By [
Fast_Frank] at [2007-11-26 11:59:59]

# 1
Hi,Your code is correct. but in addition, you need to zero out the initial text that appears in the textproperty for button1. Just clear out the "Button"text ( default text might still appear on the designer however.)/k
# 4
Hi again,
To explain a bit why you need to clear the text -- it's related to the application model . Take a look at the .jsp source for a button whose text has been cleared vs. one that is not. For more in depth, check out this white paper's section "Page Bean UI Component Properties":
http://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/app_ model.html
/k
# 5
> I never would have guessed about clearing the text
> first.
The following excerpt is from http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/abo ut_components.html#general
NOTE: As with the JavaServer Pages implementation, when the server constructs a page from JSP source, the tag attribute settings in the JSP source take precedence over runtime settings. For example, if you set the text property for a Static Text component to "moon," the IDE adds text="moon" to the Static Text component's tag in the JSP file. Even if the page's prerender method has a staticText1.setText("sun") statement, the Static Text component shows "moon" when the application renders the page. If the page is rerendered, the staticText1.setText("sun") statement takes affect and the Static Text component shows "sun." If you visit another page and come back, the server once again constructs the page from the JSP source, the values set in the JSP tag attributes take precedence, and the page displays "moon."
TIP: To ensure that property setting code always takes effect, do not set that property to a static value in the Properties window.
# 6
An alternative way to do this (that avoids the lifecycle issue and perhaps gives you a better design time view) is to set the text of the button to: #{SessionBean1.userId==null?'Login':'Logout'}
and the text property of the static text component to:
#{SessionBean1.userId!=null?SessionBean1.userName:''}#{SessionBean1.userId!=null?' is logged in':'Please log in'}
Message was edited by:
yossarian