获取参数值@RequestParam
@RequestParam表示会接收参数,value="name"表示接收的参数名为name,,required = false表示该参数可以没有,默认为true
package com.ysbt.requestparam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/vote")
public class RequestParam {
@RequestMapping("/vote01")
public String vote(@org.springframework.web.bind.annotation.RequestParam(value = "name",required = false) String username){
System.out.println("username="+username);
return "success";
}
}
获取请求头
@RequestHeader
package com.ysbt.requestparam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/header")
public class RequestHeader {
@RequestMapping("/vote01")
public String header(@org.springframework.web.bind.annotation.RequestHeader("Accept-Encoding")String ae,@org.springframework.web.bind.annotation.RequestHeader("Host") String host){
System.out.println("ae="+ae);
System.out.println("host="+host);
return "success";
}
}
获取对象javabean对象
表单控件名name必须要与对象字段对应,否则为null
后端
package com.ysbt.requestparam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/bean")
public class JavaBean {
@RequestMapping("/master")
public String javaBean(Master master){
System.out.println(master);
return "success";
}
前端
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="bean/master" method="post">
id: <input type="text" name="id"><br>
name: <input type="text" name="name"><br>
petname <input type="text" name="pet.name"><br>
petage <input type="text" name="pet.age"><br>
<input type="submit">
</form>
</body>
</html>
获取 servlet api
package com.ysbt.requestparam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/servlet")
public class ServletApi {
@RequestMapping("/api")
public String servletApi(HttpServletRequest request){
String acceptencoding =request.getHeader("Accept-Encoding");
System.out.println(acceptencoding);
return "success";
}
}
标签:请求,映射,springMVC,springframework,String,Controller,import,org,RequestMapping
From: https://www.cnblogs.com/yousuobutong/p/18079245