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实体: ...
随机推荐
- js 怎样获取div 图片等的宽度,只要值,不要px
给你的div命名id=“abc”,js中用下面的语句就能获取到js的宽度并赋值给a a=document.all.abc.offsetWidth; 然后通过 alert(a); 就能弹出这个值来了.
- 怎么从sqlserver的存储过程获得返回的数据
1.返回一个数值 declare @count int exec @count = testReturn \'111\',\'222\' select @count @count就是返回的数值是int ...
- SharePoint的安装和配置-PowerShell
1. 引入SPModule组件 Import-Module SPModule.misc Import-Module SPModule.setup 需要将执行策略修改为不限制 2. 无人值守安装Shar ...
- doom启示录
半个小时之后,doom的最后一个字节抵达威斯康星大学,瞬间,上万名玩家涌向那台服务器,淹没了她,威斯康星大学的服务器瘫痪了,大卫的服务器崩溃了. “天哪”大卫在电话里结结巴巴地对杰伊说:“我还从没见过 ...
- eclipse集成svn后总是弹出 Password Required问题解决方法
最近在集成svn后,在打开eclipse后总是一遍遍的弹出 Password Required,即使输入正确的用户名以及密码也会弹出,最后发现是eclipse的Network Connections ...
- [追加评论]三款SDR平台对比:HackRF,bladeRF和USRP
这三个月,有幸把3种板子都用到了.说说使用体会. 我用过其中的HackRF,bladeRF x115,USRP B210.我并没有仔细的测量各种板子的射频指标什么的,只是做各种实验的时候用到它们. ...
- ping命令技巧详解 windows下ping命令知识大全
windows ping命令对于多数电脑爱好者都不会陌生,通过ping ip可以知道网络是否畅通或者网络传输质量如何等,是网络技术人员常用的检测网络命令,多数朋友对ping命令知道的并不多,接下来本文 ...
- Pycharm出现的部分快捷键无效问题及解决办法
为了进行python开发,下载了Pycharm.但是发现启动后,执行ctrl+c和ctrl+v等快捷键都无法生效. 网上搜索了下,参考https://blog.csdn.net/c2366994582 ...
- IPM简介
1.IPM包含3个函数. image2ground:将图像中的像素点(u, v)对应到地平面上(Z=1)IPM的像素点(x, y): ground2image:将IPM中的像素点(x, y)基于IPM ...
- 2243: [SDOI2011]染色(LCT)
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 10909 Solved: 4216[Submit][Statu ...