企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
一.实体类:Account.java,User.java,代码如下 ~~~ package com.youge.domain; import java.io.Serializable; import java.util.List; import java.util.Map; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-09-25 0:49 */ public class Account implements Serializable { private String username; private String password; private Double money; //引用类型 private User user; //集合类型 private List<User> list; private Map<String,User> map; 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 Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } public Map<String, User> getMap() { return map; } public void setMap(Map<String, User> map) { this.map = map; } @Override public String toString() { return "Account{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", money=" + money + ", user=" + user + ", list=" + list + ", map=" + map + '}'; } } ~~~ ***** ~~~ package com.youge.domain; import java.io.Serializable; import java.util.Date; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-09-25 1:09 */ public class User implements Serializable { private String name; private Integer age; private Date date; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", date=" + date + '}'; } } ~~~ 二.params.jsp,代码如下: ~~~ <%-- Created by IntelliJ IDEA. User: Administrator Date: 2020-09-25 Time: 0:22 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>请求参数绑定</title> </head> <body> <%--请求参数绑定--%> <a href="/param/testParam1?username=haha&password=122445">请求参数绑定</a> <br/> <a href="/param/testParam1?username=乔峰&password=122445">请求参数绑定(参数带中文)</a> <br> <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> <%-- list集合--%> 用户姓名:<input type="text" name="list[0].name"><br> 用户年龄:<input type="text" name="list[0].age"><br> <%--map集合--%> 用户姓名:<input type="text" name="map['one'].name"><br> 用户年龄:<input type="text" name="map['one'].age"><br> 出生日期:<input type="text" name="map['one'].date"><br> <input type="submit" value="提交"> </form> <br> <a href="/param/testServlet">Servlet原生的API</a> </body> </html> ~~~ ***** 三.控制器 ParamController.java ~~~ package com.youge.controller; import com.youge.domain.Account; import com.youge.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; import java.util.List; import java.util.Map; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-09-25 0:25 */ /** * 请求参数绑定 */ @Controller @RequestMapping("/param") public class ParamController { @RequestMapping("/testParam1") public String testParam1(String username,String password){ System.out.println("/testParam1"); System.out.println(username); System.out.println(password); return "success"; } /** * 请求参数绑定,把数据封装到JavaBean的类中 * @param account * @return */ @RequestMapping("/saveAccount") public String saveAccount(Account account){ System.out.println("执行了...."); System.out.println(account); System.out.println(account.getPassword()); List<User> list = account.getList(); System.out.println(list); System.out.println(list.get(0).getAge()); System.out.println(list.get(0).getName()); System.out.println("***"); Map<String, User> map = account.getMap(); System.out.println(map); System.out.println(map.get("one").getName()); System.out.println(map.get("one").getAge()); return "success"; } /** * 原生API * @param request * @param response * @return */ @RequestMapping("/testServlet") public String testServlet(HttpServletRequest request, HttpServletResponse response){ System.out.println("执行了"); 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"; } } ~~~