spring mvc hibernate spring 整合的增删改查+后台校验+bootstrap

整合之前先知道大概的思路,首先要知道每个框架的重点要点。

1.首先我们从数据库开始

--创建数据库
create database gs

--创建表
create table food
(
    id int identity(1,1) primary key,
    name varchar(50) not null,
    describe varchar(150) not null,
    productionaddress varchar(100) not null
)
select * from food
--添加数据
insert into food values('好丽友','交个好朋友','广东')
insert into food values('卫龙','这种辣,爽啊','广东珠海')

 2.项目依赖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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lch</groupId>
    <artifactId>springmvc-JDBC</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!--切入点表达式需要用的包,-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.10.Final</version>
        </dependency>

        <!--数据库-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>6.2.1.jre8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!--连接池-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!--er表达式-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!--校验-->
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.2.Final</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <warSourceDirectory>web</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
View Code

3.创建entity层

FoodEntity

package com.entity;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;

public class FoodEntity {

    private Integer id;
    @Size(min = 3,max = 30,message = "亲,您输入的范围必须在2-30之间")
    private String name;

    @NotEmpty(message = "亲,商品描述不能为空")
    private String describe;

    @NotEmpty(message = "亲,生产地址不能为空")
    private String productionaddress;

    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 getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    public String getProductionaddress() {
        return productionaddress;
    }

    public void setProductionaddress(String productionaddress) {
        this.productionaddress = productionaddress;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        FoodEntity that = (FoodEntity) o;

        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;
        if (describe != null ? !describe.equals(that.describe) : that.describe != null) return false;
        if (productionaddress != null ? !productionaddress.equals(that.productionaddress) : that.productionaddress != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (describe != null ? describe.hashCode() : 0);
        result = 31 * result + (productionaddress != null ? productionaddress.hashCode() : 0);
        return result;
    }
}
View Code

3.1实体的映射文件(因为借助idea开发工具,手动生成这个表的实体,连FoodEntity.hbm.xml ,也生成到entity包里面,可能导致扫描不到,然后把这个文件,放在resources里面的mappers文件夹里面,以后有多个文件就可以方便管理)

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.entity.FoodEntity" table="food" schema="dbo" catalog="gs">
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
        <property name="name" column="name"/>
        <property name="describe" column="describe"/>
        <property name="productionaddress" column="productionaddress"/>
    </class>
</hibernate-mapping>
View Code

4.dao层

4.1 搞一个BaseDao(在这里创建SessionFactory

package com.dao;

import org.hibernate.SessionFactory;

public class BaseDao {

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}
View Code

4.2  FoodDao

package com.dao;

import com.entity.FoodEntity;
import org.hibernate.Session;

import java.util.List;

public class FoodDao extends BaseDao{

    //查询
    public List<FoodEntity> queryFood(){
        Session session = getSessionFactory().getCurrentSession();

        return session.createQuery("from FoodEntity").list();
    }

    //修改之前查询要修改的ID
    public FoodEntity queryId(int id){
        Session session = getSessionFactory().getCurrentSession();

        FoodEntity teacherEntity=session.get(FoodEntity.class,id);

        return  teacherEntity;
    }

    //修改
    public void updateFood(FoodEntity foodEntity){
        Session session = getSessionFactory().getCurrentSession();

        session.update(foodEntity);
    }

    //添加
    public void insertFood(FoodEntity foodEntity){
        Session session= getSessionFactory().getCurrentSession();

        session.save(foodEntity);
    }

    //删除
    public void deleteFood(int id){
        Session session= getSessionFactory().getCurrentSession();

        FoodEntity foodEntity=session.get(FoodEntity.class,id);

       session.delete(foodEntity);
    }

}
View Code

5.service业务层,我这里没有什么业务处理,只是简单做一下JDBC操作(FoodService)

package com.service;


import com.dao.FoodDao;
import com.entity.FoodEntity;

import java.util.List;

public class FoodService {
    private FoodDao foodDao;


    public FoodDao getFoodDao() {
        return foodDao;
    }

    public void setFoodDao(FoodDao foodDao) {
        this.foodDao = foodDao;
    }
    
    //查询
    public List<FoodEntity> queryFood(){
        return foodDao.queryFood();
    }

    //修改之前获取ID
    public FoodEntity queryId(int id){

        return foodDao.queryId(id);
    }

    //修改
    public void updateFood(FoodEntity foodEntity){

        foodDao.updateFood(foodEntity);
    }


    //添加
    public void insertFood(FoodEntity foodEntity){
       foodDao.insertFood(foodEntity);
    }

    //删除
    public void deleteFood(int id){

         foodDao.deleteFood(id);

    }
}
View Code

6.控制层(FoodAction)

package com.contorller;


import com.entity.FoodEntity;
import com.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Controller
@RequestMapping(path = "/foods")
public class FoodAction {
    //完成自动装配的工作
    @Autowired
    private FoodService foodService;

    public FoodService getFoodService() {
        return foodService;
    }

    public void setFoodService(FoodService foodService) {
        this.foodService = foodService;
    }
//这里有二种做法,把数据在页面显示
    //第一种做法
//    @GetMapping(value = "/food")
//    public String list(){
//        return "/list";
//    }
//    //设置到页面
//    @ModelAttribute("fd")
//    public List<FoodEntity> queryFood(){
//
//        return foodService.queryFood();
//    }

    //第二种做法

    @GetMapping(value = "/food")
    public String list(Model model){
        List<FoodEntity> foodEntityList = foodService.queryFood();

        model.addAttribute("fd",foodEntityList);

        return "/list";
    }


    //获取修改的ID
    @GetMapping(value = "/queryId")
    public String queryId(@RequestParam(value = "id") int id,Model model){

        model.addAttribute("fd",foodService.queryId(id));

       return "/edit";
    }

    //修改
    @PostMapping(value = "/updateFood")
    public String updateFood(@Valid FoodEntity foodEntity, BindingResult bindingResult,Model model){

        if(bindingResult.hasErrors()){
            for(int i=0;i<bindingResult.getFieldErrors().size();i++){
                String name=bindingResult.getFieldErrors().get(i).getField();
                String err=bindingResult.getFieldErrors().get(i).getDefaultMessage();
                model.addAttribute(name+"_err",err);
            }

            model.addAttribute("fd",foodEntity);
            return "/edit";

        }else {

            foodService.updateFood(foodEntity);

            return "redirect:/foods/food";
        }


    }


    //删除
    @GetMapping(value = "/deleteId")
    public String deleteFood(@RequestParam(value = "id") int id){

        foodService.deleteFood(id);

        return "redirect:/foods/food";
    }

    //跳转到添加页面
    @GetMapping(value = "/insert")
    public String insertredirect(){

            return "/add";
    }

    //做真正的添加操作操作
    @PostMapping(value = "/saveFood")
    public String insertTeacher(@Valid FoodEntity foodEntity, BindingResult bindingResult,Model model){

        if(bindingResult.hasErrors()){
            for(int i=0;i<bindingResult.getFieldErrors().size();i++){
                String name=bindingResult.getFieldErrors().get(i).getField();
                String err=bindingResult.getFieldErrors().get(i).getDefaultMessage();

                model.addAttribute(name+"_err",err);

            }

            model.addAttribute("fd",foodEntity);
            return "/add";

        }else {


            foodService.insertFood(foodEntity);

            return "redirect:/foods/food";
        }


    }


}
View Code

7.配置文件

7.1 spring mvc文件的配置(spring-mvc.xml)

 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.xsd">

 <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package="com">
    </context:component-scan>

    <!-- 开启注解 -->
    <mvc:annotation-driven />

  <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
    <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
    <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/><!--设置JSP文件的目录位置-->
        <property name="suffix" value=".jsp"/>
    </bean>


</beans>
View Code

7.2 db.properties文件的配置(外部引入数据库连接)我这里用的是SQL2008数据库

driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;database=gs
username=sa
password=12223

 7.3 spring文件的配置(applicationContext.xml)

applicationContext.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置属性持有对象 -->
    <!--外部导进连接数据库 driverClassName url username password-->
    <context:property-placeholder location="classpath:db.properties" local-override="true"/>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">

        <property name="driverClassName" value="${driverClassName}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>

        <!---->
        <property name="initialSize" value="5"></property>
        <property name="maxIdle" value="10"></property>
        <property name="maxTotal" value="10"></property>
    </bean>

    <!-- 配置Hibernate的SessionFactory,无hibernate配置文件 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" destroy-method="destroy">
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置hibernate属性-->
        <property name="hibernateProperties">
            <props>
                <!-- 数据库的方言 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>
                <!-- 是否显示SQL语句 -->
                <!-- 是否格式化显示SQL语句 -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <!-- 配置映射文件 -->
        <property name="mappingLocations">
            <list>
                <value>classpath:mappers/FoodEntity.hbm.xml</value>

            </list>
        </property>

    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!--配置事务通知-->
    <tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 事务管理策略,查为只读 -->
            <tx:method name="query*" read-only="true"/>
            <tx:method name="insert*"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
        </tx:attributes>
    </tx:advice>


    <!--配置事务AOP-->
    <aop:config>
        <aop:pointcut id="query" expression="execution(public * com.service.*.*(..))"/>
        <aop:advisor advice-ref="transactionInterceptor" pointcut-ref="query"></aop:advisor>
    </aop:config>

    <!-- 配置客户持久层bean dao-->
    <bean id="baseDao" abstract="true" class="com.dao.BaseDao">
        <!-- 该bean继承了ProductDao,则可以直接注入sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="foodDao" class="com.dao.FoodDao" parent="baseDao"></bean>

    <!-- 配置客户服务bean -->
    <bean id="foodService" class="com.service.FoodService">
        <property name="foodDao" ref="foodDao"></property>
    </bean>

    <bean id="action" class="com.contorller.FoodAction">
        <property name="foodService" ref="foodService"></property>
    </bean>

</beans>
View Code

 7.3 web的文件配置

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">

    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--spring mvc配置-->
        <!-- 加载spring mvc配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>


    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--spring配置文件的路径-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
View Code

8.页面

8.1 index页面(list.jsp)

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/1/31
  Time: 9:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>index</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>

        #fieldset{
            margin: 100px 100px;
            border: 1px solid dodgerblue;
            border-radius: 15px;

        }
    </style>

    <script>

    </script>
</head>
<body>

<fieldset id="fieldset">
    <legend>产品信息</legend>

    <table class="table table-striped">
        <thead>
        <tr align="center">
            <th>编号</th>
            <th>名称</th>
            <th>描述</th>
            <th>产地</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach  var="food" items="${fd}">
            <tr>
                <td>${food.id}</td>
                <td>${food.name}</td>
                <td>${food.describe}</td>
                <td>${food.productionaddress}</td>
                <td>
                    <button type="button" class="btn btn-danger glyphicon glyphicon-remove">
                      <a href="/foods/updateId?id=${food.id}" onclick="return confirm('are you sure?')">删除</a>
                    </button>
                    <button type="button" class="btn btn-warning glyphicon glyphicon-pencil">
                      <a href="/foods/queryId?id=${food.id}" >修改</a>
                    </button>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
    <button type="button" class="btn" style="background: dodgerblue">
        <a href="/foods/insert" style="color: white;font-size: 18px ;text-align: center">添加</a>
    </button>
</fieldset>

    <!-- HTML5 Shiv 和 Respond.js 用于让 IE8 支持 HTML5元素和媒体查询 -->
    <!-- 注意: 如果通过 file://  引入 Respond.js 文件,则该文件无法起效果 -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
    <!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
    <script src="https://code.jquery.com/jquery.js"></script>
    <!-- 包括所有已编译的插件 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
View Code

8.2 添加页面(add.jsp)包括校验

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/1/31
  Time: 13:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>add</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <style>
        #fieldset{
            margin: 100px 100px;
            border: 1px solid dodgerblue;
            width: 800px;
            border-radius: 15px;

        }
    </style>
</head>
<body>
<h2>添加页面</h2>
<%--<form action="/foods/saveFood" method="post">--%>
    <%--名称<input type="text"  name="name"><p></p>--%>
    <%--描述<input type="text"  name="describe"><p></p>--%>
    <%--地址<input type="text"  name="productionaddress"><p></p>--%>
    <%--<input type="submit" value="确定" ><p></p>--%>
<%--</form>--%>

<fieldset id="fieldset">
    <legend>添加商品</legend>

<form action="/foods/saveFood" method="post">
    <div class="form-group">
        <label for="name">名称</label>
        <input type="text" class="form-control" id="name" name="name" value="${fd.name}" placeholder="name">
        <p style="color: red">${name_err}</p>
    </div>
    <div class="form-group">
        <label for="describe">描述</label>
        <input type="text" class="form-control" id="describe" name="describe"  value="${fd.describe}" placeholder="describe">
        <p style="color: red">${describe_err}</p>
    </div>
    <div class="form-group">
        <label for="describe">地址</label>
        <input type="text" class="form-control" id="productionaddress" name="productionaddress"  value="${fd.productionaddress}" placeholder="productionaddress">
        <p style="color: red">${productionaddress_err}</p>
    </div>

    <button type="submit" class="btn btn-default">添加</button>
</form>
</fieldset>
<script src="https://code.jquery.com/jquery.js"></script>
<!-- 包括所有已编译的插件 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
View Code

结果:

8.3 修改页面(edit.jsp) 

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/1/31
  Time: 13:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>update</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <style>
    #fieldset{
        margin: 100px 100px;
        border: 1px solid dodgerblue;
        width: 800px;
        border-radius: 15px;

    }
    </style>

</head>
<body>

<%--<form action="/foods/update" method="post">--%>
    <%--编号:<input type="text"  name="id" value="${fd.id}" readonly="readonly"><p></p>--%>
    <%--名称:<input type="text"  name="name" value="${fd.name}"><p></p>--%>
    <%--描述:<input type="text"  name="describe" value="${fd.describe}"><p></p>--%>
    <%--生产地址:<input type="text"  name="productionaddress" value="${fd.productionaddress}"><p></p>--%>
    <%--<input type="submit" value="确定" ><p></p>--%>
<%--</form>--%>


<fieldset id="fieldset">
    <legend>修改商品</legend>

<form action="/foods/updateFood" method="post">

    <div class="form-group">
        <label for="id">编号</label>
        <input type="text" class="form-control" id="id"  name="id" value="${fd.id}" readonly="readonly">
    </div>

    <div class="form-group">
        <label for="name">名称</label>
        <input type="text" class="form-control" id="name" name="name" value="${fd.name}">
      <p style="color: red">${name_err}</p>
    </div>
    <div class="form-group">
        <label for="describe">描述</label>
        <input type="text" class="form-control" id="describe"  name="describe" value="${fd.describe}">
        <p style="color: red">${describe_err}</p>
    </div>
    <div class="form-group">
        <label for="describe">地址</label>
        <input type="text" class="form-control" id="productionaddress" name="productionaddress" value="${fd.productionaddress}">
        <p style="color: red">${productionaddress_err}</p>
    </div>

    <button type="submit" class="btn btn-default">修改</button>
</form>
</fieldset>


<script src="https://code.jquery.com/jquery.js"></script>
<!-- 包括所有已编译的插件 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
View Code

 结果:

9. 项目结构

最后在浏览器打开:http://localhost:8080/foods/food

原文地址:https://www.cnblogs.com/LCH-M/p/8395273.html