SpringMvc注入list实现的方法



SpringMVC的xml配置文件
| <?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:util="http://www.springframework.org/schema/util" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xmlns:aop="http://www.springframework.org/schema/aop" |
| xmlns:tx="http://www.springframework.org/schema/tx" |
| xmlns:mvc="http://www.springframework.org/schema/mvc" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/util |
| http://www.springframework.org/schema/util/spring-util.xsd |
| http://www.springframework.org/schema/context |
| http://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/aop |
| http://www.springframework.org/schema/aop/spring-aop.xsd |
| http://www.springframework.org/schema/tx |
| http://www.springframework.org/schema/tx/spring-tx.xsd |
| http://www.springframework.org/schema/mvc |
| http://www.springframework.org/schema/mvc/spring-mvc.xsd |
| "> |
| |
| <context:component-scan base-package="com.msb.controller" /> |
| |
| <mvc:annotation-driven> |
| |
| <mvc:message-converters> |
| <bean class="org.springframework.http.converter.StringHttpMessageConverter"> |
| <property name="supportedMediaTypes"> |
| <list> |
| |
| <value>text/plain;charset=UTF-8</value> |
| <value>text/html;charset=UTF-8</value> |
| <value>application/json;charset=UTF-8</value> |
| </list> |
| </property> |
| </bean> |
| </mvc:message-converters> |
| </mvc:annotation-driven> |
| |
| |
| <mvc:resources mapping="static/**" location="/static/"/> |
| |
| |
| |
| <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
| <property name="prefix" value="/view/"></property> |
| <property name="suffix" value=".jsp"></property> |
| </bean> |
| |
| </beans> |
webapp下的index.jsp文件
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <body> |
| <%--action在直接写/需要加相对路径--%> |
| <h2>信息提交</h2> |
| <form action="threadController.do" method="get"> |
| id:<input name="persions[0].id" type="text"><br/> |
| username:<input name="persions[0].name" type="text"><br/> |
| password:<input name="persions[0].password" type="password"><br/> |
| <p>爱好: |
| <input type="checkbox" name="persions[0].hobby" value="打篮球" v-model = "hobby">打篮球 |
| <input type="checkbox" name="persions[0].hobby" value="看书" v-model = "hobby">看书 |
| <input type="checkbox" name="persions[0].hobby" value="放风筝" v-model = "hobby">放风筝 |
| </p> |
| <br/> |
| birthday:<input name="persions[0].birthday" type="date"> |
| <br/> |
| <p> |
| 姓名:<input type="text" name="pets[0].Petname"> |
| 类型:<input type="text" name="pets[0].Petanimal"> |
| </p> |
| <p> |
| 姓名:<input type="text" name="pets[1].Petname"> |
| 类型:<input type="text" name="pets[1].Petanimal"> |
| </p> |
| <input value=提交信息 type="submit"> |
| </form> |
| |
| </body> |
| </html> |
总集合的配置
| package com.msb.pojo; |
| import lombok.AllArgsConstructor; |
| import lombok.Data; |
| import lombok.NoArgsConstructor; |
| |
| import java.util.List; |
| @Data |
| @AllArgsConstructor |
| @NoArgsConstructor |
| public class ListArrays { |
| private List<Pet> pets; |
| private List<Persion> persions; |
| } |
子集合的配置
| |
| package com.msb.pojo; |
| import lombok.AllArgsConstructor; |
| import lombok.Data; |
| import lombok.NoArgsConstructor; |
| import org.springframework.format.annotation.DateTimeFormat; |
| import java.util.Date; |
| import java.util.List; |
| @Data |
| @AllArgsConstructor |
| @NoArgsConstructor |
| public class Persion { |
| private Integer id; |
| private String name; |
| private String password; |
| private List[] hobby; |
| @DateTimeFormat(pattern = "yyyy-MM-dd") |
| private Date birthday; |
| } |
| |
| package com.msb.pojo; |
| import lombok.AllArgsConstructor; |
| import lombok.Data; |
| import lombok.NoArgsConstructor; |
| import java.util.List; |
| @NoArgsConstructor |
| @AllArgsConstructor |
| @Data |
| public class Pet { |
| private String Petname; |
| private String Petanimal; |
| |
| } |
访问跳转配置Controller
| package com.msb.controller; |
| |
| import com.msb.pojo.ListArrays; |
| import com.msb.pojo.Persion; |
| import com.msb.pojo.Pet; |
| import jdk.nashorn.internal.ir.RuntimeNode; |
| import lombok.Value; |
| import org.springframework.stereotype.Controller; |
| import org.springframework.web.bind.annotation.*; |
| import sun.security.util.Password; |
| import java.util.Arrays; |
| import java.util.List; |
| |
| |
| |
| |
| |
| |
| @RestController |
| public class FirstController { |
| @RequestMapping(value = "testMappering.do", method = RequestMethod.GET) |
| public String testMappering() { |
| System.out.println("RequestMapping收到访问"); |
| return "first"; |
| } |
| |
| @RequestMapping("/firstController.do") |
| public String firstController() { |
| System.out.println("this is firstController"); |
| |
| return "first"; |
| } |
| |
| @RequestMapping("/secondController/{username}/{password}") |
| |
| public String secondController(@PathVariable("username") String username, @PathVariable("password") String password) { |
| System.out.println("this is firstController"); |
| System.out.println("username:" + username); |
| System.out.println("password:" + password); |
| |
| return "first"; |
| } |
| |
| @RequestMapping(value = "/threadController.do" ,method = RequestMethod.GET) |
| public String threadController(ListArrays arrays ) { |
| |
| System.out.println(arrays.getPets()); |
| System.out.println(arrays.getPersions()); |
| return "执行成功"; |
| } |
| |
| } |
| |
不同方式获取前端资源
Map键值对设计
| <h6>Map标签的键值对</h6> |
| <p> |
| 姓名:<input type="text" name="petMap['p'].Petname"> |
| 类型:<input type="text" name="petMap['p'].Petanimal"> |
| </p> |
| <p> |
| 姓名:<input type="text" name="petMap['m'].Petname"> |
| 类型:<input type="text" name="petMap['m'].Petanimal"> |
| </p> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!