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实体: ...
随机推荐
- VS2003在win7 64位的调试
win7 64位下安装了VS2003 ,在调试时,一直加载不了W3P.解决方案是.打开VS时,右键已管理员身份打开,即可调试.
- 不同数据库的driverClassName与url
# Properties file with JDBC-related settings. ########## # HSQLDB # ########## #jdbc.driverClassName ...
- juery 安全加固 集合
来源 jquery升级坑 2 3 4 5 版本 相关源码分享 新建document jquery ajax使用说明 最近在iteye的新闻中看到jQuery已经更新到了1.6.1. 和 ...
- bg-script 错误信息显示,以及global
chrome.developerPrivate.openDevTools({ renderViewId: -1, renderProcessId: -1, extensionId: chrome.ru ...
- vue-cli 添加less 以及sass
1.sasscnpm i node-sass --save-dev cnpm i sass-loader --save-dev2.less npm install less --save-dev np ...
- Hadoop streaming使用自定义python版本和第三方库
在使用Hadoop的过程中,遇到了自带python版本比较老的问题. 下面以python3.7为例,演示如何在hadoop上使用自定义的python版本以及第三方库. 1.在https://www.p ...
- Hign-Speed Tracking with Kernelzied Correlation Filters
reference:Hign-Speed Tracking with Kernelzied Correlation Filters questions: The core componet of mo ...
- LIBCURL踩坑记
这里一个java程序员进行C++开发使用libcurl踩过的坑: 1. 发送指定请求类型body,比较通用方法如下,可以直接填写数据并手动指定content type,如果是form表单等形式,则需 ...
- 判断颜色信息-RGB2HSV(opencv)
前言 项目车号识别过程中,车体有三种颜色黑车黑底白字.红车红底白字.绿车黄底绿字,可以通过判断车体的颜色信息,从而判断二值化是否需要反转,主要是基于rgb2hsv函数进行不同颜色的阈值判断. matl ...
- [LeetCode&Python] Problem 637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...