初学SpringMVC之集合数据绑定

一、开发环境(IDEA)SDK1.8项目创建
实现批量修改操作,删除同理
项目创建
项目文件结构
springmvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<context:component-scan base-package="com.team5408.Controller" />
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	     <property name="prefix" value="/WEB-INF/jsp/" />
	     <property name="suffix" value=".jsp" />
	</bean>

</beans> 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <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>
</web-app>

CourseController.java

package com.team5408.Controller;

import com.team5408.po.Course;
import com.team5408.vo.CourseVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* Author: Linran
 * \* createDate: 2019/4/11 19:08
 * \* Description:
 * \
 */
@Controller
public class CourseController {
	@RequestMapping("/course")
	public String course(){
		return "course";
	}
	@RequestMapping("/choice")
	public String choice(CourseVO courseList){
		Integer Credit=0;
		List<Course> courses=courseList.getCourses();
		for(Course course: courses){
			if(course.getId()!=null) {
				System.out.println("课程ID:" + course.getId() + "**" + "学期:" + course.getTerm() + "**" + "课程代码:" + course.getCno() + "**" + "课程名称:" + course.getCname() + "**" + "课程学分:" + course.getCredit());
				Credit += course.getCredit();
			}
		}
		//
		System.out.println("已选择课程总学分为:"+Credit);
		return "success";
	}

}

Course.java

package com.team5408.po;

/**
 * \* Created with IntelliJ IDEA.
 * \* Author: Linran
 * \* createDate: 2019/4/12 10:53
 * \* Description:
 * \
 */
public class Course {
	private Integer id;
	private String term;
	private String cno;
	private String cname;
	private Integer credit;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public String getCno() {
		return cno;
	}

	public void setCno(String cno) {
		this.cno = cno;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public Integer getCredit() {
		return credit;
	}

	public void setCredit(Integer credit) {
		this.credit = credit;
	}
}

CourseVO.java

package com.team5408.vo;

import com.team5408.po.Course;

import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* Author: Linran
 * \* createDate: 2019/4/12 9:04
 * \* Description:
 * \
 */
public class CourseVO {
	private List<Course> courses;
	public List<Course> getCourses(){
		return courses;
	}
	public void setCourses(List<Course> courses){
		this.courses=courses;
	}
}

course.jsp

<%--
  Created by IntelliJ IDEA.
  User: Linran
  Date: 2019/4/12
  Time: 10:44
  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>
<form action="${pageContext.request.contextPath}/choice" method="post">
    <table border="1">
        <tr><td>请选择</td><td>学期(term)</td><td>代码(cno)</td><td>课程(cname)</td><td>学分(credit)</td></tr>
        <tr><td><input type="checkbox" name="courses[0].id" value="1" /></td>
            <td><input type="text" name="courses[0].term" value="2016-2017-1" /></td>
            <td><input type="text" name="courses[0].cno" value="1H11137" /></td>
            <td><input type="text" name="courses[0].cname" value="程序设计基础1" /></td>
            <td><input type="text" name="courses[0].credit" value="2" /></td></tr>
        <tr><td><input type="checkbox" name="courses[1].id" value="2" /></td>
            <td><input type="text" name="courses[1].term" value="2016-2017-2"></td>
            <td><input type="text" name="courses[1].cno" value="1H11145"></td>
            <td><input type="text" name="courses[1].cname" value="程序设计基础2"></td>
            <td><input type="text" name="courses[1].credit" value="4"></td></tr>
        <tr><td><input type="checkbox" name="courses[2].id" value="3" /></td>
            <td><input type="text" name="courses[2].term" value="2017-2018-1"></td>
            <td><input type="text" name="courses[2].cno" value="1H10500"></td>
            <td><input type="text" name="courses[2].cname" value="面向对象程序设计"></td>
            <td><input type="text" name="courses[2].credit" value="6"></td></tr>
        <tr><td><input type="checkbox" name="courses[3].id" value="4" /></td>
            <td><input type="text" name="courses[3].term" value="2017-2018-2"></td>
            <td><input type="text" name="courses[3].cno" value="1H10617"></td>
            <td><input type="text" name="courses[3].cname" value="数据结构与算法"></td>
            <td><input type="text" name="courses[3].credit" value="6"></td></tr>
        <tr><td><input type="checkbox" name="courses[4].id" value="5" /></td>
            <td><input type="text" name="courses[4].term" value="2018-2019-1"></td>
            <td><input type="text" name="courses[4].cno" value="1H10177"></td>
            <td><input type="text" name="courses[4].cname" value="操作系统"></td>
            <td><input type="text" name="courses[4].credit" value="4"></td></tr>
    </table>
    <input type="submit" value="确定" />
</form>
</body>
</html>

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: Linran
  Date: 2019/4/11
  Time: 19:09
  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>
OK
</body>
</html>

效果
jsp界面
控制台
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值