struts2处理传入多个值

阅读更多
1. struts2处理传入多个值

新建项目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! 控制台: 执行了HobbyAction的默认方法 唱歌 跳舞 睡觉 玩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]

你可能感兴趣的:(Java,struts2)