zoukankan      html  css  js  c++  java
  • SpringBootMVC01——A simple SpringBootMVC Sample

    不带数据库的SpringBootMVC案例

    1.创建一个SpringBoot项目,添加thymeleaf,webstarter

    2.目录层级

    3.启动器代码

    package com.littlepage;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SpringBoot04Application {
        public static void main(String[] args) {
            SpringApplication.run(SpringBoot04Application.class, args);
        }
    }

    4.Dao层代码

    package com.littlepage.dao;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.management.RuntimeErrorException;
    
    import org.springframework.stereotype.Repository;
    
    import com.littlepage.domain.City;
    
    @Repository
    public class CityDao {
    
        /**
         * 在内存中虚拟出一份数据
         * @return
         */
        static Map<Integer, City> dataMap=Collections.synchronizedMap(new HashMap<>());
        
        public List<City> findAll(){
            return new ArrayList<City>(dataMap.values());
        }
        
        public void save(City city) throws Exception {
            if(dataMap.get(city.getId())==null) {
                dataMap.put(city.getId(), city);
            }else {
                throw new RuntimeException("数据已经存在");
            }
        }
    }

    5.service层代码

    package com.littlepage.service;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.littlepage.dao.CityDao;
    import com.littlepage.domain.City;
    
    @Service
    public class CityService {
        
        @Autowired
        CityDao cityDao;
    
        public List<City> findAll(){
            return cityDao.findAll();
        }
        
        public String add(Integer id,String name) {
            try {
                cityDao.save(new City(id, name));
                return "保存成功";
            } catch (Exception e) {
                return "保存失败";
            }
        }
    }

    6.controller层代码

    package com.littlepage.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import com.littlepage.dao.CityDao;
    import com.littlepage.domain.City;
    import com.littlepage.service.CityService;
    
    @Controller
    @RequestMapping("city")
    public class CityController {
    
        @Autowired
        CityService citySrv;
        
        @RequestMapping("/list")
        public String list(Model model) {
            List<City> list=citySrv.findAll();
            model.addAttribute("list",list);return "list";
        }
        
        @RequestMapping("/add")
        public String add(@RequestParam("id") Integer id,@RequestParam("name") String name,Model model) {
            String success=citySrv.add(id, name);
            model.addAttribute("success",success);
            return "add";
        }
        
        @RequestMapping("/addPage")
        public String addPage() {
            return "addPage";
        }
    
    }

    7.三个页面

    list

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>list</title>
    </head>
    <body>
        <table>
            <tr>
                <th>ID</th>
                <th>Name</th>
            </tr>
            <tr th:each="city:${list}">
                <td th:text="${city.id}"></td>
                <td th:text="${city.name}"></td>
            </tr>
        </table>
    </body>
    </html>

    add

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1 th:text="${success}"></h1>
    </body>
    </html>

    addPage

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>add</title>
    </head>
        <body>
            <form action="add" method="post" >
            id:<input name="id" type="text" ><br>
            name:<input name="name" type="text"><br>
            <button type="submit">提交</button>
            </form>
        </body>
    </html>

    实现功能:基于内存进行增加和显示页面效果

  • 相关阅读:
    带箭头提示框
    文本溢出显示省略号
    Git高级操作
    sublime text 2 破解
    python如何画三维图像?
    pytorch梯度下降法讲解(非常详细)
    pytorch数学运算与统计属性入门(非常易懂)
    pytorch张量数据索引切片与维度变换操作大全(非常全)
    pytorch中tensor张量数据基础入门
    pytorch深度学习神经网络实现手写字体识别
  • 原文地址:https://www.cnblogs.com/littlepage/p/11060067.html
Copyright © 2011-2022 走看看