1. 参数类型
基本类型 POJO类型 数组类型 集合类型 json字符串 Restful风格 日期类型 servlet api 请求头
2. 编写代码测试
2.1 添加依赖
< dependencies>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.2.10.RELEASE</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> servlet-api</ artifactId>
< version> 2.5</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-core</ artifactId>
< version> 2.11.3</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-databind</ artifactId>
< version> 2.11.3</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-annotations</ artifactId>
< version> 2.11.3</ version>
</ dependency>
</ dependencies>
2.2 编写实体类
public class User
{
private String username;
private Integer age;
public User ( )
{
}
public User ( String username, Integer age)
{
this . username = username;
this . age = age;
}
public String getUsername ( )
{
return username;
}
public void setUsername ( String username)
{
this . username = username;
}
public Integer getAge ( )
{
return age;
}
public void setAge ( Integer age)
{
this . age = age;
}
@Override
public String toString ( )
{
return "User{" +
"username='" + username + '\'' +
", age=" + age +
'}' ;
}
}
public class Vo
{
private List< User> users;
public List< User> getUsers ( )
{
return users;
}
public void setUsers ( List< User> users)
{
this . users = users;
}
@Override
public String toString ( )
{
return "Vo{" +
"users=" + users +
'}' ;
}
}
2.3 编写日期转换器
public class DateConverter implements Converter < String, Date>
{
private SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
@Override
public Date convert ( String s)
{
Date date = null;
try
{
date = sdf. parse ( s) ;
} catch ( ParseException e)
{
e. printStackTrace ( ) ;
}
return date;
}
}
2.4 编写Spring核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: mvc= " http://www.springframework.org/schema/mvc"
xmlns: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" >
< context: component-scan base-package = " site.zhouyun.controller" />
< bean id = " viewResolver" class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /jsp/" > </ property>
< property name = " suffix" value = " .jsp" > </ property>
</ bean>
< mvc: annotation-driven conversion-service = " conversionService" />
< mvc: default-servlet-handler />
< bean id = " conversionService" class = " org.springframework.context.support.ConversionServiceFactoryBean" >
< property name = " converters" >
< list>
< bean class = " site.zhouyun.converter.DateConverter" > </ bean>
</ list>
</ property>
</ bean>
</ beans>
2.5 修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns = " http://xmlns.jcp.org/xml/ns/javaee"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version = " 4.0" >
< 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>
< servlet>
< servlet-name> dispatcherServlet</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< init-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:spring-mvc.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> dispatcherServlet</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
</ web-app>
2.6 编写Controller
@Controller
@RequestMapping ( "request" )
public class DataRequestController
{
@ResponseBody
@RequestMapping ( "method01" )
public void method01 ( String username, Integer age)
{
System. out. println ( "username=" + username + ", age=" + age) ;
}
@ResponseBody
@RequestMapping ( "method02" )
public void method02 ( User user)
{
System. out. println ( user) ;
}
@ResponseBody
@RequestMapping ( "method03" )
public void method03 ( String[ ] strs)
{
System. out. println ( Arrays. asList ( strs) ) ;
}
@ResponseBody
@RequestMapping ( "method04" )
public void method04 ( Vo vo)
{
System. out. println ( vo) ;
}
@RequestMapping ( value = "method05" , method = RequestMethod. POST)
@ResponseBody
public void method05 ( @RequestBody List< User> users)
{
System. out. println ( users) ;
}
@ResponseBody
@RequestMapping ( "method06" )
public void method06 ( @RequestParam ( value = "name" , required = false , defaultValue = "cloud" ) String username)
{
System. out. println ( username) ;
}
@ResponseBody
@RequestMapping ( "method07/{username}" )
public void method07 ( @PathVariable String username)
{
System. out. println ( username) ;
}
@ResponseBody
@RequestMapping ( "method08" )
public void method08 ( Date date)
{
System. out. println ( date) ;
}
@RequestMapping ( "method09" )
public void method09 ( HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException
{
String username = request. getParameter ( "username" ) ;
session. setAttribute ( "username" , username) ;
System. out. println ( username) ;
response. sendRedirect ( "/jsp/success.jsp" ) ;
}
@ResponseBody
@RequestMapping ( "method10" )
public void method10 ( @RequestHeader ( "User-Agent" ) String user_agent, @CookieValue ( "JSESSIONID" ) String jsessionid)
{
System. out. println ( "User-Agent: " + user_agent) ;
System. out. println ( "JSESSIONID: " + jsessionid) ;
}
}
2.7 添加jQuery
在webapp目录下创建js文件夹, 然后将jQuery放入.
2.8 添加jsp文件
在webapp目录下创建jsp文件夹, 然后将jsp文件放入.
ajax.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<!-- 引入jQuery -->
<script src="${pageContext.request.contextPath}/js/jquery-3.5.1.js"></script>
<script>
// 测试json字符串方式, 在浏览器访问 http://localhost:8080/jsp/ajax.jsp 然后查看控制台就能看到效果了
var users = new Array();
// 构造数据
users.push({username:"zhangsan",age:18});
users.push({username:"lisi",age:20});
$.ajax({
type: "post",
url: "${pageContext.request.contextPath}/request/method05",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(users)
});
</script>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Hello ${sessionScope.username}</h1>
</body>
</html>
user.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<!-- 测试数组方式 -->
<form method="post" action="${pageContext.request.contextPath}/request/method04">
用户1:<br/>
<input type="text" name="users[0].username" placeholder="用户名"><br/>
<input type="number" name="users[0].age" placeholder="年龄"><br/><br/>
用户2:<br/>
<input type="text" name="users[1].username" placeholder="用户名"><br/>
<input type="number" name="users[1].age" placeholder="年龄"><br/><br/>
用户3:<br/>
<input type="text" name="users[2].username" placeholder="用户名"><br/>
<input type="number" name="users[2].age" placeholder="年龄"><br/><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
3. 查看效果
启动项目打开浏览器访问http://localhost:8080/request/method(01~10)
就可以看到效果了.