不带数据库的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>

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

SpringBootMVC01——A simple SpringBootMVC Sample的更多相关文章

  1. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 5 The accuracy of simple random samples

    Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  2. Server-Side UI Automation Provider - WinForm Sample

    Server-Side UI Automation Provider - WinForm Sample 2014-09-14 源代码  目录 引用程序集提供程序接口公开服务器端 UI 自动化提供程序从 ...

  3. spring boot源码分析之SpringApplication

    spring boot提供了sample程序,学习spring boot之前先跑一个最简单的示例: /* * Copyright 2012-2016 the original author or au ...

  4. ANNOTATION PROCESSING 101 by Hannes Dorfmann — 10 Jan 2015

    原文地址:http://hannesdorfmann.com/annotation-processing/annotationprocessing101 In this blog entry I wo ...

  5. A quick introduction to HTML

    w3c reference : https://www.w3.org/TR/2014/REC-html5-20141028/introduction.html#writing-secure-appli ...

  6. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Final

    Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  7. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Midterm

    Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  8. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 2 Random sampling with and without replacement

    Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  9. 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: FINAL

    Stat2.3x Inference(统计推断)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

随机推荐

  1. 非均匀B样条离散点的加密与平滑

    非均匀B样条离散点的加密与平滑 离散点的预处理是点云网格化很关键的一步,主要就是离散点的平滑.孔洞修补:本文是基于非均匀B样条基函数进行离散点云的加密和平滑的,一下为初步实现结果. 算法步骤: 1.数 ...

  2. webDriver各版本对应

    chromeDriver http://npm.taobao.org/mirrors/chromedriver/ http://chromedriver.storage.googleapis.com/ ...

  3. 为什么重写了equals(),还要重写hashCode()?

    解决这个问题得先明白:hashCode 方法用于散列集合的查找,equals 方法用于判断两个对象是否相等. 第一步:具体背景(没有背景的讨论就是在耍流氓) 以HashMap中put方法为背景 第二步 ...

  4. 全面解读php-开发环境及配置

    一.版本控制软件 1.集中式 (CVS和 SVN) 中间有一个中央服务器,所有的客户机都会把版本信息上传到中央服务器里,版本信息只在中央服务器里保存,当我们去上传或者下载的时候 ,都是从中央服务器来连 ...

  5. Dark 类与对象

    1 类的声明 使用class声明一个类 使用new 创建一个对象,new可省略 dark文件名和声明的类名可以不一样,与java不同 void main() { var function = Pers ...

  6. MSTest/NUnit 单元测试 代码覆盖率试用 OpenCover 和ReportGenerator

    VS自带是单元测试代码覆盖率(VS自带这个是最佳选择)需要企业版才有.很蛋疼...... 1.下载安装OpenCover 和ReportGenerator. 关于这2个是干啥的百度下.简单说就是可以分 ...

  7. 浏览器端-W3School-HTML:HTML DOM Area 对象

    ylbtech-浏览器端-W3School-HTML:HTML DOM Area 对象 1.返回顶部 1. HTML DOM Area 对象 Area 对象 Area 对象代表图像映射的一个区域(图像 ...

  8. 第六章 SpringCloud之Ribbon负载均衡

    ###################使用默认的负载均衡(轮询)############################# 1.pom.xml <?xml version="1.0&q ...

  9. 创建策略(Creation Policy )和生命周期(Life Cycle)

    前言 在前面的介绍中我们已经知道:导入和导出的匹配成功需要ContractType,ContractName,Metadata都匹配,这里我们还要介绍一个新的东西:创建策略(creation poli ...

  10. Object Creation

    Although using the object constructor or an object literal are convenient ways to create single obje ...