1

I have a actionform class:

public class NameForm extends ActionForm {

private String firstName;
private String lastName;

public void setLastName(String lName) {
    lastName = lName;
}

public String getLastName() {
    return lastName;
}

 public void setFirstName(String fName) {
    firstName = fName;
}

public String getFirsttName() {
    return firstName;
}   
}

and I have another class that contains other getters/setters that I would like to use in my action form it is:

public class sports {

private String sport;
private String team;
private String position;

public void setSport(String sp) {
    sport = sp;
}

public String getSport() {
    return sport;
}

public void setTeam(String tm) {
    team = tm;
}

public String getTeam() {
    return team;
}

public void setPosition(String po) {
    position = po;
}

public String getPosition() {
    return position;
}

} How can I get the values contained in the getters for the sports class into the actionform without creating another actionform? I am trying to use beans to populate my jsp from my action form.

billy
  • 157
  • 1
  • 5
  • 17

1 Answers1

1

To do this you can create another attribute in your NameForm that is of type Sports.

private Sports sports = new Sports();

public void setSports(Sports s){ this.sports = s; }
public Sports getSports(){ return this.sports; }

Then in your JSP you can access it using assuming you're using something like OGNL.

%{#attr.sports.team}
%{#attr.sports.position}
%{#attr.sports.sport}
bsimic
  • 926
  • 7
  • 15
  • why do you only have to create an attribute for sports in the NameForm and not one for sport, team, position? I am not familiar with OGNL how would %{#attr.sports.team} be written in scriplet code would it be <%= request.getAttribute("team") %>? or how would it be written using a bean? – billy Mar 15 '12 at 23:06
  • You can rewrite this in a scriptlet such as: <% Sports sports = (Sports) request.getAttribute("sports"); %> then you can do something like sports.getTeam() in your scriptlet later on. – bsimic Mar 15 '12 at 23:57
  • How would I use OGNL to get an attribute in a class that was being extended from Sports? For example if I had public class sports extends activites. How would I use OGNL to get to an object inside of activites? – billy Sep 30 '12 at 03:34
  • If the class "Sports" extends "Activities" and the Activities class has public getters for it's attributes, it would work the same way. Meaning that you should be able to access the attributes from the parent class via the child class if it has public accessors. – bsimic Oct 23 '12 at 19:34