1  首先写两个javabean对象  person 和 address 代码如下。两个类之间的关系如代码中

springmvc使用javabean作为请求参数-LMLPHP
package cn.bean.demo.bo;

public class Person {

</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String name;</br>

</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String number;</br>

</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String sex;</br>

</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String age;</br>

</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Address address;</br>

</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getName() {</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> name;</br>
}</br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setName(String name) {</br>
</span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name;</br>
}</br></br> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getNumber() {</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> number;</br>
}</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setNumber(String number) {</br>
</span><span style="color: #0000ff;">this</span>.number =<span style="color: #000000;"> number;</br>
}</br></br> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getSex() {</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> sex;</br>
}</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setSex(String sex) {</br>
</span><span style="color: #0000ff;">this</span>.sex =<span style="color: #000000;"> sex;</br>
}</br></br> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getAge() {</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> age;</br>
}</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setAge(String age) {</br>
</span><span style="color: #0000ff;">this</span>.age =<span style="color: #000000;"> age;</br>
}</br></br> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Address getAddress() {</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> address;</br>
}</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setAddress(Address address) {</br>
</span><span style="color: #0000ff;">this</span>.address =<span style="color: #000000;"> address;</br>
}</br></br> @Override</br>
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String toString() {</br>
</span><span style="color: #0000ff;">return</span> "Person [name=" + name + ", number=" + number + ", sex=" + sex + ", age=" + age + ", address=" +<span style="color: #000000;"> address
</span>+ "]"<span style="color: #000000;">;</br>
}</br></br>

}

springmvc使用javabean作为请求参数-LMLPHP
springmvc使用javabean作为请求参数-LMLPHP
package cn.bean.demo.bo;

public class Address {
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String city;</br></br>

</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String province;</br></br></br>

</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getCity() {</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> city;</br>
}</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setCity(String city) {</br>
</span><span style="color: #0000ff;">this</span>.city =<span style="color: #000000;"> city;</br>
}</br></br> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getProvince() {</br>
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> province;</br>
}</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setProvince(String province) {</br>
</span><span style="color: #0000ff;">this</span>.province =<span style="color: #000000;"> province;</br>
}</br> @Override
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String toString() {</br>
</span><span style="color: #0000ff;">return</span> "Address [city=" + city + ", province=" + province + "]"<span style="color: #000000;">;</br>
}</br></br>

}

springmvc使用javabean作为请求参数-LMLPHP

2   编写控制器 使用person来作为请求参数

springmvc使用javabean作为请求参数-LMLPHP
package cn.bean.demo.service;

import org.springframework.stereotype.Controller;


import org.springframework.web.bind.annotation.RequestMapping;


import org.springframework.web.bind.annotation.RequestMethod;

import cn.bean.demo.bo.Person;

@Controller


@RequestMapping("/PojoService")


public class PojoService {


public final static String SUCCEEDD="show";
@RequestMapping(value</span>="/testPojo",method=<span style="color: #000000;">RequestMethod.POST)</br>
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String testPojo(Person person){</br> System.out.println(</span>"testPojo person :"+<span style="color: #000000;">person);</br></br> System.out.println(person.getAddress());</br></br> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> SUCCEEDD;</br>
}</br>

}

springmvc使用javabean作为请求参数-LMLPHP

3   写测试类:可以用httpclient来模拟。 我这使用表单进行测试的。 表单代码

springmvc使用javabean作为请求参数-LMLPHP
<%@ 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="PojoService/testPojo" method="post">
  username:</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="name"</span><span style="color: #0000ff;">/&gt;</span></br>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">br</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"></br>
number: </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="number"</span><span style="color: #0000ff;">/&gt;</span></br>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">br</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"></br>
sex: </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="sex"</span><span style="color: #0000ff;">/&gt;</span></br>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">br</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"></br>
age: </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="age"</span><span style="color: #0000ff;">/&gt;</span></br>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">br</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"></br>
province: </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="address.province"</span><span style="color: #0000ff;">/&gt;</span></br>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">br</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"></br></br> city: </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="address.city"</span><span style="color: #0000ff;">/&gt;</span></br>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">br</span><span style="color: #0000ff;">&gt;</span></br></br> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="subit"</span><span style="color: #0000ff;">/&gt;</span></br>

</form>

</body>


</html>

springmvc使用javabean作为请求参数-LMLPHP

测试:

点击提交:为了方便看到控制器中的对象 ,进行断点。

表单中的属性都已经被填充到person对象中

继续向下 看到address对象

已经都被赋值成功

05-11 22:19