1.Controller
package com.tz.controller; import org.springframework.beans.factory.annotation.Required; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.tz.domain.Course; @Controller public class RequestController { /** * @PathVariable("user"):获取路径中的值/handler/{user} * @RequestParam("user"):获取请求参数/hander/{user}?user=coco * * @param userName * @return */ @RequestMapping("/handle") public String handler(@RequestParam(value="user",required=false) String userName){ //userName = request.getParameter("user"); System.out.println(userName); return "success"; } @RequestMapping("/handle2") public String handler02(@RequestParam(value="user",required=false,defaultValue="你没有带参数") String userName, @RequestHeader(value="User-Agent",required=false) String user_Agent, @CookieValue("JSESSIONID") String js){ //userName = request.getParameter("user"); System.out.println(userName); System.out.println(user_Agent); System.out.println(js); return "success"; } @RequestMapping("/addCourse") public String addCourse(Course c){//springMVC会自动为我们定义的类型进行赋值 /** * 1.将对象中的每一个属性从request参数中尝试取出来,并进行封装 * request.getParameter("参数名");如果有值,就会取出来封装到实体类的属性中(调用set方法) */ System.out.println(c); return "success"; } }
2.domain
package com.tz.domain; import java.util.List; import java.util.Map; public class Course { private String cname; private String teacher; private Integer price; private Integer stock; private Integer number; private Address address; private List<String> clist;//课程内容 private Map<String,String> cmap;//key:day01,value:内容 public List<String> getClist() { return clist; } public void setClist(List<String> clist) { this.clist = clist; } public Map<String, String> getCmap() { return cmap; } public void setCmap(Map<String, String> cmap) { this.cmap = cmap; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public Integer getStock() { return stock; } public void setStock(Integer stock) { this.stock = stock; } public Integer getNumber() { return number; } public void setNumber(Integer number) { this.number = number; } @Override public String toString() { return "Course [cname=" + cname + ", teacher=" + teacher + ", price=" + price + ", stock=" + stock + ", number=" + number + ", address=" + address + ", clist=" + clist + ", cmap=" + cmap + "]"; } } package com.tz.domain; public class Address { private String province; private String city; private String street; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } @Override public String toString() { return "Address [province=" + province + ", city=" + city + ", street=" + street + "]"; } }
3.jsp页面
<%@ 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> <a href="handle2?user=coco">访问</a><br/> <form action="addCourse" method="post"> 课程名称: <input type="text" name="cname"><br/> 主讲老师: <input type="text" name="teacher"><br/> 课程价格: <input type="text" name="price"><br/> 剩余名额: <input type="text" name="stock"><br/> 报名人数: <input type="text" name="number"><br/> <hr/> 老师的居住地址: <br/> 省份: <input type="text" name="address.province"><br/> 城市: <input type="text" name="address.city"><br/> 街道: <input type="text" name="address.street"> <hr/> 课程内容list:<br/> <input type="text" name="clist[0]"> <input type="text" name="clist[1]"> <input type="text" name="clist[2]"> <br/> 课程内容:<br/> <input type="text" name="cmap['day01']"> <input type="text" name="cmap['day02']"> <input type="text" name="cmap['day03']"> <input type="submit" value="提交"> </form> </body> </html>