Struts2核心知识(下)

2020-07-14 16:50:59 浏览数 (1)

1,Struts2处理传入多值;

1.1,处理数目不定的字符串时;

这里我们采用的是字符串类型来接受;

hobby.jsp页面:

代码语言:javascript复制
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<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>
</html>

struts.xml:

代码语言:javascript复制
<action name="hobby" class="com.java.action.HobbyAction">
           <result name="success">success.jsp</result>
           <result name="error">error.jsp</result>
       </action>

处理类HobbyAction:

代码语言:javascript复制
package com.java.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("默认执行了User方法");
        for(String h:hobby){
            System.out.println(h);
        }
        return SUCCESS;
    }
}

运行之后,会发现后台可以获取!

点击提交之后,后台可以获取:

1.2,处理数目不定的javabean对象时,

这里采用对象列表的形式来接受数据;

student.jsp:

代码语言:javascript复制
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<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>
</body>
</html>

struts.xml:

代码语言:javascript复制
<action name="student" class="com.java.action.StudentAction">
           <result name="success">success.jsp</result>
           <result name="error">error.jsp</result>
       </action>

处理类StudentAction:

代码语言:javascript复制
package com.java.action;

import com.java.model.Student;
import com.opensymphony.xwork2.ActionSupport;

import java.util.List;

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("默认执行了User方法");
        for(Student s:students){
            System.out.println(s);
        }
        return SUCCESS;
    }
}

javabean对象:

代码语言:javascript复制
package com.java.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   "]";
  }
  
  
}

运行界面:

2,Struts.xml配置文件;

2.1,package配置;

name:包名 extends:继承 namespaces:包命名空间 abstract:抽象包

这里的namespaces是指我们在输入请求时,可以多加一层访问请求路径;

先写两个处理类:

ForeStudent:

代码语言:javascript复制
package com.java.action;

import com.opensymphony.xwork2.ActionSupport;

public class ForeStudent extends ActionSupport {

    @Override
    public String execute() throws Exception {
        System.out.println("默认执行了fore方法");
        return SUCCESS;
    }
}

BackStudent:

代码语言:javascript复制
package com.java.action;

import com.opensymphony.xwork2.ActionSupport;

public class BackStudent extends ActionSupport {

    @Override
    public String execute() throws Exception {
        System.out.println("默认执行了back方法");
        return SUCCESS;
    }
}

struts.xml配置:

代码语言:javascript复制
<package name="foreground" namespace="/fore" extends="struts-default">
        <action name="studentList"  class="com.java.action.ForeStudent">
            <result name="success">${pageContext.request.contextPath}/success.jsp</result>
            <result name="error">error.jsp</result>
        </action>
    </package>

    <package name="background" namespace="/back" extends="struts-default">
        <action name="studentList"  class="com.java.action.BackStudent">
            <result name="success">${pageContext.request.contextPath}/success.jsp</result>
            <result name="error">error.jsp</result>
        </action>
    </package>

运行结果:

我们需要多加一个/fore路径

2.2,action配置;

name:action名 class:处理类 method:方法

代码语言:javascript复制
<action name="studentList"  class="com.java.action.ForeStudent" method="show">
            <result name="success">${pageContext.request.contextPath}/success.jsp</result>
            <result name="error">error.jsp</result>
        </action>

struts2默认给我们执行的是execute()方法,而当我们想执行掐方法时,就可以用method,其中属性写上我们想要调用的方法就可以了!

0 人点赞