nested beans in struts?

D

Daniel

Hi all,

I am having some difficulty accessing nested beans inside an ActionForm
with Struts (latest version).

I have a User object inside the form, and in the JSP page, I am trying
to call some properties on the User object:

<html:text property="user.username"/>
<html:text property="user.password"/>

I get the following error when accessing the JSP page:

javax.servlet.ServletException: Invalid argument looking up property
user.username of bean org.apache.struts.taglib.html.BEAN

If I use <html:text property="user"/> I don't get any errors but it
doesn't like user.username or user.password.

My form and User classes are as follows:

public class User
{
public String username;
private String password;
private String firstname;
private String lastname;
private String phone;
private String email;
private String state;

public User()
{
}

public String getUsername()
{
return username;
}

public void setUsername( String username )
{
this.username = username;
}

public String getPassword()
{
return password;
}

public void setPassword( String password )
{
this.password = password;
}

public String getFirstname()
{
return firstname;
}

public void setFirstname( String firstname )
{
this.firstname = firstname;
}

public String getLastname()
{
return lastname;
}

public void setLastname( String lastname )
{
this.lastname = lastname;
}

public String getPhone()
{
return phone;
}

public void setPhone( String phone )
{
this.phone = phone;
}

public String getEmail()
{
return email;
}

public void setEmail( String email )
{
this.email = email;
}

public String getState()
{
return state;
}

public void setState( String state )
{
this.state = state;
}
}


public class Form extends ActionForm
{
private User user;

public User getUser()
{
return user;
}

public void setUser( User user )
{
this.user = user;
}

public Form()
{

}
}



Thanks in advance!
 
D

David McDivitt

What you describe is not a nested bean, but multiple beans.

You should not use class.field syntax. In struts config set up an arbitrary
form bean and have it reference the actual bean class. Make sure the actual
form bean class has getters and setters with precise syntax. Use string
variables only. If the string variable name is "code", the getter and setter
would be getCode and setCode. For this example, in the jsp use
"<html:text property="code"/>
In struts config set up an action. Have the action use the arbitrary form
bean name and forward to the jsp.
 
W

Wendy S

Daniel said:
I am having some difficulty accessing nested beans inside an ActionForm
with Struts (latest version).
I have a User object inside the form, and in the JSP page, I am trying
to call some properties on the User object:
<html:text property="user.username"/>
<html:text property="user.password"/>

Shouldn't you be using the <nested: > taglib if you're nesting beans?
http://struts.apache.org/userGuide/struts-nested.html

I'm not aware that 'dot notation' works in the regular Struts tags, but I
have not used those for ages (nor do I use the nested taglib, I use
Struts-EL and JSTL).
 
S

Sudsy

Wendy S wrote:
I'm not aware that 'dot notation' works in the regular Struts tags, but I
have not used those for ages (nor do I use the nested taglib, I use
Struts-EL and JSTL).

It works just fine. As David mentioned, the property name gets mapped to
the appropriate accessor/mutator method name using standard rules.
 
W

Wendy S

Sudsy said:
Wendy S wrote:


It works just fine. As David mentioned, the property name gets mapped to
the appropriate accessor/mutator method name using standard rules.

Then shouldn't what he's doing work just fine? It should call getUser on
the form and getUsername on whatever is returned from getUser.

I've never found a use for this, but it's supposed to be possible.
 
S

Sudsy

Wendy said:
Then shouldn't what he's doing work just fine? It should call getUser on
the form and getUsername on whatever is returned from getUser.

I've never found a use for this, but it's supposed to be possible.

Did you notice that OP said that they had a "User" object in context
but were trying to access via "user.username" and "user.password"?
Turns out that case is very important!
Similary, when creating a text field named "user" (sic) the methods
won't work because of the underlying object class.
IOW it's just a matter of case.
Changing the code to read
<html:text property="User.username"/>
<html:text property="User.password"/>
should solve the problem.
Then again, I wouldn't be surprised to learn that there is something
else going on, something we've not been told. Perhaps the ActionForm
mapping is incorrect or... ?
Your guess is as good as mine. Maybe they just need jsp:useBean.
 
W

Wendy S

Sudsy said:
Did you notice that OP said that they had a "User" object in context
but were trying to access via "user.username" and "user.password"?
Turns out that case is very important!

He said "I have a User object inside the form" and posted the code, an
ActionForm with get/setUser methods, and a User class with get/setUsername.
He has one object of type User nested inside the ActionForm.

I'd just have get/setUsername methods directly in the ActionForm and use
BeanUtils.copyProperties to move the data back and forth between the form
and the user object.
 
S

Sudsy

Wendy said:
He said "I have a User object inside the form" and posted the code, an
ActionForm with get/setUser methods, and a User class with get/setUsername.
He has one object of type User nested inside the ActionForm.

I'd just have get/setUsername methods directly in the ActionForm and use
BeanUtils.copyProperties to move the data back and forth between the form
and the user object.

Right, but he didn't say what name (or id) it was under.
We both know that you don't refer to an object instance by class name.
So what's the instance called?
That's why I was suggesting that the simple inclusion of jsp:useBean
with the appropriate parameters might solve the perceived problem.
You comments vis copyProperties deserve the usual consideration. You
obviously know your stuff!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top