• SpringMVC之参数绑定


    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>
    

      

  • 相关阅读:
    HUB_mysql学习笔记
    SQL学习笔记
    java_cmd_命令行
    JavaScript_2016_8_28
    linux mysql 安装配置
    solr 添加索引
    solr 查询 实例分析
    solr update接口常用方法
    solr schema.xml文档节点配置
    solr4.5安装配置 linux+tomcat6.0+mmseg4j-1.9.1分词
  • 原文地址:https://www.cnblogs.com/luyuan-chen/p/11678091.html
Copyright © 2020-2023  润新知