SpringMVC之传值

2021-03-14 01:28

阅读:433

标签:vat   prope   action   需要   输入   相同   map   new   接受   

一、jsp中:


name:
age:

  controller中:

@RequestMapping(value="testparam")
	public String welcome9(@RequestParam("uname") String name,@RequestParam(value="uage",required=false,defaultValue="23") Integer age){
		System.out.println(name);
		System.out.println(age);
		return "success";
	}

  required=false意思是该值可以不输入。

二、接受对象(Student student):

原始servlet接受jsp中的数据,然后Student student = new Student();

student.setId=...太麻烦。

创建实体类Student和Address。

Student.java:

package org.ruangong.entity;

public class Student {
	private int id;
	private String name;
	private int age;
	private Address address;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
}

  Address.java:

package org.ruangong.entity;

public class Address {
	private String homeAddress;
	private String schoolAddress;
	public String getHomeAddress() {
		return homeAddress;
	}
	public void setHomeAddress(String homeAddress) {
		this.homeAddress = homeAddress;
	}
	public String getSchoolAddress() {
		return schoolAddress;
	}
	public void setSchoolAddress(String schoolAddress) {
		this.schoolAddress = schoolAddress;
	}
}

  controller.java:

@RequestMapping(value="testObjectProperties")
	public String testObjectProperties(Student student){
		System.out.println(student.getId()+","+student.getName()+","+student.getAge()+","+student.getAddress().getHomeAddress()+","+student.getAddress().getSchoolAddress());
		return "success";
	}

  jsp中:


id:
name:
age:
homeAddress:
schoolAddress:

  允许级联属性。input标签中的name值必须跟实体类属性值相同。

 

三、在controller中任然可以使用原始servlet中的东西:

只需要:

@RequestMapping(value="testServletAPI")
	public String testServletAPI(HttpServletRequest request){
		String name = request.getParameter("name");
		System.out.println(name);
		return "success";
	}

  

SpringMVC之传值

标签:vat   prope   action   需要   输入   相同   map   new   接受   

原文地址:https://www.cnblogs.com/jccjcc/p/14043524.html


评论


亲,登录后才可以留言!