第一节:@RequestMapping 配置url 映射
 
第二节:@Controller 处理http 请求
转发到一个页面,以前是转发到jsp页面,现在使用freemarker;
在pom.xml页面右键,spring-edit starters , 添加freemarker支持:spring-boot-starter-freemarker
pom.xml:
      <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你好!

第三节:@RestController 处理ajax 请求
 
第四节:@PathVariable 获取url 参数
 
第五节:@RequestParam 获取请求参数

例子如下:

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 支持的更多相关文章

  1. spring boot学习(4) SpringBoot 之Spring Data Jpa 支持(1)

    第一节:Spring Data Jpa 简介 Spring-Data-Jpa JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate ...

  2. spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid

    SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...

  3. spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  4. spring boot 学习(十)SpringBoot配置发送Email

    SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...

  5. 【Spring Boot学习之九】缓存支持

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.Spring Boot Cache以及整合EhCacheSpring从3.1开始定义了org.springfram ...

  6. spring boot学习(2) SpringBoot 项目属性配置

    第一节:项目内置属性 application.properties配置整个项目的,相当于以前的web.xml: 注意到上一节的访问HelloWorld时,项目路径也没有加:直接是http://loca ...

  7. spring boot学习(5) SpringBoot 之Spring Data Jpa 支持(2)

    第三节:自定义查询@Query 有时候复杂sql使用hql方式无法查询,这时候使用本地查询,使用原生sql的方式:   第四节:动态查询Specification 使用 什么时候用呢?比如搜索有很多条 ...

  8. spring boot 学习笔记(二) 构建web支持jsp

    一.必须将项目打包成war包 <packaging>war</packaging> 二.pom.xml加入依赖包 <dependency> <groupId& ...

  9. spring boot学习(7) SpringBoot 之表单验证

    第一节:SpringBoot 之表单验证@Valid 是spring-data-jpa的功能:   下面是添加学生的信息例子,要求姓名不能为空,年龄大于18岁.   贴下代码吧: Student实体: ...

随机推荐

  1. js 禁止剪切、复制、粘贴的文本框代码

    有的网站中不允许用户复制.粘贴.剪切文本框中的内容的,是怎么实现的呢?看看下面的代码就知道了. <input id=”username” oncut=”return false” oncopy= ...

  2. windows下清除svn密码

    刚进公司的时候没有SVN账号,暂用别人的账号很平常,为了更好的代码管理,后面肯定用自己的账号. 那么怎么清除呢. 进入 C:\Documents and Settings\Administrator\ ...

  3. ES Log4J配置信息

    # 配置RollingFileappender appender.rolling.type = RollingFile appender.rolling.name = rolling # 日志名称 / ...

  4. ZooKeeper客户端原生API的使用以及ZkClient第三方API的使用

    这两部分内容的介绍主要讲的是节点及节点内容和子节点的操作,并且讲解的节点的事件监听以及ACL授权 ZooKeeper客户端原生API的使用 百度网盘地址: http://pan.baidu.com/s ...

  5. mysql 聚合函数

    1.sum 用法 有这种类型的数据: id date user_id result 1 2015-05-04 1 win 2 2015-05-06 1 loss 3 2015-05-09 2 loss ...

  6. OC基础:getter和setter,@public @protected @private 分类: ios学习 OC 2015-06-15 19:23 22人阅读 评论(0) 收藏

    @public 1.公开的,公共的,可以在类的内部和外部访问. 2.类的内部:实例变量名 3.类的外部:对象->实例变量名 @protected 1.受保护的,只能在本类和子类中可以访问 2.类 ...

  7. matlab sparse()

    一.sparse()的稀疏矩阵简单运用 1.>> A = [0, 0, 0, 0; 0, 0, 1, 0; 0, 0, 0, 0; 0, 1, 0, 2]; >> sparse ...

  8. chapter02 PCA主成分分析在手写数字识别分类的应用

    #coding=utf8 # 导入numpy工具包. import numpy as np # 导入pandas用于数据分析. import pandas as pd from sklearn.met ...

  9. Failed to fetch URl https://dl-ssl.google.com/android/repository/addo Android SDK更新以及ADT更新出现问题的解决办法

    问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...

  10. python--selenium实用的自动生成测试HTML报告方法--HTMLTestRunner

    python--selenium实用的自动生成测试HTML报告方法--HTMLTestRunner 下面给大家介绍下用HTMLTestRunner模块自动生成测试报告的方法. 一.首先我们导入unit ...