my dog learned polymorphism*
The moose likes JSF and the fly likes accessing input text field in controller class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "accessing input text field in controller class" Watch "accessing input text field in controller class" New topic
Author

accessing input text field in controller class

Kondapally Ashwin
Greenhorn

Joined: May 16, 2006
Posts: 25
Hi,

I have just started learning jsf and tried out an example in RAD. I have an input text field in login.jsp, which I want to retrieve in doLoginButtonAction() to perform validation. In Login.java class (In PageCode folder in RAD),in the method doLoginButtonAction() I wrote the code as
"String userName = (String)usernamefield.getValue();"
where usernamefield is the id value in the login.jsp page.

But I am getting a null pointer exception. I have a bean class User.java where I wrote the setter and getter methods for usernamefield and bound this bean to username field in Login.jsp and result.jsp. In result.jsp I just want to display the username from login.jsp after clicking on the submit button. I also have the navigation rule configured to go to result.jsp for action "success". I just want to check that the username is in a predefined list of names (which I have added in an arraylist). But I am unable to retrieve the username field in Login.java class.

Please advise.
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Hi there,

Can you post the code here. The jsf file and the bean.

The field in the jsf file should look something like

<h:inputText value="#{userBean.usernameField}"> in order to set the value on the bean. Also you need to have the bean specified in the jsf xml config file.

So I reckon it's best to post the actual code (jsf, bean and jsf xml config file) here so that we can help you better.

cheers
Darryl
Kondapally Ashwin
Greenhorn

Joined: May 16, 2006
Posts: 25
code for login.jsp



code for Login.java (class automatically generated by rad)

only for two methods :
1)



2)


code for inbox.jsp (output page)



code for faces-config.xml



Know that this is a lot of code but can't help.
Kondapally Ashwin
Greenhorn

Joined: May 16, 2006
Posts: 25
forgot the bean

here it goes

Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Howzit,

A couple of questions.

The method doLoginButtonAction(), is that in the User bean??

Reason I ask is you call in the bean code the field userName, yet in the above mentioned method you are trying to access a field named usernamefield. and it is null. so change usernamefield to userName if the doLoginButtonAction method is in the User bean.

Otherwise everything initially looks right..

cheers
Darryl
Kondapally Ashwin
Greenhorn

Joined: May 16, 2006
Posts: 25
Hi Darryl,

Thanks for the reply. I really appreciate your help. And sorry for the late reply.

The method doLoginButtonAction() is in Login.java a class automatically generated by IBM RAD IDE in the page code class. What I had learnt from a book was to write the controller code i.e the action performed when the login button is clicked on. Here I didnt enter the code which has some methods automatically built by the tool. The method is not in User.java bean class.

Looking forward for your reply...
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Could you then post the entire Login.java code here.

It looked as if in your first post that you were trying to get the value of username from the variable called usernamefield, but in your Login.java src that you posted here you named the username field "private String userName;"

So what I'm saying is either rename the variable to usernamefield, and adjust the mutators and accessors accordingly, OR change the line
String userName = (String)usernamefield.getValue();
To use the getter from the Login .java class...

If this doesn't work, post the entire User.java here. I'll have a look at it, I don't use the IBM rad tool for generating anything, so it might be doing some other wierd things, which is not entirely unthinkable.

cheers
Darryl
Kondapally Ashwin
Greenhorn

Joined: May 16, 2006
Posts: 25
Hi Darryl,

Thanks for your valuable advice. I understood the problem. Instead of giving usernamefield.getValue(), I had to use a getter method getUserNamefield().getValue() in the Login.java method. The RAD tool generated the getter method getUserNamefield() in the Login.java method which I had overlooked. Here is the code for that method:



Now the application is working!! Thanks once again!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: accessing input text field in controller class
 
Similar Threads
regarding focus in swing
Status 405 - HTTP method GET is not supported by this URL
static results in JSP pages
Cookies example in HFSJ
Working on Tapestry Validation