基于SpringMVC注解的学生信息批量修改

2021-08-11 10:52:52 浏览数 (1)

实验目的:

  1. 熟悉SpringMVC环境搭建,环境的配置;
  2. 掌握SpringMVC的核心类和注解;
  3. 掌握SpringMVC的复杂数据绑定;
  4. 完成入门程序学生信息批量修改功能代码的编写。
  5. 采用编码过滤器解决中文乱码问题

实验内容:

  1. 1.创建com.xxx.po.Student.java包含属性id,stname,score。
  2. 在com.xxx.controller创建处理器类
  3. 采用编码过滤器解决中文乱码问题

实验步骤:

  1. 创建Web项目studentManager,并导入相关Jar包;

2. 在web.xml中配置Spring MVC的前端控制器等信息;

3. 创建Spring MVC配置文件,并配置组件扫描器和视图解析器;

4. 创建com.itheima.po.Student.java。包含属性id,stname,score

5. 创建StudentVO.java

6. 在com.itheima.controller创建处理器类。

7. 创建stuEdit.jsp

8. 创建访问成功后的响应页面。(localhost:8080/项目名/toStudentEdit)

界面展示:

代码展示:

代码语言:javascript复制
//Student.java
package com.xzk.po;

public class Student {
	private Integer id;
	private String stname;
	private int score;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getStname() {
		return stname;
	}
	public void setStname(String stname) {
		this.stname = stname;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	
}


//StudentVO.java
package com.xzk.vo;

import java.util.List;

import com.xzk.po.Student;

public class StudentVO {
	private List<Student> students;

	public List<Student> getStudents() {
		return students;
	}
	public void setStudents(List<Student> students) {
		this.students = students;
	}
}


//StudentController.java
package com.xzk.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xzk.po.Student;
import com.xzk.vo.StudentVO;

@Controller
public class StudentController {
	@RequestMapping("/toStudentEdit")
	public String toStudentEdit() {
		return "student_edit";
	}
	
	@RequestMapping("/editStudents")
	public String editStudent(StudentVO stuList) {
		List<Student> stus=stuList.getStudents();
		for(Student s:stus) {
			if(s.getId()!=null) {
				System.out.println("徐振凯修改了id=" s.getId() "的姓名为" "" s.getStname() "他的分数为:" s.getScore());
			}
		}
		return "success";
	}
}

//springmvc-config.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: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/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<!-- 指定需要扫描的包 -->
	<context:component-scan base-package="com.xzk.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>

0 人点赞