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 ...
随机推荐
- leetcode 714. 买卖股票的最佳时机含手续费
继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式 class Solution { public: int maxProf ...
- 多变量线性回归时使用梯度下降(Gradient Descent)求最小值的注意事项
梯度下降是回归问题中求cost function最小值的有效方法,对大数据量的训练集而言,其效果要 好于非迭代的normal equation方法. 在将其用于多变量回归时,有两个问题要注意,否则会导 ...
- nodejs之mongodb操作
声明: 当查询到数据库数据后,对数据库数据进行遍历,可以采用toArray()函数,具体实现可以看第六点 1.本地安装mongodb 安装包:https://www.mongodb.com/downl ...
- Selenium 2自动化测试实战4(引用模块)
一.模组1.模组也叫类库或模块,引用模块 在python中,通过import….或from….import….的方式引用模块,下面引用time模块 import time print (time.ct ...
- 三十二:数据库之SQLAlchemy.query函数可查询的数据和聚合函数
准备工作 from sqlalchemy import create_engine, Column, Integer, String, Floatfrom sqlalchemy.ext.declara ...
- 人事中的BP是什么意思?
BP= business partner,指业务伙伴 HR=human resources,指人力资源 HRBP就是人力资源服务经理.主要工作内容是负责公司的人力资源管理政策体系.制度规范在各业务单元 ...
- Web测试方法_01
一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符号.禁止直接输入特殊字符时,使 ...
- cocos2dx[3.2](5) 屏幕适配
1.两个分辨率 1.1.窗口分辨率 在AppDelegate.cpp中有个设置窗口分辨率的函数.该函数是设置了我们预想设备的屏幕大小,也就是应用程序窗口的大小. // glView->setFr ...
- PHP 按照时区获取当前时间
/** * 时间格式化 * @param string $dateformat 时间格式 * @param int $timestamp 时间戳 * @param int $timeoffse ...
- 【VS开发】Caffelib中出现的问题:强制链接静态库所有符号(包括未被使用的)
C++程序在链接一个静态库时,如果该静态库里的某些方法没有任何地方调用到,最终这些没有被调用到的方法或变量将会被丢弃掉,不会被链接到目标程序中.这样做大大减小生成二进制文件的体积.但是,某些时候,即使 ...