<filter>
<filter-name>CharterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
形参为username时可以返回username的参数taotao
接收name时,返回值为null
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick16")
@ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
public void save16(@RequestParam (value = "name") String username ) {
System.out.println(username);
}
}
name参数
username参数
不再测试,随便看下
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
//localhost:8080/user/quick17/taotao
@RequestMapping(value = "/quick17/{username}")
@ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
public void save17(@PathVariable(value = "username") String username ) {
System.out.println(username);
}
@RequestMapping(value = "/quick16")
@ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
public void save16(@RequestParam (value = "name") String username ) {
System.out.println(username);
}
}
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick18")
@ResponseBody
public void save18(Date date) {
System.out.println(date);
}
}
date=2022/1/2
date=2022-1-2
package com.taotao.converter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* create by 刘鸿涛
* 2022/4/19 21:41
*/
@SuppressWarnings({"all"})
public class DateConverter implements Converter<String, Date> {
public Date convert(String dateStr) {
//将日期字符串转换成日期对象 返回
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(dateStr);
}catch (ParseException e){
e.printStackTrace();
}
return date;
}
}
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.taotao.converter.DateConverter">bean>
list>
property>
bean>
<mvc:annotation-driven conversion-service="conversionService">mvc:annotation-driven>
date=2022-1-2
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick19")
@ResponseBody
public void save19(HttpServletRequest req, HttpServletResponse resp, HttpSession session) {
System.out.println(req);
System.out.println(resp);
System.out.println(session);
}
}
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick20")
@ResponseBody
public void save20() {
}
}
package com.taotao.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.ResponseBody;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick20")
@ResponseBody
public void save20(@RequestHeader(value = "User-Agent",required = false) String user_agent) {
System.out.println(user_agent);
}
}
package com.taotao.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.ResponseBody;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick20")
@ResponseBody
public void save20(@CookieValue(value = "JSESSIONID",required = false) String jsessionId) {
System.out.println(jsessionId);
}
}