不带数据库的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. Oracle9i的详细安装与卸载步骤(有图解)

       Oracle9i的安装和卸载详解      本章将以Windows操作系统为例讲述Oracle9i数据库的安装                                           ...

  2. 万变的Web,不变的CRUD

    用JSP+Servlet写程序,到Struts,Spring,hibernate写程序,到现在Spring Cloud分布式写程序,到底有多大区别,是不是还在写CRUD? 看着JD上各种要求,简直是S ...

  3. golang 使用reflect反射结构体

    "反射结构体"是指在程序执行时,遍历结构体中的字段以及方法. 1.反射结构体 下面使用一个简单的例子说明如何反射结构体. 定义一个结构体,包括3个字段,以及一个方法. 通过refl ...

  4. 使用bloomfilter

    package bloom; /** * 项目名:SpiderCrawler * 文件名:BloomFilterTest.java * 作者:zhouyh * 时间:2014-8-29 下午02:54 ...

  5. Jmeter之乱码 (一)

    Jmeter历史版本下载: http://archive.apache.org/dist/jmeter/binaries/ Jmeter3.0接口测试脚本POST请求主体中的中文无法正确显示,现象如下 ...

  6. UniEAP Platform V5.0建库

    create tablespace platform datafile 'platform.dbf' size 100M reuse autoextend on next 50M; . . drop ...

  7. Cocos2d-X多线程(3) cocos2dx中的线程安全

    在使用多线程时,总会遇到线程安全的问题.cocos2dx 3.0系列中新加入了一个专门处理线程安全的函数performFunctionInCocosThread(),他是Scheduler类的一个成员 ...

  8. P2077 【红绿灯】

    我tm真是想不出来还有什么好玩的东西了~~ 这题是一道纯模拟题,只需要用一个变量表示当前汽车行驶了多少分钟 不难发现,这个神奇的变量可以直接用m表示,还可以省去一个变量...(好像并没有什么卵用) 具 ...

  9. Mac OS X 11中的/usr/bin 的“Operation not permitted”

    更新了 Mac OS X 11后发现,MacVim 不再能够通过Terminal用命令打开了. mvim hello.txt 于是尝试将 mvim 重新复制到/usr/bin/中去 sudo cp - ...

  10. [Python3] 026 常用模块 calendar

    目录 calendar 1. calendar.calendar(year, w, l, c, m) 2. calendar.prcal(year, w, l, c, m) 3. calendar.m ...