SpringMVC学习之——02

版权声明:为中华之崛起而努力! https://blog.csdn.net/Kj_Gym/article/details/81191621

简单的增删改查操作

1. 建立student的model类

package com.kjgym.model;

public class Student {
	private int id;
	private String stuName;
	private int age;
	public Student() {
		super();
	}
	public Student(int id, String stuName, int age) {
		super();
		this.id = id;
		this.stuName = stuName;
		this.age = age;
	}
	public int getid() {
		return id;
	}
	public void setid(int id) {
		this.id = id;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

2.  建立StudentController类

package com.kjgym.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.kjgym.model.Student;

@Controller
@RequestMapping("/student")  // 用来区分模块
public class StudentController {
	// 这里使用集合模拟数据库操作
	private static List<Student> studentList = new ArrayList<Student>();
	static{
		studentList.add(new Student(1,"张三",20));
		studentList.add(new Student(2,"李四",21));
		studentList.add(new Student(3,"王五",22));
	}
	
	@RequestMapping("/list")
	public ModelAndView list(){
		ModelAndView mav = new ModelAndView();
		mav.addObject("studentList", studentList);// 键值对形式,弄进request里
		mav.setViewName("student/list");
		return mav;
	}
	
	/**
	 * 添加和修改
	 * @param id
	 * @return
	 */
	@RequestMapping("/preSave")
	public ModelAndView preSave(@RequestParam(value="id",required=false) String id){
		ModelAndView mav = new ModelAndView();
		
		if(id!=null){
			mav.addObject("student", studentList.get(Integer.parseInt(id)-1));
			mav.setViewName("student/update");
		}else{
			mav.setViewName("student/add");
		}
		return mav;
	}
	/**
	 * 对象属性自动封装
	 * @param student
	 * @return
	 */
	@RequestMapping("/save")
	public String save(Student student){
		studentList.add(student);
		return "redirect:/student/list.do";
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="id") int id){
		studentList.remove(id);
		return "redirect:/student/list.do";
	}
}

3.  spring-mvc.xml配置,这个文件放在src目录下即可

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 使用注解的包,包括子集 -->
    <context:component-scan base-package="com.kjgym"/>
	
	<!-- 支持对象与JSON的转换 -->
	<mvc:annotation-driven/>


    <!-- 视图解析器 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

4.  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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SpringMvc01</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<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:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
    <!-- 下面这个是为了解决spring乱码问题,直接加上就行 -->
	<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>*.do</url-pattern>
	</filter-mapping>
	
</web-app>

5. 

1.在WEB-INF目录下建立jsp文件夹,在jsp文件夹下建立student文件夹。然后再其中建立add.jsp,list.jsp,update.jsp

2. 在webcontent目录下建立index.jsp

3.代码如下

 add.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/student/save.do" method="post">
	<table>
		<tr>
			<th colspan="2" style="text-align: center;">学生添加</th>
		</tr>
		<tr>
			<td>学生姓名:</td>
			<td><input type="text" name="stuName"/></td>
		</tr>
		<tr>
			<td>学生年龄:</td>
			<td><input type="text" name="age"/></td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="submit" value="提交"/>
			</td>
		</tr>
	</table>
</form>
</body>
</html>

list.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>主页</title>
</head>
<body>

<a href="${pageContext.request.contextPath }/student/preSave.do">添加学生</a>
<table border="1px">
	<tr style="padding-bottom: 4px">
		<th colspan="3">学生信息</th>
	</tr>
	<tr>
		<th>编号</th>
		<th>姓名</th>
		<th>年龄</th>
		<th>操作</th>
	</tr>
	
	<c:forEach var = "student" items="${studentList }">
	<tr>
		<td>${student.id }</td>
		<td>${student.stuName }</td>
		<td>${student.age }</td>
		<td>
			<a href="${pageContext.request.contextPath }/student/preSave.do?id=${student.id}">修改</a>
			<a href="${pageContext.request.contextPath }/student/delete.do?id=${student.id}">删除</a>
		</td>
	</tr>
	</c:forEach>
</table>
</body>
</html>

update.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/student/save.do" method="post">
	<table>
		<tr>
			<th colspan="2" style="text-align: center;">学生修改</th>
		</tr>
		<tr>
			<td>学生姓名:</td>
			<td><input type="text" name="stuName" value="${student.stuName }"/></td>
		</tr>
		<tr>
			<td>学生年龄:</td>
			<td><input type="text" name="age" value="${student.age }"/></td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="hidden" name="id" value="${student.id }">
				<input type="submit" value="提交"/>
			</td>
		</tr>
	</table>
</form>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<% response.sendRedirect("student/list.do"); %>

运行效果

猜你喜欢

转载自blog.csdn.net/Kj_Gym/article/details/81191621