struts 2は複数の値を処理する

4161 ワード

詳細
1.struts 2は複数の値を入力する処理

    HeadFirstStruts2Chap02_02

1)処理数不定の文字列

HobbyAction.java

package com.andrew.action;
import com.opensymphony.xwork2.ActionSupport;
public class HobbyAction extends ActionSupport {
    private String[] hobby;
    public String[] getHobby() {
        return hobby;
    }
    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }
    @Override
    public String execute() throws Exception {
        System.out.println("   HobbyAction     ");
        if (hobby != null) {
            for (String h : hobby) {
                System.out.println(h);
            }
        }
        return SUCCESS;
    }
}

struts.xml


      success.jsp


hobby.jsp

趣味:
歌を歌う
ダンスを
寝る
CFで遊ぶ.
success.jsp
OK!
http://localhost:8080/HeadFirstStruts2Chap02_02/hobby.jsp
実行結果:
Ok!
コンソール:
ホビーアクションのデフォルトメソッドが実行されました
歌を歌う
ダンスを
寝る
CFで遊ぶ.
2)処理数不定のJavaBeanオブジェクト

Student.java

package com.andrew.model;
public class Student {
    private String name;
    private String sex;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }
}

StudentAction.java

package com.andrew.action;
import java.util.List;
import com.andrew.model.Student;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport {
    private List students;
    public List getStudents() {
        return students;
    }
    public void setStudents(List students) {
        this.students = students;
    }
    @Override
    public String execute() throws Exception {
        System.out.println("   StudentAction     ");
        for (Student s : students) {
            System.out.println(s);
        }
        return SUCCESS;
    }
}

struts.xml


      success.jsp


addstudents.jsp

success.jsp Ok! http://localhost:8080/HeadFirstStruts2Chap02_02/hobby.jsp a 1 11 b 2 12 submit : Ok! : StudentAction Student [name=a, sex=1, age=11] Student [name=b, sex=2, age=12]