码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC之传值

时间:2020-12-01 12:15:03      阅读:4      评论:0      收藏:0      [点我收藏+]

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

一、jsp中:

<form action="SpringMVCController/testparam" method="get">
		name:<input name="uname" type="text"><br/>
		age: <input name="uage" type="text"><br/>
		<input type="submit" value="查">
	</form>

  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中:

<form action="SpringMVCController/testObjectProperties" method="get">
		id:<input name="id" type="text"><br/>
		name:<input name="name" type="text"><br/>
		age: <input name="age" type="text"><br/>
		homeAddress: <input name="address.homeAddress" type="text"><br/>
		schoolAddress: <input name="address.schoolAddress" type="text"><br/>
		<input type="submit" value="确定">
	</form>

  允许级联属性。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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!