spring boot学习(3) SpringBoot 之MVC 支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
com.cy.controller.HelloWorldFreemarkerController.java:
package com.cy.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/freemarker")
public class HelloWorldFreemarkerController { @RequestMapping("/say")
public ModelAndView say(){
ModelAndView mav=new ModelAndView();
mav.addObject("message", "springboot你好!");
mav.setViewName("helloWorld");
return mav;
} }
freemarker模板是ftl为后缀的;src/main/resources/templates/helloWorld.ftl:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
show: ${message}
</body>
</html>
浏览器http://localhost:8888/HelloWorld/freemarker/say:显示:show: springboot你好!
例子如下:
com.cy.controller.HelloWorldAjaxController.java:
package com.cy.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/ajax")
public class HelloWorldAjaxController { @RequestMapping("/hello")
public String say(){
return "{'message1':'SpringBoot你好','message2','Spring你好2'}";
}
}
com.cy.controller.BlogController.java:
package com.cy.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/blog")
public class BlogController { @RequestMapping("/{id}")
public ModelAndView show(@PathVariable("id") Integer id){
ModelAndView mav=new ModelAndView();
mav.addObject("id", id);
mav.setViewName("blog");
return mav;
} @RequestMapping("/query")
public ModelAndView query(@RequestParam(value="q",required=false) String q){
ModelAndView mav=new ModelAndView();
mav.addObject("q", q);
mav.setViewName("query");
return mav;
} }
webapp/index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="resources/jquery/jquery.min.js"></script>
<script type="text/javascript"> function show(){
$.post("ajax/hello",{},function(result){
alert(result);
});
} </script>
</head>
<body>
<button onclick="show()">点击</button>
<a href="/HelloWorld/blog/21">博客</a>
<a href="/HelloWorld/blog/query?q=123456">搜索</a>
</body>
</html>
src/main/resoureces/templates/blog.ftl:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
博客id:${id}
</body>
</html>
src/main/resources/templates/query.ftl:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
q: ${q}
</body>
</html>
测试:
浏览器:http://localhost:8888/HelloWorld/index.html
点击按钮:alert: {'message1':'SpringBoot你好','message2','Spring你好2'}
点击博客链接:转发页面,显示博客id:21
点击搜索,到页面http://localhost:8888/HelloWorld/blog/query?q=123456,显示q: 123456
spring boot学习(3) SpringBoot 之MVC 支持的更多相关文章
- spring boot学习(4) SpringBoot 之Spring Data Jpa 支持(1)
第一节:Spring Data Jpa 简介 Spring-Data-Jpa JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate ...
- spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid
SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...
- spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)
SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...
- spring boot 学习(十)SpringBoot配置发送Email
SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...
- 【Spring Boot学习之九】缓存支持
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.Spring Boot Cache以及整合EhCacheSpring从3.1开始定义了org.springfram ...
- spring boot学习(2) SpringBoot 项目属性配置
第一节:项目内置属性 application.properties配置整个项目的,相当于以前的web.xml: 注意到上一节的访问HelloWorld时,项目路径也没有加:直接是http://loca ...
- spring boot学习(5) SpringBoot 之Spring Data Jpa 支持(2)
第三节:自定义查询@Query 有时候复杂sql使用hql方式无法查询,这时候使用本地查询,使用原生sql的方式: 第四节:动态查询Specification 使用 什么时候用呢?比如搜索有很多条 ...
- spring boot 学习笔记(二) 构建web支持jsp
一.必须将项目打包成war包 <packaging>war</packaging> 二.pom.xml加入依赖包 <dependency> <groupId& ...
- spring boot学习(7) SpringBoot 之表单验证
第一节:SpringBoot 之表单验证@Valid 是spring-data-jpa的功能: 下面是添加学生的信息例子,要求姓名不能为空,年龄大于18岁. 贴下代码吧: Student实体: ...
随机推荐
- Python Django 之 Template 模板语言简介
一.什么事模板语言 html+逻辑控制语句 二.模板语言的作用 帮助前端处理后端发来的数据,方便前端展示(杂糅渲染) 三.模板语言语法 1.{{变量}} 变量使用双大括号{{}} 2.万能的句点号. ...
- Android Touch事件之一:Touch事件在父ViewGroup和子View之间的传递篇
2015-11-26 17:00:22 前言:Android的Touch事件传递和View的实现紧密相连,因此理解Touch事件的传递,有助于我们更好的理解View的工作原理. 1. 几个重要的方法: ...
- Excel根据人名匹配得到编号
操作步骤:输入公式 =IF(COUNTIF($E$2:$E2,$E2)>COUNTIF($B:$B,$E2),"",INDEX(C:C,SMALL(IF($B$1:$B$10 ...
- 安装sybase服务器并连接数据库
一. 安装sybase服务器(根据安装指南安装) win32位的系统安装1550_winx86_32+补丁包eBF18157 win64位的系统安装ase157_winx64_安装包 如果不创建服务器 ...
- avalon 路由问题
1, 直接使用avalon的 amd加载器, 可以不需要 require.js 2, 配置baseUrl 路径, 这个一定要在 js所在的目录, 而不是jsp所在的目录, 如果js 和jsp分开 ...
- pdo::quey excu excute 区别
PDO::query(PHP环境下同)和PDOStatement::execute函数均能实现SELECT查询功能,但官方文档并未见对此设计初衷的说明,此外还有个PDO::exec函数功能也很类似.天 ...
- C++语言的一些问题
论坛上看到的有人提出的关于C++的一些问题,真是细致(下面还有回复说他对C++一知半解的),一直以为自己的C++还是可以的,可是看了之后真是内牛满面,为什么自己从来没有想的这么深入,差距真的有这么大吗 ...
- ADB命令行工具使用
Putty工具连接Android设备 下载链接:https://github.com/sztupy/adbputty/downloads 如上图所示:在HostName中输入transport-usb ...
- C语言基础:初级指针 分类: iOS学习 c语言基础 2015-06-10 21:50 30人阅读 评论(0) 收藏
指针:就是地址. & 取地址运算符 %p 打印地址占位符 int a=0; printf("%p ",&a); 指针变量:用来存放地址的变量 定义: ...
- Mac 活动监视器 闪退 pro发热耗电过快问题解决,亲测可用解决
该解决办法转载 Mac 活动监视器 闪退 pro发热耗电过快问题解决 这个月新买了mac,升级了系统,出现CPU发热,高负荷运转问题,始终找不到问题解决办法, ,这个过程太痛苦了,也不知道是什么原因. ...