Search code examples
javajspstrutsgetter-setteractionform

use java to get getters/setters from one class into an actionform


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.


Solution

  • 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}