struts2多值传输

阅读更多

一.处理数目不定的字符串

文件创建如图所示

struts2多值传输_第1张图片

hobby.jsp视图代码

爱好: 唱歌 跳舞 睡觉 玩CF

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;
	}


	@Override
	public String execute() throws Exception {
		System.out.println("执行了Action的默认方法");
		if(hobby!=null){
			for(String h:hobby){
				System.out.println(h);
			}
		}
		return SUCCESS;
	}

}

struts.xml





	
  
  	
  		success.jsp
  	
  
  
  

 

二.处理数目不定的javaBean

视图层addstudents.jsp

form action="student" method="post">
	
姓名 性别 年龄

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 + "]";
	}

	
	
}

action

import java.util.List;

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

public class StudentAction implements Action{

	private List students;
        //List接收多个javaBean	
	public List getStudents() {
		return students;
	}

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


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

}

struts.xml





	
  
  
   
  		success.jsp
  	
  

.struts.xml配置详解

struts.xml





	
        
        
        
  
  	
  		${pageContext.request.contextPath}/success.jsp
  	
  
  


  
  
  	
  		${pageContext.request.contextPath}/success.jsp
  	
  
  
  
  

BackStudent 

package com.java1234.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class BackStudent extends ActionSupport{

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

}

ForeStudent 

package com.java1234.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ForeStudent extends ActionSupport{

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

}

 

转载于:https://my.oschina.net/u/3848699/blog/2086085

你可能感兴趣的:(struts2多值传输)