ssm系列整合bootscrap的增删改查

**

前言:首先你得需要具备一定的java基础知识,maven等相应的基础知识,IntelliJ
IDEA开发工具的一些知识,在掌握以上几个知识之后,再来跟着学习就会轻松一些。如果要放弃,请早点做好准备,这个并不是哪个人都能做好一个It开发人员,没有艰辛的付出,是不可能达到的,即使付出了,也不一定成功,还是趁早放弃。如果坚持,请接受寂寞的努力学习吧!不抛弃,不放弃,加油!!!

**

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接出处:https://blog.csdn.net/qq_3076499,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 如果对您有帮助 ,请多多支持.多少都是您的心意与支持,一分也是爱,再次感谢!!! 支付宝赞赏:

这里写图片描述

记得点击下面的余额宝,红包可能要大些。

开发环境:这个根据个人的喜欢,没有强制的。喜欢用什么版本就用什么版本。 Window 7 64; IntelliJ IDEA;Apache-Maven-3.3.9;Spring ;Mybatis;Springmvc;数据库:mysql

前端技术框架:bootscrap

完整代码之后会上传:会提供链接供大家参考学习

整体的一个项目结构如下图:
这里写图片描述

搭建ssm工程,也许刚开始你是非常模糊的,但如果你搭建10次,20次,50次以上,那么真的其实也都大同小异,也就那点知识。
搭建的步骤:

1:首先pom.xml 加载jar包具体如下:

<?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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>

  <name>ssm-crud2</name>
  <groupId>com.demo</groupId>
  <artifactId>ssm-crud2</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <!-- 设置项目编码编码 -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <!-- spring版本号 -->
    <spring.version>4.3.5.RELEASE</spring.version>
    <!-- mybatis版本号 -->
    <mybatis.version>3.4.1</mybatis.version>
  </properties>

  <dependencies>
    <!-- java ee -->
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
    </dependency>

    <!-- 单元测试 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

    <!-- 实现slf4j接口并整合 -->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.2</version>
    </dependency>

    <!-- JSON -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.7</version>
    </dependency>


    <!-- 数据库 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.41</version>
      <scope>runtime</scope>
    </dependency>

    <!-- 数据库连接池 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <!-- MyBatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>

    <!-- mybatis/spring整合包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>

    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>


    <!--spring整合shiro,这个是与shiro的整合,暂时在这里不起作用,但加上以后会用上-->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-core</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-web</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-ehcache</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-all</artifactId>
      <version>1.2.1</version>
    </dependency>

  </dependencies>


</project>

2: 配置:spring-mybatis.xml的整合配置代码如下:注:这个文件整合了jdbc mybatis service一起整合了,不管怎么变,反正ssm也就这几个整合。

<?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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--扫描service-->
    <context:component-scan base-package="com.demo.service"/>
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--C3PO数据源配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--加载mybatis工厂-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <!--注入数据库连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--扫描bean包 使用别名-->
        <property name="typeAliasesPackage" value="com.demo.bean"/>
        <!--扫描sql配置文件:mapper需要的xml文件-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>


    <!--配置扫描Dao接口包,动态实现Dao接口,注入到Spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.demo.dao"/>
    </bean>

    <!--配置事务管理器transactionManager-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
<!--这里使用了注解的事务配置-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

3: 配置:spring-mvc.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"
       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-3.0.xsd">
    <!--扫描controller-->
    <context:component-scan base-package="com.demo.controller"/>
    <!--开启注解-->
    <mvc:annotation-driven/>
    <!--解决静态资源请求-->
    <mvc:default-servlet-handler/>

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

</beans>

4:jdbc.properties的代码如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

5:log4j.properties的代码如下:

log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

6:web.xml的配置代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <!--加载spring相应的配置文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>
    <!--配置编码过滤器-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--配置springmvc的配置-->
    <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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

至此:很快的ssm工程就搭建好了。接下开始写代码了:
开发思路:首先设计数据库,这里简单的设计
一:使用的是mysql:数据库代码如下:

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `sex` varchar(255) DEFAULT NULL,
  `age` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

二:根据数据库,创建相应的bean实体类。实体类不同的人,喜欢叫法不一样,有的叫pojo,有的叫domain。反正都一样,就是实体类而已。代码如下:

package com.demo.bean;

public class User {
    private Integer id;
    private String name;
    private String age;
    private String sex;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

三:创建dao接口类,这个类是用于与mysql数据库的交互的接口。具体代码如下:
简单的几个增删改查接口:

package com.demo.dao;

import com.demo.bean.User;

import java.util.List;

public interface UserDao {
    User findUserById(Integer id);

    List<User> findAll();

    boolean deleteUser(Integer id);

    Integer updateUser(User user);

    void saveUser(User user);

}

四:dao接口写好之后,接下来是userDao.xml的语句,对数据库进行操作。具体代码如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.dao.UserDao">
    <select id="findUserById" resultType="com.demo.bean.User" parameterType="Integer">
        select id,name,sex,age from user where id=#{id}
    </select>

    <select id="findAll" resultType="com.demo.bean.User">
        select id,name,sex,age from user
    </select>

    <!--新增-->
    <insert id="saveUser" parameterType="com.demo.bean.User">
        insert  into user(id,name,sex,age) values(#{id},#{name},#{sex},#{age})
    </insert>

    <!--删除-->
    <delete id="deleteUser" parameterType="int">
        delete from user where id=#{id}
    </delete>

    <!--修改-->
    <update id="updateUser" parameterType="com.demo.bean.User">
        update user set name=#{name},sex=#{sex},age=#{age} where id=#{id}
    </update>
</mapper>

五:service接口代码如下:

package com.demo.service;

import com.demo.bean.User;

import java.util.List;


public interface UserService {
    User findUserById(Integer id);

    List<User> findAll();

    boolean deleteUser(Integer id);

    Integer updateUser(User user);

    void saveUser(User user);

}

六:service接口的业务实现,那么我们在开发中,其实主要就是编写这个serviceImpl业务逻辑的具体实现与controller表现层,具体代码如下:

package com.demo.service.Impl;

import com.demo.bean.User;
import com.demo.dao.UserDao;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserUserviceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    //根据Ids查询
    public User findUserById(Integer id) {
        return userDao.findUserById(id);
    }
    //查询所有
    public List<User> findAll() {
        return userDao.findAll();
    }
    //根据id查询
    public boolean deleteUser(Integer id) {
        return userDao.deleteUser(id);
    }
    //修改用户
    public Integer updateUser(User user) {
        return userDao.updateUser(user);
    }
    //添加用户
    public void saveUser(User user) {
     userDao.saveUser(user);
    }
}

七:controller表现层的代码如下:

package com.demo.controller;

import com.demo.bean.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/user")

public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 跳转到添加用户界面
     * @return
     */
    @RequestMapping("toAddUser")
    public String toAddUser(){
        return "addUser";
    }


    /**
     * 添加用户并重定向
     * @param model
     * @param user
     * @return
     */
    @RequestMapping("addUser")
    public String addUser(Model model, User user){
        if (user != null){
            userService.saveUser(user);
        }
        return "redirect:/user/getAllUser";
    }


    /**
     * 查询全部用户
     * @param model
     * @return
     */
    @RequestMapping("getAllUser")
    public String getAllUser(Model model){
        List<User> user = userService.findAll();
        model.addAttribute("userList",user);
        return "allUser";
    }


    /**
     * 删除用户
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("delUser")
    public String removeUser(Model model,Integer id){
        model.addAttribute("user",userService.deleteUser(id));
        //userService.deleteUser(id);
        return "redirect:/user/getAllUser";
    }


    /**
     * 跳转到更新用户页面
     */
    @RequestMapping("toUpdate")
    public String toUpdate(@RequestParam(value = "id")Integer id, Model model){
        model.addAttribute("user",userService.findUserById(id));
        return "updateUser";
    }

    /**
     * 更新用户
     * @param
     * @return
     */
    @RequestMapping(value = "updateUser",method = RequestMethod.POST)
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:/user/getAllUser";
    }

}

八:UserDaoTest.java测试,用来测试数据:具体代码如下:

import com.demo.bean.User;
import com.demo.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-mybatis.xml"})
public class UserDaoTest {
    @Autowired
    private UserService userService;

    @Test //查询所有测试
    public void testFindAllUser(){
        List<User> user = userService.findAll();
        System.out.println(user);
    }
    @Test //查询测试
    public void testFindUser(){
        User user = userService.findUserById(1);
        System.out.println(user);
    }
    @Test //新增测试
    public void testAddUser(){
        for (int i = 2; i <=20 ; i++) {
            User user= new User();
            user.setId(i);
            user.setAge("18");
            user.setName("李四"+i);
            user.setSex("男");
            userService.saveUser(user);
            System.out.println(user);
        }
    }
}

测试结果,这里就给一个查询的结果如下:
这里写图片描述
ok,这里的测试是没有问题的,其他测试可以自行测试,都是ok的,这里就不粘贴了。到此,代码就差不多写完了。

接下来是bootscrap的前端技术了。
一:allUser.jsp代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    pageContext.setAttribute("path",request.getContextPath());
%>
<html>
<head>
    <title>所有用户查询</title>
    <link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h1>用户管理</h1>
        </div>
    </div>

    <div class="row">
        <div class="col-md-4 col-md-offset-8">
            <a class="btn btn-primary" href="/user/toAddUser">新增</a>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <table class="table table-hover table-striped">
                <tr>
                    <th>id</th>
                    <th>name</th>
                    <th>sex</th>
                    <th>age</th>
                    <th>操作</th>
                </tr>
                <c:forEach items="${userList}" var="user">
                    <tr>
                        <td>${user.id}</td>
                        <td>${user.name}</td>
                        <td>${user.sex}</td>
                        <td>${user.age}</td>
                        <td>
                            <a type="button"  href="${path}/user/toUpdate?id=${user.id}" class="btn btn-info btn-sm">
                                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                                编辑</a>
                            <a type="button"  href="${path}/user/delUser?id=${user.id}" class="btn btn-danger btn-sm">
                                <span class="glyphicon glyphicon-trash" aria-hidden="true" ></span>
                                删除</a>
                        </td>
                    </tr>

                </c:forEach>
            </table>
        </div>
    </div>
</div>

</body>
</html>

二:addUser.jsp代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户添加</title>
    <link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <style>
        .container{
            width: 400px;
        }
    </style>

</head>
<script>
    function addUser(){
        var form = document.forms[0];
        form.action = "addUser";
        form.method = "post";
        form.submit();
    }
</script>
allUser.jsp
<body>

<div class="container">
    <div class="row" >
        <div class="col-md-12">
            <form class="form-horizontal">
                <div class="form-group">
                    <label for="inputId" class="col-sm-2 control-label">ID</label>
                    <div class="col-sm-10">
                        <input type="text" name="id" class="form-control" id="inputId" placeholder="id">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputName" class="col-sm-2 control-label">name</label>
                    <div class="col-sm-10">
                        <input type="text"  name="name" class="form-control" id="inputName" placeholder="id">
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputSex" class="col-sm-2 control-label">sex</label>
                    <div class="col-sm-10">
                        <input type="text"  name="sex" class="form-control" id="inputSex" placeholder="id">
                    </div>
                </div>
                <div class="form-group">
                    <label for="age" class="col-sm-2 control-label">age</label>
                    <div class="col-sm-10">
                        <input type="text"  name="age" class="form-control" id="age" placeholder="id">
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button onclick="addUser()" type="submit" class="btn btn-success">添加</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

</body>
</html>

三:updateUser.jsp代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<script>
    alert(${user.id});
    function updateUser(){
        var form = document.forms[0];
        form.action = "<%=basePath%>user/updateUser";
        form.method = "post";
        form.submit();
    }
</script>
<head>
    <title>Title</title>
</head>
<body>
<h1>修改用户信息</h1>
<form action="${pageContext.request.contextPath}/user/getAllUser" method="post" name="userForm">
    <input value="${user.id}" name="id" type="hidden"/>
    name:<input type="text" name="name" value="${user.name}"><br>
    sex:<input type="text" name="sex" value="${user.sex}"><br>
    age:<input type="text" name="age" value="${user.age}"><br>
    <input type="submit" value="确认修改" onclick="updateUser()"> </input>
</form>
</body>
</html>

ok,全部代码写好了,在本地配置完tomcat后,运行项目即可。
访问地址:我这里的地址是:http://localhost:8080/user/getAllUser
查询所有的链接:http://localhost:8080/user/getAllUser
查询所有的结果如下图:
这里写图片描述

修改编辑链接:http://localhost:8080/user/toUpdate?id=1
这里写图片描述
新增链接:http://localhost:8080/user/toAddUser
这里写图片描述

Ok,整个流程至此结束!加油!也许看起来容易,但你自己操作还不一定能操作对。如果基础不牢固。这个对于新手来说,是一个不错的练习学习项目。
好了,小编我在这里了,也码了很久的代码与文字。如果对你有帮助,记得留言点赞,!我会接下来继续分享提供大家学习参考………..come on

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接出处:https://blog.csdn.net/qq_3076499,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 如果对您有帮助 ,请多多支持.多少都是您的心意与支持,一分也是爱,再次感谢!!! 支付宝赞赏:

这里写图片描述

记得点击下面的余额宝,红包可能要大些。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值