在控制器中使用原生的ServletAPI对象:只需要在控制器的方法参数定义HttpServletRequest和HttpServletResponse对象。
示例:
在SpringMVC中自定义类型转换器
基础上进行修改。
param.jsp
<%--
Created by IntelliJ IDEA.
User: Think
Date: 2020/4/30
Time: 8:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- 请求参数的绑定 --%>
<%--<a href="param/testParam?username=hehe&password=1234">请求参数的绑定</a>--%>
<%--<form action="param/saveAccount" method="post">--%>
<%--姓名:<input type="text" name="username"/><br/>--%>
<%--密码:<input type="text" name="password"/><br/>--%>
<%--金额:<input type="text" name="money"/><br/>--%>
<%--用户姓名:<input type="text" name="user.name"/><br/>--%>
<%--用户年龄:<input type="text" name="user.age"/><br/>--%>
<%--<input type="submit" value="提交"/>--%>
<%--</form>--%>
<%--<form action="param/saveAccount" method="post">--%>
<%--姓名:<input type="text" name="username"/><br/>--%>
<%--密码:<input type="text" name="password"/><br/>--%>
<%--金额:<input type="text" name="money"/><br/>--%>
<%--用户姓名:<input type="text" name="map['1'].name"/><br/>--%>
<%--用户年龄:<input type="text" name="map['1'].age"/><br/>--%>
<%--用户姓名:<input type="text" name="list[0].name"/><br/>--%>
<%--用户年龄:<input type="text" name="list[0].age"/><br/>--%>
<%--<input type="submit" value="提交"/>--%>
<%--</form>--%>
<%-- 自定义类型转换器 --%>
<%--<form action="param/saveUser" method="post">--%>
<%--用户姓名:<input type="text" name="name"/><br/>--%>
<%--用户年龄:<input type="text" name="age"/><br/>--%>
<%--用户生日:<input type="text" name="date"/>--%>
<%--<input type="submit" value="提交"/>--%>
<%--</form>--%>
<a href="param/testServlet">servlet原生API</a>
</body>
</html>
ParamController类:
package com.qublog.controller;
import com.qublog.domain.Account;
import com.qublog.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//请求参数绑定
@Controller
@RequestMapping("/param")
public class ParamController {
//请求参数绑定入门
@RequestMapping("/testParam")
public String testParam(String username, String password) {
System.out.println("执行了...");
System.out.println("用户名"+username);
System.out.println("密码"+password);
return "success";
}
//请求参数绑定,把数据封装到javabeand的类中
@RequestMapping("/saveAccount")
public String saveAccount(Account account) {
System.out.println(account);
return "success";
}
//自定义类型转换器
@RequestMapping("/saveUser")
public String saveUser(User user) {
System.out.println(user);
return "success";
}
//原生API获取
@RequestMapping("testServlet")
public String testServlet(HttpServletRequest request, HttpServletResponse response) {
System.out.println(request);
HttpSession session = request.getSession();
System.out.println(session);
ServletContext servletContext = session.getServletContext();
System.out.println(servletContext);
System.out.println(response);
return "success";
}
}