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>
实现功能:基于内存进行增加和显示页面效果
SpringBootMVC01——A simple SpringBootMVC Sample的更多相关文章
- 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 5 The accuracy of simple random samples
Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
- Server-Side UI Automation Provider - WinForm Sample
Server-Side UI Automation Provider - WinForm Sample 2014-09-14 源代码 目录 引用程序集提供程序接口公开服务器端 UI 自动化提供程序从 ...
- spring boot源码分析之SpringApplication
spring boot提供了sample程序,学习spring boot之前先跑一个最简单的示例: /* * Copyright 2012-2016 the original author or au ...
- ANNOTATION PROCESSING 101 by Hannes Dorfmann — 10 Jan 2015
原文地址:http://hannesdorfmann.com/annotation-processing/annotationprocessing101 In this blog entry I wo ...
- A quick introduction to HTML
w3c reference : https://www.w3.org/TR/2014/REC-html5-20141028/introduction.html#writing-secure-appli ...
- 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Final
Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
- 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Midterm
Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
- 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 2 Random sampling with and without replacement
Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
- 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: FINAL
Stat2.3x Inference(统计推断)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
随机推荐
- Gson解析list类型的json串
Gson gson = new Gson(); Type type = new TypeToken<List<Object>>() {}.getType(); List< ...
- linux (一)
一.文件目录 cd 目录进出 pwd 查看路径 ls:ll 查看目录 mkdir : mkdir -p app/test 创建目录 touch 创建文件 rm :rm -r 递归 -f 强制 mv ...
- 说说 Activity、Intent、Service 是什么关系
他们都是 Android 开发中使用频率最高的类.其中 Activity 和 Service 都是 Android 四大组件之一.他俩都是Context 类的子类 ContextWrapper 的子类 ...
- fiddler配置会话框菜单栏
1.添加会话框菜单:鼠标移至会话框菜单的左上角“#”位置,右键>Customize column,Collection下拉菜单选择"Miscellaneous",Field ...
- nodejs 框架 中文express 4.xxx中文API手册
介于最近express 中文文档比较难找的现状,特地找了一个,供大家学习思考 Express 4.x API express 翻译 api文档 中文 -- express() expre ...
- 去除雨滴的滤镜 Derain in FFmpeg
Remove the rain in the input image/video by applying the derain methods based on convolutional neura ...
- ffmpeg 视频过度滤镜 gltransition
ffmpeg 视频过度滤镜 gltransition 上次随笔中提到的 ffmpeg-concat 可以处理视频过度,但是缺点是临时文件超大. 经过查找 ffmpeg 还有 gltransition ...
- 基于 vue2 + vuex 构建一个具有 45 个页面的大型单页面应用
源码地址: https://github.com/bailicangdu/vue2-elm 技术栈 vue2 + vuex + vue-router + webpack + ES6/7 + fetch ...
- 【HANA系列】SAP Vora(SAP HANA和Hadoop)简析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP Vora(SAP HAN ...
- LeetCode.937-重新排序日志数组(Reorder Log Files)
这是悦乐书的第358次更新,第385篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第220题(顺位题号是937).你有一系列日志.每个日志都是以空格分隔的单词串. 每个日 ...