springmvc-servlet.xml 与web.xml 配置请参照前面的文章!
Person类:
package com.mth.springmvcmodel;
import java.util.Date;
public class Person {
private String name;
private Integer age;
private String address;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Person(String name, Integer age, String address, Date birthday) {
super();
this.name = name;
this.age = age;
this.address = address;
this.birthday = birthday;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Person [address=" + address + ", age=" + age + ", birthday="
+ birthday + ", name=" + name + "]";
}
}
User类:
package com.mth.springmvcmodel;
public class User {
private String name;
private Integer age;
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + "]";
}
}
TestControler类:
package com.mth.springmvc;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mth.springmvcmodel.Person;
import com.mth.springmvcmodel.User;
/**
* @author 注解controler
*
*/
@RequestMapping("/test")
// controler命名空间或者 唯一标识
@Controller
// 用来标注当前类是springmvc的控制层的类
public class TestControler {
/**
* 方法的返回值代表ModelAndView中的ViewName
*
* @return
*/
@RequestMapping("/hello.do")
// 用来访问控制层方法的注解
public String hello() {
System.out.println("mth=================");
return "index";
}
/**
* HttpServletRequest 可以直接定义在参数列表里面
*
* @param req
* @return
*/
@RequestMapping("/toPerson.do")
public String toPerson(HttpServletRequest req) {
String name = req.getParameter("name");
System.out.println(name);
return "index";
}
/**
* http://localhost:8088/Spring_MVC_2/test/toPerson1.do?name=zs name
* 就是前台传递的name 在参数列表上面直接定义要接受的参数名称 只要这个参数名称一样 就能接收传过来的数据
*
* @return
*/
@RequestMapping("/toPerson1.do")
public String toPerson1(String name) {
System.out.println("toPerson1方法中的name:" + name);
return "index";
}
/**
* http://localhost:8088/Spring_MVC_2/test/toPerson2.do?name=12
*
* 在参数列表上面直接定义要接受的参数名称 只要这个参数名称一样 就能接收传过来的数据 此时可自动转换成参数列表里面的类型
* 注意的是值与类型之间是可以转换的
*
* @return
*/
@RequestMapping("/toPerson2.do")
public String toPerson2(Integer name) {
System.out.println("toPerson1方法中的name:" + name);
return "index";
}
/**
* http://localhost:8088/Spring_MVC_2/test/toPerson3.do?name=zs&age=22&
* address=DL 传递多个参数 姓名 年龄 地址 生日
*
* @return
*/
@RequestMapping("/toPerson3.do")
public String toPerson3(String name, Integer age, String address,
Date birthday) {
System.out.println("toPerson1方法中的name: " + name + "....age: " + age
+ "....address: " + address + "....birthday: " + birthday);
return "index";
}
/**
* 注册时间类型的属性编辑器
*
* @param binder
*/
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd"), true));
}
/**
* http://localhost:8088/Spring_MVC_2/test/
* toPerson4.do?name=zs&age=22&address=DL&birthday=2012-12-21 SpringMVC接受对象
* 传递的参数的名字必须要与实体类里面set方法后面的字符串匹配的上才能接收到参数 首字符的大小写不区分 (通过反射方式调用set方法注入)
* 注意:请求中传的参数和参数列表里面的变量名或者实体里面的set后面的字符串 匹配的上就能接收到
*
* @param p
* @return
*/
@RequestMapping("/toPerson4.do")
public String toPerson4(Person p, User user) {
System.out.println(p);
// Person [address=DL, age=22, birthday=Fri Dec 21 00:00:00 CST 2012,
// name=zs]
System.out.println(user);
// User [age=22, name=zs]
return "index";
}
/**
* http://localhost:8088/Spring_MVC_2/test/toPerson5.do?name=zs&name=ww&name
* =ls 比如checkbox多选的时候接受的就是数组(爱好)
*
* @return
*/
@RequestMapping("/toPerson5.do")
public String toPerson5(String[] name) {
for (String n : name) {
System.out.println(n);
}
return "index";
}
}