参数问题
java
package com.hwq.controller;
import com.hwq.beans.User;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.sql.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;
@Controller
public class ParamController {
@InitBinder
public void initData(WebDataBinder web){
//将日期格式转化成yyyy-MM-dd
web.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
/*
方式一:传统request接收参数
param1?id=1
get中文乱码问题:
调整Tomcat服务器server.xml配置
post中文乱码(偏码过滤器)
在web.xml中配置过滤器
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*/
@RequestMapping("/param1")
public String Param1(HttpServletRequest request){
String name = request.getParameter("name");
System.out.println("name="+name);
return "index";
}
/*
方式二:直接使用参数接收参数
param2?id=12
*/
@RequestMapping("/param2")
public String Param1(String name){
System.out.println("id="+name);
return "index";
}
/*
方式三:使用数组接收参数 int integer[]
param3?hobby=睡觉&hobby=打豆豆
*/
@RequestMapping("/param3")
public String Param3(String[] hobby){
System.out.println("likes="+ Arrays.toString(hobby));
return "index";
}
/*
方式四:使用集合接收参数
param4?hobby=睡觉&hobby=打豆豆
*/
@RequestMapping("/param4")
public String Param4(@RequestParam("hobby") List<String> hobby){
System.out.println("likes="+ hobby);
return "index";
}
/*
方式五:日期 接收参数
方式一:字符串接收
param5?data=2019-09-09
方式二:形式参数直接接收,这种方式对日期格式有严格要求yyyy/MM/dd
param51?data=2019/09/09
方式三:使用日期初始化绑定
@InitBinder
public void initData(WebDataBinder web){
//将日期格式转化成yyyy-MM-dd
web.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
*/
@RequestMapping("/param5")
public String Param5(String data){
System.out.println("data="+ data);
//将字符串转化成日期
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date parse = simpleDateFormat.parse(data);
System.out.println("parse="+parse);
} catch (ParseException e) {
e.printStackTrace();
}
return "index";
}
@RequestMapping("/param51")
public String Param51(Date data){
System.out.println("data="+ data);
//将字符串转化成日期
return "index";
}
@RequestMapping("/param52")
public String Param52(Date data){
System.out.println("data="+ data);
//将字符串转化成日期
return "index";
}
/*
方式六:RestFull风格传参
*/
@RequestMapping("/param6/{id}/{name}")
public String Param6(@PathVariable("id") Integer id, @PathVariable("name") String name){
System.out.println("id="+id+"name="+name);
return "index";
}
/*
方式七:对象传参
*/
@RequestMapping("/param7")
public String Param7(User user){
System.out.println("user="+user);
System.out.println(user.getGroup());
return "index";
}
}
方式一:传统request接收参数
方式二:直接使用参数接收参数
创建一个add.jsp
方式三:使用数组接收参数
方式四:使用集合接收参数
方式五:日期 接收参数
java
/*
方式五:日期 接收参数
方式一:字符串接收
param5?data=2019-09-09
方式二:形式参数直接接收,这种方式对日期格式有严格要求yyyy/MM/dd
param51?data=2019/09/09
方式三:使用日期初始化绑定
@InitBinder
public void initData(WebDataBinder web){
//将日期格式转化成yyyy-MM-dd
web.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
*/
@RequestMapping("/param5")
public String Param5(String data){
System.out.println("data="+ data);
//将字符串转化成日期
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date parse = simpleDateFormat.parse(data);
System.out.println("parse="+parse);
} catch (ParseException e) {
e.printStackTrace();
}
return "index";
}
@RequestMapping("/param51")
public String Param51(Date data){
System.out.println("data="+ data);
//将字符串转化成日期
return "index";
}
@RequestMapping("/param52")
public String Param52(Date data){
System.out.println("data="+ data);
//将字符串转化成日期
return "index";
}
方式六:RestFull风格传参
创建一个User类
java
package com.hwq.beans;
import java.util.Date;
import java.util.List;
public class User {
private int id;
private String username;
private String password;
private int age;
private List<String> hobby;
private Date data;
private Group group;
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public User() {
}
public User(int id, String username, String password, int age, List<String> hobby, Date data) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
this.hobby = hobby;
this.data = data;
}
public int getId() {
return id;
}
public void setId(int 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public Date getData() {
return data;
}
public void setData(Date data) {
this.data = data;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", hobby=" + hobby +
", data=" + data +
'}';
}
}