先期上力量图吧. 本人菜鸟称帮派, 请勿喷.

澳门蒲京娱乐 1结缘前先行清楚大致的思路,首先要了解每个框架的重中之重要。

澳门蒲京娱乐 2

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('卫龙','这种辣,爽啊','广东珠海')

澳门蒲京娱乐 3澳门蒲京娱乐 4

 2.品种依赖jar包

  1 1 body{
  2   2     margin: 0;
  3   3     padding: 0;
  4   4     width: 100%;
  5   5     height: 100%;
  6   6 }
  7   7 
  8   8 .headers{
  9   9     width: 100%;
 10  10     height: 100px;
 11  11 }
 12  12 .siv-ng{
 13  13     width: 100%;
 14  14     height: 30px;
 15  15     border: 1px solid #ccc;
 16  16     background: #ccc;
 17  17 }
 18  18 
 19  19 .contents{
 20  20     width: 100%;
 21  21     height: 400px;
 22  22     position: relative;
 23  23 }
 24  24 .login{
 25  25     width: 400px;
 26  26     height: 400px;
 27  27     background: #A9B0A4;
 28  28     margin: 0 auto;
 29  29     border-radius: 5px;
 30  30     opacity: 0.8;
 31  31 }
 32  32 
 33  33 .login-img-top{
 34  34     height: 40px;
 35  35 }
 36  36 .login h1{
 37  37     text-align: center;
 38  38 }
 39  39 .form-text{
 40  40     width: 350px;
 41  41     height: 45px;
 42  42     border: 1px solid #ccc;
 43  43     background: white;
 44  44     margin: 0 auto;
 45  45     border-radius: 5px;
 46  46     margin-top: 20px;
 47  47     position: relative;
 48  48 }
 49  49 
 50  50 .login-img{
 51  51     width: 50px;
 52  52     height: 45px;
 53  53     float: left;
 54  54     margin-left: 8px;
 55  55 }
 56  56 
 57  57 .input-in{
 58  58     width: 290px;
 59  59     height: 45px;
 60  60     line-height: 45px;
 61  61     float: left;
 62  62     border: 0px;
 63  63     letter-spacing: 1px;
 64  64     font-size: 18px;
 65  65     color:gray;
 66  66 }
 67  67 
 68  68 .bt-login-div{
 69  69     width: 100%;
 70  70     height: 50px;
 71  71     margin-top: 20px; 
 72  72 }
 73  73 
 74  74 
 75  75 .submit{
 76  76     width: 100px;
 77  77     height: 40px;
 78  78     margin: 0 auto;
 79  79     text-align: center;
 80  80     line-height: 40px;
 81  81     border: 1px solid #ccc;
 82  82     border-radius: 5px;
 83  83     background: #0099FF;
 84  84     color: white;
 85  85 }
 86  86 
 87  87 .submit:hover{
 88  88     border: 1px solid #77DDC9;
 89  89     background: #0099FF;
 90  90     opacity: 0.9;
 91  91     cursor: hand;
 92  92 }
 93  93 
 94  94 
 95  95 .fogot{
 96  96     width: 100px;
 97  97     height: 40px;
 98  98     margin: 0 auto;
 99  99     text-align: center;
100 100 }
101 101 .fogot a{
102 102     color: white;
103 103     text-decoration-style: none;
104 104     text-decoration: #ccc;
105 105 }

澳门蒲京娱乐 5澳门蒲京娱乐 6

View Code

<?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层

澳门蒲京娱乐 7澳门蒲京娱乐 8

FoodEntity

 1 <html>
 2 <meta charset="utf-8" />
 3 <title>登陆</title>
 4 
 5 <head>
 6     <link href="css/login.css" rel="stylesheet" type="text/css" />
 7 </head>
 8 
 9 <body>
10     <div class="headers">
11         <div class="siv-ng">
12 
13         </div>
14     </div>
15     <div class="contents">
16         <div class="login">
17             <div class="login-img-top">
18 
19             </div>
20             <h1>
21                 XXXX后台管理系统V1.0
22             </h1>
23             <div>
24                 <form method="post">
25                     <div class="form-text">
26                         <div class="login-img">
27                             <img src="images/user-icon.png" width="40" height="40" style="margin-top:2px;">
28                         </div>
29                         <input class="input-in" value="" type="text" class="username">
30                     </div>
31                     <div class="form-text">
32                         <div class="login-img">
33                             <img src="images/lock-icon.png" width="40" height="40"  style="margin-top:2px;">
34                         </div>
35                         <input class="input-in" value="" type="password" class="password">
36                     </div>
37                     <div class="bt-login-div">
38                         <div class="submit">
39                             登陆
40                         </div>
41                     </div>
42                     <div class="bt-login-div">
43                             <div class="fogot">
44                                 <a href="#">忘记密码?</a>
45                             </div>
46                         </div>
47                 </form>
48             </div>
49         </div>
50     </div>
51     <div class="footer">
52 
53     </div>
54 </body>
55 
56 </html>

澳门蒲京娱乐 9澳门蒲京娱乐 10

View Code

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;
    }
}

只是做只记录而一度. 这个页面,断断续续的,大概做了1单小时这样吧. 

View Code

 

3.1实体的映照文件(因为依靠idea开发工具,手动生成这个发明底实业,连FoodEntity.hbm.xml
,也转移到entity包里面,可能致扫描不至,然后把这个文件,放在resources里面的mappers文件夹里面,以后产生差不多只公文就足以方便管理)

澳门蒲京娱乐 11澳门蒲京娱乐 12

<?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**)

澳门蒲京娱乐 13澳门蒲京娱乐 14

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

澳门蒲京娱乐 15澳门蒲京娱乐 16

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)

澳门蒲京娱乐 17澳门蒲京娱乐 18

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)

澳门蒲京娱乐 19澳门蒲京娱乐 20

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(文件)

澳门蒲京娱乐 21澳门蒲京娱乐 22

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

澳门蒲京娱乐 23澳门蒲京娱乐 24

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

澳门蒲京娱乐 25澳门蒲京娱乐 26

<?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)

澳门蒲京娱乐 27澳门蒲京娱乐 28

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

澳门蒲京娱乐 29

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

 

澳门蒲京娱乐 30澳门蒲京娱乐 31

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

 

结果:

澳门蒲京娱乐 32

**8.3
修改页面(edit.jsp)** 

澳门蒲京娱乐 33澳门蒲京娱乐 34

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

 结果:

澳门蒲京娱乐 35

9. 品种结构

澳门蒲京娱乐 36澳门蒲京娱乐 37

末了在浏览器打开:http://localhost:8080/foods/food

澳门蒲京娱乐 38

澳门蒲京娱乐 39