欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2处理传入多个值

程序员文章站 2022-07-12 16:13:42
...
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

<action name="hobby" class="com.andrew.action.HobbyAction">
      <result name="success">success.jsp</result>
</action>

hobby.jsp

<form action="hobby" method="post">
    爱好:
    <input type="checkbox" name="hobby" value="唱歌"/>唱歌
    <input type="checkbox" name="hobby" value="跳舞"/>跳舞
    <input type="checkbox" name="hobby" value="睡觉"/>睡觉
    <input type="checkbox" name="hobby" value="玩CF"/>玩CF
    <input type="submit" value="提交"/>
</form>

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<Student> students;
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> 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

<action name="student" class="com.andrew.action.StudentAction">
      <result name="success">success.jsp</result>
</action>

addstudents.jsp

<form action="student" method="post">
    <table>
        <tr><th>姓名</th><th>性别</th><th>年龄</th></tr>
        <tr>
            <td><input type="text" name="students[0].name"/></td>
            <td><input type="text" name="students[0].sex"/></td>
            <td><input type="text" name="students[0].age"/></td>
        </tr>
        <tr>
            <td><input type="text" name="students[1].name"/></td>
            <td><input type="text" name="students[1].sex"/></td>
            <td><input type="text" name="students[1].age"/></td>
        </tr>
        <tr><td colspan="3"><input type="submit" value="提交"/></td></tr>
    </table>
</form>

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