Struts2学习(三) Struts2 处理传入多个值和struts.xml 配置

1,处理数目不定的字符串;
2,处理数目不定的 JavaBean 对象;

1、需求:前台选择多个复选框,复选框选中的值传入到后台;

2、需求:前端批量添加多个Student到后台

HobbyAction.java:

package com.cy.action;

import com.opensymphony.xwork2.Action;

public class HobbyAction implements Action{
    
    private String[] hobby;
    
    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    public String execute() throws Exception {
        System.out.println("执行了HobbyAction的默认方法");
        for(String h : hobby){
            System.out.println(h);
        }
        return SUCCESS;
    }

}

StudentAction.java:

package com.cy.action;

import java.util.List;

import com.cy.model.Student;
import com.opensymphony.xwork2.Action;

public class StudentAction implements Action{
    
    private List<Student> students;
    
    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public String execute() throws Exception {
        System.out.println("执行了StudentAction的默认方法");
        for(Student s : students){
            System.out.println(s);
        }
        return SUCCESS;
    }

}

com.cy.model.Student.java:

package com.cy.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 + "]";
    }

    
    
}

struts.xml配置:

<struts>

    <package name="helloWorld" extends="struts-default">
        <action name="hobby" class="com.cy.action.HobbyAction">
            <result name="success">success.jsp</result>
        </action>
        
        <action name="student" class="com.cy.action.StudentAction">
            <result name="success">success.jsp</result>
        </action>
    </package>

</struts>

hobby.jsp:

<body>
<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>
</body>

addstudents.jsp

<body>
<form action="student" method="post">
    <table>
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
        </tr>
        <tr>
            <!-- students[0].name命名要有规范的,要不然action中取不到 -->
            <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>
</body>

success.jsp:

<body>
    OK!
</body>

测试结果:

1)

 

2)

第二节:struts.xml 配置

一,pageckage 配置
name 包名
extends 继承
namespace 包命名空间
abstract 抽象包
二,action 配置
name action 名
class 处理类
method 方法
三,分模块配置方法
<include file="" ></include>
四,使用通配符

package name:主要用来区分模块的,没有实际作用,就是一个名字而已。

struts.xml配置文件:

<struts>
//package上加上了/fore的namespace,那么访问下面的action,前面url就要加上/fore/xx
    <package name="foreground" namespace="/fore" extends="struts-default">
        <action name="studentList" class="com.cy.action.ForeStudent">
            <result name="success">${pageContext.request.contextPath}/success.jsp</result>
        </action>
    </package>
    
//method:默认执行的是Action里面的execute方法;指定method="show",就执行的是show方法了。
    <package name="background" namespace="/back" extends="struts-default">
        <action name="studentList" class="com.cy.action.BackStudent" method="show">
            <result name="success">${pageContext.request.contextPath}/success.jsp</result>
        </action>
    </package>
    
     //abstract: 默认是false;
    <!-- 抽象包,别人继承它,可以过滤敏感词汇 -->
    <package name="infoFilter" abstract="true"></package>
    
    
</struts>

com.cy.action.ForeStudent.java:

package com.cy.action;


import com.opensymphony.xwork2.ActionSupport;

public class ForeStudent extends ActionSupport {
    public String execute() throws Exception {
        System.out.println("执行了ForeStudent Action的默认方法");
        return SUCCESS;
    }

}

com.cy.action.BackStudent.java:

package com.cy.action;

import com.opensymphony.xwork2.ActionSupport;

public class BackStudent extends ActionSupport{
    public String execute() throws Exception {
        System.out.println("执行了BackStudent Action的默认方法");
        return SUCCESS;
    }
    
    public String show() throws Exception{
        System.out.println("执行了BackStudent show方法");
        return SUCCESS;
    }
}

测试截图:

2)/back/studentList:

console打印: 

执行了BackStudent show方法

猜你喜欢

转载自blog.csdn.net/qq_40135955/article/details/89072046
今日推荐