做网站视频学什么专业,做网站的字体,东莞网站建设 环保设备,正版网络推广公司目录
通过ServletAPI获取#xff08;不常用#xff09;
通过控制器方法的形参获取请求参数
RequestParam
RequestHeader
CookieValue
通过POJO获取请求参数
解决获取请求参数的乱码问题 test_param.html#xff1a;
!DOCTYPE html
html langen不常用
通过控制器方法的形参获取请求参数
RequestParam
RequestHeader
CookieValue
通过POJO获取请求参数
解决获取请求参数的乱码问题 test_param.html
!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8title测试请求参数/title
/head
body
h1测试请求参数/h1
/body
/html
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;Controller
public class TestController {RequestMapping(/param)public String param(){return test_param;}}启动 通过ServletAPI获取不常用
将HttpServletRequest作为控制器方法的形参此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;Controller
public class ParamController {RequestMapping(/testServletAPI)//形参位置的request表示当前请求public String testServletAPI(HttpServletRequest request){String username request.getParameter(username);String password request.getParameter(password);System.out.println(usernameusernamepasswordpassword);return success;}}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8title测试请求参数/title
/head
body
h1测试请求参数/h1
a th:href{/testServletAPI(usernameadmin,password123456)}测试使用ServletAPI获取请求参数/a
/body
/html 通过控制器方法的形参获取请求参数
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;Controller
public class ParamController {RequestMapping(/testParam)public String testParam(String username,String password){System.out.println(usernameusernamepasswordpassword);return success;}
}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8title测试请求参数/title
/head
body
h1测试请求参数/h1
a th:href{/testParam(usernameadmin,password123456)}测试使用控制器的形参获取请求参数/a
/body
/html 注
若请求所传输的请求参数中有多个同名的请求参数此时可以在控制器方法的形参中设置字符串数组或者字符串类型的形参接收此请求参数
若使用字符串数组类型的形参此参数的数组中包含了每一个数据若使用字符串类型的形参此参数的值为每个数据中间使用逗号拼接的结果
字符串数组
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;Controller
public class ParamController {RequestMapping(/testParam)public String testParam(String username,String password,String[] hobby){System.out.println(usernameusernamepasswordpasswordhobby Arrays.toString(hobby));return success;}
}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8title测试请求参数/title
/head
body
h1测试请求参数/h1
form th:action{/testParam} methodpost用户名input typetext nameusernamebr密码input typepassword namepasswordbr爱好input typecheckbox namehobby valueaainput typecheckbox namehobby valuebbinput typecheckbox namehobby valueccbrinput typesubmit value测试使用控制器的形参获取请求参数
/form
/body
/html 字符串
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;Controller
public class ParamController {RequestMapping(/testParam)public String testParam(String username,String password,String hobby){System.out.println(usernameusernamepasswordpasswordhobbyhobby);return success;}
}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8title测试请求参数/title
/head
body
h1测试请求参数/h1
form th:action{/testParam} methodpost用户名input typetext nameusernamebr密码input typepassword namepasswordbr爱好input typecheckbox namehobby valueaainput typecheckbox namehobby valuebbinput typecheckbox namehobby valueccbrinput typesubmit value测试使用控制器的形参获取请求参数
/form
/body
/html RequestParam
RequestParam是将请求参数和控制器方法的形参创建映射关系
RequestParam注解一共有三个属性
value指定为形参赋值的请求参数的参数名
required设置是否必须传输此请求参数默认值为true
若设置为true时则当前请求必须传输value所指定的请求参数若没有传输该请求参数且没有设置defaultValue属性则页面报错400Required String parameter xxx is not present若设置为false则当前请求不是必须传输value所指定的请求参数若没有传输则注解所标识的形参的值为null
defaultValue不管required属性值为true或false当value所指定的请求参数没有传输或传输的值为时则使用默认值为形参赋值
!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8title测试请求参数/title
/head
body
h1测试请求参数/h1
form th:action{/testParam} methodpost用户名input typetext nameuser_namebr密码input typepassword namepasswordbr爱好input typecheckbox namehobby valueaainput typecheckbox namehobby valuebbinput typecheckbox namehobby valueccbrinput typesubmit value测试使用控制器的形参获取请求参数
/form
/body
/html
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;Controller
public class ParamController {RequestMapping(/testParam)public String testParam(RequestParam(user_name) String username, String password, String[] hobby){System.out.println(usernameusernamepasswordpasswordhobby Arrays.toString(hobby));return success;}
}RequestHeader
RequestHeader是将请求头信息和控制器方法的形参创建映射关系
RequestHeader注解一共有三个属性value、required、defaultValue用法同RequestParam
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;Controller
public class ParamController {RequestMapping(/testParam)public String testParam(RequestParam(user_name) String username, String password, String[] hobby,RequestHeader(Host) String host){System.out.println(usernameusernamepasswordpasswordhobby Arrays.toString(hobby));System.out.println(host:host);return success;}
}CookieValue
CookieValue是将cookie数据和控制器方法的形参创建映射关系
CookieValue注解一共有三个属性value、required、defaultValue用法同RequestParam
创建session package com.qcby.mvc.controller;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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;Controller
public class ParamController {RequestMapping(/testServletAPI)//形参位置的request表示当前请求public String testServletAPI(HttpServletRequest request){HttpSession session request.getSession();String username request.getParameter(username);String password request.getParameter(password);System.out.println(usernameusernamepasswordpassword);return success;}RequestMapping(/testParam)public String testParam(RequestParam(user_name) String username, String password, String[] hobby,RequestHeader(Host) String host,CookieValue(JSESSIONID) String JSESSIONID){System.out.println(usernameusernamepasswordpasswordhobby Arrays.toString(hobby));System.out.println(host:host);System.out.println(JSESSIONID:JSESSIONID);return success;}
}通过POJO获取请求参数
可以在控制器方法的形参位置设置一个实体类类型的形参此时若浏览器传输的请求参数的参数名和实体类中的属性名一致那么请求参数就会为此属性赋值
package com.qcby.mvc.bean;public class User {private Integer id;private String username;private String password;private Integer age;private String sex;private String email;public User() {}public User(Integer id, String username, String password, Integer age, String sex, String email) {this.id id;this.username username;this.password password;this.age age;this.sex sex;this.email email;}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex sex;}public String getEmail() {return email;}public void setEmail(String email) {this.email email;}Overridepublic String toString() {return User{ id id , username username \ , password password \ , age age , sex sex \ , email email \ };}
}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8title测试请求参数/title
/head
body
h1测试请求参数/h1
form th:action{/testpojo} methodpost用户名input typetext nameusernamebr密码input typepassword namepasswordbr性别input typeradio namesex value男男input typeradio namesex value女女br年龄input typetext nameagebr邮箱input typetext nameemailbrinput typesubmit value使用实体类接收请求参数
/form
/body
/html
package com.qcby.mvc.controller;import com.qcby.mvc.bean.User;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;Controller
public class ParamController {RequestMapping(/testpojo)public String testpojo(User user){System.out.println(user);return success;}
}解决获取请求参数的乱码问题
解决获取请求参数的乱码问题可以使用SpringMVC提供的编码过滤器CharacterEncodingFilter但是必须在web.xml中进行注册
!--配置springMVC的编码过滤器--
filterfilter-nameCharacterEncodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-paraminit-paramparam-nameforceResponseEncoding/param-nameparam-valuetrue/param-value/init-param
/filter
filter-mappingfilter-nameCharacterEncodingFilter/filter-nameurl-pattern/*/url-pattern
/filter-mapping
注
SpringMVC中处理编码的过滤器一定要配置到其他过滤器之前否则无效