@RequestMapping(value = "/quick11")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save11(String userName,int age) throws IOException {
System.out.println(userName);
System.out.println(age);
//http://localhost:8080/SpringMVC/user/quick11?userName=jack&age=17
}
@RequestMapping(value = "/quick12")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save12(User user) throws IOException {
System.out.println(user);
//http://localhost:8080/SpringMVC/user/quick12?username=jack&age=22
}
@RequestMapping(value = "/quick13")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save13(String [] strs) throws IOException {
System.out.println(Arrays.asList(strs));
//http://localhost:8080/SpringMVC/user/quick13?strs=111&strs=222&strs=333
}
<%--
Created by IntelliJ IDEA.
User: 86166
Date: 2022/6/29
Time: 17:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/quick14" method="post">
<input type="text" name="userList[0].username">
<input type="text" name="userList[0].age">
<input type="text" name="userList[1].username">
<input type="text" name="userList[1].age">
<input type="submit" value="提交">
</form>
</body>
</html>
VO:
package com.study.domain;
import java.util.List;
public class VO {
private List<User> userList;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
@Override
public String toString() {
return "VO{" +
"userList=" + userList +
'}';
}
}
@RequestMapping(value = "/quick14")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save14(VO vo) throws IOException {
System.out.println(vo);
}
<!-- 配置全局过滤filter-->
<filter>
<!-- 随便命名 -->
<filter-name>characterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<!-- /*:表示强制所有的请求先通过过滤器处理 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
@RequestMapping(value = "/quick14")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save14(VO vo) throws IOException {
System.out.println(vo);
}
@RequestMapping(value = "/quick15")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save15(@RequestParam(value = "name",required = false,defaultValue = "jack")String userName) throws IOException {
System.out.println(userName);
}
//http://localhost:8080/SpringMVC/user/quick16/jack
@RequestMapping(value = "/quick16/{username}")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save16(@PathVariable("username") String userName) throws IOException {
System.out.println(userName);
}
1.实现DateConverter接口:
package com.study.converter;
import org.springframework.core.convert.converter.Converter;
import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConverter implements Converter<String,Date> {
@Override
public Date convert(String dateStr) {
//将日期字符串转换为日期对象 返回
Date date=null;
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
try {
date = d.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
2.声明转换器:
<!--声明转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.study.converter.DateConverter"></bean>
</list>
</property>
</bean>
3.在annotation-driven中引用转换器
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
@RequestMapping(value = "/quick17")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save17(Date date) throws IOException {
System.out.println(date);
}
@RequestMapping(value = "/quick18")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save18(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
System.out.println(request);
System.out.println(response);
System.out.println(session);
}
@RequestMapping(value = "/quick19")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save19(@RequestHeader(value = "User-Agent",required = false) String user_agent) throws IOException {
System.out.println(user_agent);
}
使用@CookieValue可以获得指定Cookie的值
@RequestMapping(value = "/quick20")
@ResponseBody //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
public void save20(@CookieValue(value = "JSESSIONID",required = false) String jsessionId) throws IOException {
System.out.println(jsessionId);
}