一:springMvc注解参数的接收
1:类上加注解:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class myController {
@RequestMapping("/he.do")
public String he(){
System.out.println("已经运行了.....");
return "hello";
}
}
访问路径加/test
2:参数列表接收:
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class myController {
@RequestMapping("/he.do")
public String he(){
System.out.println("已经运行了.....");
return "hello";
}
@RequestMapping("/getName.do")
public String getName(HttpServletRequest request){
String name= request.getParameter("name");
System.out.println(name);
return "hello";
}
}
输入地址;http://localhost:8080/springMvcTest/getName.do?name=zhangsan
传递参数
多个参数的传递,springMvc会自动转换数据类型;
springMvc自动数据类型转换失败,会报400错误。
下面是自定义日期转换;
@Controller
public class myController {
@RequestMapping("/getName1.do")
public String getName1(String name,String addr,Integer age, Date birthday){
System.out.println("name"+name+"addr="+addr+"age="+age+"birthday="+birthday);
return "hello";
}
//自定义日期转换
@InitBinder
public void initBinder(ServletRequestDataBinder binder){
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-mm-dd"), true));
}
}
实体对象参数接收:
首先创建person 类,私有的name,addr,birthday,id,提供getset方法。
controller中:
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;
@Controller
public class myController {
@RequestMapping("/getParamByObj.do")
public String getParamByObj(Person person){
System.out.println(person);
return "index";
}
}
输入地址,接收的值为null.
3:从后台接受到的数据,往前台传递显示
controller:
import java.text.ParseException;
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.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class myController {
@RequestMapping("/returnData.do")
public String returnData(Model model) throws Exception{
Person p= new Person();
p.setId(2);
p.setName("王五");
p.setAddr("上海");
p.setBirthday(new SimpleDateFormat("yyyy-mm-dd").parse("2013-08-08"));
model.addAttribute("person",p);
return "index";
}
}
jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>test index</title>
</head>
<body>
<h1>${person.id}</h1>
<h1>${person.name}</h1>
<h1>${person.addr}</h1>
<h1>${person.birthday}</h1>
</body>
</html>
通过EL表达式,将数据传递到前台显示。