创建项目:
导包:
HelloController.java类:
@Controller
public class HelloController {
@RequestMapping("fun1")
@ResponseBody
public void fun1(Integer id,String name){
System.out.println(id+"--我是fun1--"+name);
}
/**
* 接收数据的基本类型
* @param id
* @param name
*/
@RequestMapping("fun2")
@ResponseBody
public void fun2(@RequestParam(name="ids") Integer id,String name){
System.out.println(id+"--我是fun2--"+name);
}
/**
* 接收基本类型的参数
* @param id
* @param name
*/
@RequestMapping("fun3")
@ResponseBody
public void fun3(Integer id,String name){
System.out.println(id+"--我是fun3--"+name);
}
/**
* 接收基本类型的参数
* @param id
* @param name
*/
@RequestMapping("fun4")
@ResponseBody
public void fun4(@RequestParam(name="ids") Integer id,@RequestParam(name="name",
required=true)String name){
System.out.println(id+"--我是fun4--"+name);
}
UserController.java类:
@Controller
public class UserController {
/**
* user接收类
* @param user
*/
@RequestMapping("/add")
@ResponseBody
public void insertUser(User user){
System.out.println(user+"---"+user.getBirth());
}
/**
* 数组接收
* @param id
* @param name
* @param favorites
*/
@RequestMapping("/add1")
@ResponseBody
public void insertUser1(Integer id,String name,String[]favorites){
for (String f : favorites) {
System.out.println(f);
}
}
/**
* 集合接收
* @param id
* @param name
* @param favorites
*/
@RequestMapping("/add2")
@ResponseBody
public void insertUser2(Integer id,String name,List<String>favorites){
for (String f : favorites) {
System.out.println(f);
}
}
/**
*
* @param id
* @param name
* @param favorites
*/
@RequestMapping("/add3")
@ResponseBody
public void insertUser3(User user){
System.out.println(user);
System.out.println(user.getList());
}
/**
*
* @param id
* @param name
* @param favorites
*/
@RequestMapping("/add4")
@ResponseBody
public void insertUser4(User user){
String [] favorites=user.getFavorites();
for (String f : favorites) {
System.out.println(f);
}
}
StringToDateConvert.java类:(日期转换)
public class StringToDateConvert implements Converter<String, Date> {
@Override
public Date convert(String msg) {
SimpleDateFormat adf=new SimpleDateFormat("yyyy-MM-dd");
try {
return adf.parse(msg);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
Book类:
public class Book {
private String bookid;
private String bookname;
private String author;
public String getBookid() {
return bookid;
}
public void setBookid(String bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [bookid=" + bookid + ", bookname=" + bookname + ", author=" + author + "]";
}
User类:
private Integer id;
private String name;
private String[] favorites;
private Data birth;
private List<String> list;
private String address;
private Book book;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getFavorites() {
return favorites;
}
public void setFavorites(String[] favorites) {
this.favorites = favorites;
}
public Data getBirth() {
return birth;
}
public void setBirth(Data birth) {
this.birth = birth;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", favorites=" + Arrays.toString(favorites) + ", birth=" + birth
+ ", list=" + list + ", address=" + address + ", book=" + book + "]";
}
Spring-mvc.xml配置文件类:(这里也配置了视图解析器)如在输入是有报异常将视图解析器注释掉即可!
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.ytl.controller"></context:component-scan>
<!-- 开启注解 -->
<mvc:annotation-driven conversion-service="formattingConversionServiceFactoryBean"></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/"/>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 设置自定义的转换器 -->
<bean id="formattingConversionServiceFactoryBean"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.ytl.converter.StringToDateConvert"></bean>
</set>
</property>
</bean>
</beans>