aspose file tools*
The moose likes Struts and the fly likes Struts: getting null from bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts: getting null from bean" Watch "Struts: getting null from bean" New topic
Author

Struts: getting null from bean

Lex Wouda
Greenhorn

Joined: Mar 07, 2001
Posts: 22
Hi,
I've setup a very simple app to test the Struts framework.
1. input.jsp with form which calls
2. InputAction - interacts with
3. InputForm, which is a sort of copy of a 'normal' bean, InputForm has the Struts specific methods extra.
4. a result.jsp (configured in struts-config.xml when there is a successfull workflow).
in InputAction I put a instance of the normal bean into the request object.
In result.jsp I can retrieve the values I've put in in input.jsp via <bean:write name="user" property="username" /> etc.
However, according to the documentation, a form on result.jsp with in that form the following tag <html:text property="username" size="50"/> should give the username also.
This doesn't work.
In the bean class I've put some extra debug info and this is the result I get from console.
What am I doing wrong?
I would like to see the input fields in result.jsp filled with the values I've entered in input.jsp
(sorry for the long piece of ouput, I dunno how to layout it properly for this forum..)
I SRVE0091I: [Servlet LOG]: action: Processing a POST for /input
[02.05.06 18:13:36:708 CEST] 2dd5cf97 WebGroup I SRVE0091I: [Servlet LOG]: action: Setting locale 'en_US'
[02.05.06 18:13:36:738 CEST] 2dd5cf97 WebGroup I SRVE0091I: [Servlet LOG]: action: Looking for ActionForm bean under attribute 'inputForm'
[02.05.06 18:13:36:758 CEST] 2dd5cf97 WebGroup I SRVE0091I: [Servlet LOG]: action: Creating new ActionForm instance of class 'InputForm'
[02.05.06 18:13:36:778 CEST] 2dd5cf97 WebGroup I SRVE0091I: [Servlet LOG]: action: Storing instance under attribute 'inputForm' in scope 'session'
[02.05.06 18:13:36:778 CEST] 2dd5cf97 WebGroup I SRVE0091I: [Servlet LOG]: action: Populating bean properties from this request
[02.05.06 18:13:36:938 CEST] 2dd5cf97 SystemOut U setting the username in User object, username= test_username
[02.05.06 18:13:36:938 CEST] 2dd5cf97 WebGroup I SRVE0091I: [Servlet LOG]: action: Validating input form properties
[02.05.06 18:13:36:948 CEST] 2dd5cf97 SystemOut U getting the username from the User object, username= test_username
[02.05.06 18:13:36:948 CEST] 2dd5cf97 SystemOut U getting the username from the User object, username= test_username
[02.05.06 18:13:36:958 CEST] 2dd5cf97 WebGroup I SRVE0091I: [Servlet LOG]: action: No errors detected, accepting input
[02.05.06 18:13:36:968 CEST] 2dd5cf97 WebGroup I SRVE0091I: [Servlet LOG]: action: Looking for Action instance for class InputAction
[02.05.06 18:13:36:968 CEST] 2dd5cf97 SystemOut U getting the username from the User object, username= test_username
[02.05.06 18:13:37:309 CEST] 2dd5cf97 SystemOut U getting the username from the User object, username= test_username
[02.05.06 18:13:37:339 CEST] 2dd5cf97 SystemOut U getting the username from the User object, username= null
My guess is, when the <bean:write etc.> tag is called it retrieves the username from the bean (test_username) but when <html:text etc.> tries to do the same, suddenly the bean is not existing anymore?
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
The <html:text> tag will only work inside an <html:form> tag. The name of the html:text tag must correspond to a getter method on the form bean.
You must also define the form-bean in the form-beans tag in struts-config.xml, and also tell struts which form-bean to use in the action tag there too.


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Lex Wouda
Greenhorn

Joined: Mar 07, 2001
Posts: 22
Originally posted by Adam Hardy:
The <html:text> tag will only work inside an <html:form> tag. The name of the html:text tag must correspond to a getter method on the form bean.


You must also define the form-bean in the form-beans tag in struts-config.xml, and also tell struts which form-bean to use in the action tag there too.

both issues were already taken care of.
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
Sorry but from your first post it seemed that you were confusing the form bean and the user bean. The user bean isn't actually part of the struts framework, I think it's just used as a quick and dirty way of storing login info in the struts example apps.
The bean tag you have that works gets its info from the user bean. (name="user")
The html:text gets its info from the form bean. So the form bean is not there. Either it wasn't created because it's not configured in struts-config.xml, or it has been destroyed, and the code that destroys it might be in your action sub-class's perform - look for code like this:
request.removeAttribute(mapping.getAttribute());
Also of course it might have been put in the session and not the request.
Lex Wouda
Greenhorn

Joined: Mar 07, 2001
Posts: 22
thanks,
the request.removeAttribute line did it.
I was confused about this line because in the sample application that comes with strus, it sometimes is present in the Action (SaveRegistrationAction.java) and sometimes not (EditRegistrationAction.java).
Because in the sample app, the forward (for the registration.jsp) is also to the registration.jsp, the data is persistant, I figured that somewhere far back in the Struts package the form-bean was set or something.
For my own understanding:
create jsp.
jsp --> Action class (fills automatically with form-bean)
when not removed from request/session, this form-bean is everywhere accessible.
optional; create your own 'normal' bean and map it to a form-bean or whatever, but this bean must be set in the request/session manually to retrieve it later.
kwwong wong
Ranch Hand

Joined: Feb 15, 2002
Posts: 56
How to set the name of the form when using <html:form> ?
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Originally posted by kwwong wong:
How to set the name of the form when using <html:form> ?

I did not find it in the struts documentation. But for what do you need it?
If you need to do some javaScript action you can access the form programatically by
<javascript>document.forms[0]</javascript>
For what else do you need name attribute of form-tag?
kwwong wong
Ranch Hand

Joined: Feb 15, 2002
Posts: 56
In some conditions, document.forms[0] is not work.
I have a javascript download from internet, it pass a field name as a parameter. When I use form name as a parameter, it work. But when I pass a forms[0] as parameter, it fails.
[ October 24, 2002: Message edited by: kwwong wong ]
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
how about "name"? If you're not sure about an attribute, just check in the .tld file.
lusha tak
Ranch Hand

Joined: Dec 30, 2000
Posts: 185
Hello!!!
i am facing the same problem as Lax, and i think it can also be solved once i remove the following from the FormTag class
But now my confusion is how do i go about replacing the changed file??? coz. i just have struts.jar in my lib directory and *.tld in my classes folder...where and how to chage the code provided by struts framework???
pl. help....
Thanks in advance
Lusha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts: getting null from bean