美文网首页程序员
maven+spring mvc+idea搭建简单增删改查系统完

maven+spring mvc+idea搭建简单增删改查系统完

作者: 小白学编程 | 来源:发表于2018-09-01 16:21 被阅读0次

效果图


image.png

工程目录


目录结构.png

因为使用了maven,所以各种需要的jar包只需要在pom.xml添加相关依赖
pom.xml配置内容

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>secondlearnspringmvc</artifactId>
        <groupId>com.zzq</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>practice-01</artifactId>
    <packaging>war</packaging>

    <name>practice-01 Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>practice-01</finalName>
        <plugins>
            <!-- 编译插件,指定编译用的的jdk版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <!-- jdk的版本号 -->
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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

    <!-- 注册组件扫描器 -->
    <context:component-scan base-package="com.zzq.*"/>
    <mvc:annotation-driven/>
    <!--<mvc:resources mapping="/images/**" location="/images/" />-->

    <!--注册multipartResolver,由DispatcherServlet来负责调用-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--设置字符编码防止文件名乱码-->
        <property name="defaultEncoding" value="utf-8"/>
        <!--设置上传文件的总大小,单位是字节b-->
        <property name="maxUploadSize" value="10485760"/>
        <!--设置单个上传文件的大小,单位是字节b-->
        <property name="maxUploadSizePerFile" value="10485760"/>

    </bean>
    <!-- 视图解释类 -->

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

web.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <!--字符编码过滤器-->
    <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>

        <!--强制指定字符编码,即如果在request中指定了字符编码,那么也会为其强制指定当前设置的字符编码-->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>

    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 注册spring MVC中央控制器 -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <!-- spring MVC中的核心控制器 -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

先在bean包新建Person类

package com.zzq.bean;

public class Person {

    private int id;

    private String name;

    private int age;

    public Person(){

    }

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

在service包下新建PersonService类

package com.zzq.service;

import com.zzq.bean.Person;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;

@Service
public class PersonService {

    private static HashMap<Integer,Person> map=new HashMap();

    private static int id=0;

    static {
        map.put(1,new Person(++id,"小张",21));
        map.put(2,new Person(++id,"小王",18));
        map.put(3,new Person(++id,"小李",19));
        map.put(4,new Person(++id,"小赵",21));
        map.put(5,new Person(++id,"小钱",18));
        map.put(6,new Person(++id,"小孙",19));
    }

    //返回所有学生信息
    public List findAll(){
        return new ArrayList(map.values());
    }

    //添加学生
    public void add(Person person){
        person.setId(++id);
        map.put(id,person);
    }

    //取学生id
    public Person getPersonById(int id){
        return map.get(id);
    }
//修改学生
    public void update(Person person){
        map.put(person.getId(),person);
    }
//删除学生
    public void delete(Person person){
        map.remove(person.getId());
    }
}

在controller包下新建PersonController类

package com.zzq.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.zzq.bean.Person;
import com.zzq.service.PersonService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.jws.WebParam;
import java.util.ArrayList;

@Controller
public class PersonController {
@Resource
    PersonService ps;

    //展示学生信息列表
    @RequestMapping("/showAll.do")
    public String showAll(Model m){
        ArrayList list= (ArrayList) ps.findAll();
        m.addAttribute("personList",list);

        return "PersonList";
    }

    //添加新生信息
    @RequestMapping("/addStudent.do")
    public String addStudent(Person person){

        ps.add(person);
        return "redirect:/showAll.do";
    }

    //修改学生信息
    @RequestMapping("/updateStudent.do")
    public String updateStudent(int id, Model model){
        Person person=ps.getPersonById(id);
        model.addAttribute("person",person);
        return "updateStudent";
    }
    @RequestMapping("/update.do")
    public String update(Person person){
        ps.update(person);
        return "redirect:/showAll.do";
    }

    //删除单条学生信息
    @RequestMapping("/delete.do")
    public String deleteStudent(int id){
        ps.delete(ps.getPersonById(id));
        return "redirect:/showAll.do";
    }

    //批量删除学生信息
    @RequestMapping("/deleteMuch.do")
    public String deleteMuch(@RequestParam("id") int[] ids){
        for (int id : ids){
            ps.delete(ps.getPersonById(id));
        }
        return "redirect:/showAll.do";
    }

    //根据学号id查询学生信息
    @RequestMapping("/search.do")
    public String search(int searchId,Model model){
        Person person=ps.getPersonById(searchId);
        model.addAttribute("p",person);
        return  "searchStudent";
    }
}

在jsp文件夹下新建PersonList.jsp显示人员列表

<%--
  Created by IntelliJ IDEA.
  User: zzq
  Date: 2018/8/30
  Time: 19:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"   prefix="c" %>
<html>
<head>
    <title>Title</title>
</head>

<script type="text/javascript">
    function deleteMuch() {
        document.forms[0].action = "${pageContext.request.contextPath}/deleteMuch.do";
        document.forms[0].submit();  <!-- 手动提交 -->
    }
    
    function search() {
        //var id=document.getElementById("searchId").value;
        document.forms[0].action="${pageContext.request.contextPath}/search.do";
        document.forms[0].submit();

    }
</script>
<body>

<form action="#" method="post">

    <div style="padding:20px;">
        学生列表
    </div>

    <%--<form action="/jsp/addStudent.jsp">--%>
        <%--<input value="添加" type="submit">--%>
    <%--</form>--%>
    <a href="/jsp/addStudent.jsp">添加</a>
    <a href="#" onclick="deleteMuch()">批量删除</a>
    <input name="searchId" type="text" ><button  onclick="search()">查找</button>

    <%--<form action="/jsp/">--%>
        <%--<input name="search" type="text"><input value="查询" type="submit">--%>
    <%--</form>--%>

    <table border="1">
        <tr>
            <td>选择:</td>
            <td>学号:</td>
            <td>姓名:</td>
            <td>年龄:</td>
            <td>操作:</td>

        </tr>

        <c:forEach items="${personList}" var="p">
            <tr>
                <td>
                    <input type="checkbox" name="id" value="${p.id}"/>
                </td>
                <td>${p.id}</td>
                <td>${p.name}</td>
                <td>${p.age}</td>
                <td>
                    <a href="/updateStudent.do?id=${p.id}">修改</a>
                </td>
                <td>
                    <a href="/delete.do?id=${p.id}">删除</a>
                </td>
            </tr>
        </c:forEach>

    </table>
</form>

</body>
</html>
</body>
</html>

继续在jsp文件夹下新建addStudent.jsp,用于添加学生的页面

<%--
  Created by IntelliJ IDEA.
  User: zzq
  Date: 2018/8/30
  Time: 20:01
  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}/addStudent.do" method="post">

    <div style="padding:20px;">
        新生信息编辑
    </div>

    <table>
        <tr>
            <td>姓名:</td>
            <td><input type="text" name="name" value=""/></td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td><input type="text" name="age" value=""/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" name="btnOK" value="确定"/></td>
        </tr>
    </table>
</form>

</body>
</html>

继续新建updateStudent.jsp,用于修改信息的页面

<%--
  Created by IntelliJ IDEA.
  User: zzq
  Date: 2018/8/30
  Time: 22:30
  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}/update.do" method="post">

    <!-- 添加隐藏域,隐藏 id  -->
    <input type="hidden" name="id" value="${person.id}"/>


    <div style="padding:20px;">
        学生信息修改
    </div>

    <table>
        <tr>
            <td>姓名:</td>
            <td><input type="text" name="name" value="${person.name}"/></td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td><input type="text" name="age" value="${person.age}"/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" name="btn" value="修改"/></td>
        </tr>
    </table>
</form>
</body>
</html>

继续新建searchStudent.jsp,用于显示搜索的学生信息


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: zzq
  Date: 2018/8/31
  Time: 9:43
  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>
<table border="1">
    <tr>

        <td>学号:</td>
        <td>姓名:</td>
        <td>年龄:</td>


    </tr>

    <tr>
        <td>${p.id}</td>
        <td>${p.name}</td>
        <td>${p.age}</td>

    </tr>
</table>
</body>
</html>

相关文章

网友评论

    本文标题:maven+spring mvc+idea搭建简单增删改查系统完

    本文链接:https://www.haomeiwen.com/subject/ryvkwftx.html