SpringMVC获取请求参数-集合类型

1.创建User实体类


```java
public class User {private String username;private int age;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User{" +"username='" + username + '\'' +", age=" + age +'}';}
}

2.配置spring mvc.xml文件


<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.xsdhttp://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.hao.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/>
beans>

3.配置web.xml配置文件


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><servlet><servlet-name>DispatcherServletservlet-name><servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class><init-param><param-name>contextConfigLocationparam-name><param-value>classpath:spring-mvc.xmlparam-value>init-param><load-on-startup>1load-on-startup>servlet><servlet-mapping><servlet-name>DispatcherServletservlet-name><url-pattern>/url-pattern>servlet-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>listener><context-param><param-name>contextConfigLocationparam-name><param-value>classpath:applicationContext.xmlparam-value>context-param>
web-app>

4.编写Controller层

@Controller
public class UserController {@RequestMapping("/report14")@ResponseBodypublic void save14(VO vo){System.out.println(vo);}
}

5.编写VO类封装集合

public class VO {private List<User> userList;public List<User> getUserList() {return userList;}public void setUserList(List<User> userList) {this.userList = userList;}@Overridepublic String toString() {return "VO{" +"userList=" + userList +'}';}
}

6.编写form.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><form action="${pageContext.request.contextPath}/report14" method="post"><input type="text" name="userList[0].username"><br/><input type="text" name="userList[0].age"><br/><input type="text" name="userList[1].username"><br/><input type="text" name="userList[1].age"><br/><input type="submit" value="提交"></form>
</body>
</html>

7.启动tomcat服务器,填写表单数据,结果如下:
在这里插入图片描述
8.乱码问题解决-》设置编码过滤器


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部