基于第二讲,这一讲我们主要讲解包含以下内容

  1. springBoot添加对freemarker的支持
  2. 使用@RestController处理ajax请求
  3. 使用@PathVariable注解获取url参数
  4. 使用@RequestParam注解获取请求参数

(一)springBoot添加对freemarker的支持

(1)首先要使用freemarker,我们需要添加对freemarker的支持,需要在pom.xml中添加依赖,操作如下

(2)编写FreemarkerAction,返回数据视图

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/freemarker")
public class FreemarkerAction { @RequestMapping("/say")
public ModelAndView say() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", "hello freemarker");
modelAndView.setViewName("index");
return modelAndView;
}
}

(3)在templates目录下新建index.html,更改名为index.ftl,这个是Freemarker的格式要求,内容如下

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
msg show :${msg}
</body>
</html>

(3)重启服务,访问http://localhost:8888/hello/freemarker/say,结果如下

(二)使用@RestController处理ajax请求

(1)在webapp目录下新增index.html,同时引用的是官网的jquery-2.1.1.min.js,内容如下

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
function show(){
$.post("ajax/test",{},function(result){
alert(result);
});
}
</script>
<body>
<button onclick="show()">点击这里</button>
</body>
</html>

(2)新增AjaxActionTest。内容如下:

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/ajax")
public class AjaxActionTest { @RequestMapping("/test")
public String test() { return "{'name':'李四','age':'24'}";
}
}

(3)重启服务。浏览器访问:http://localhost:8888/hello/

(三)使用@PathVariable注解获取url参数

(1)在类FreemarkerAction类中,新增方法say2,如下:

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/freemarker")
public class FreemarkerAction { @RequestMapping("/say")
public ModelAndView say() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", "hello freemarker");
modelAndView.setViewName("index");
return modelAndView;
} @RequestMapping("/{msg}")
public ModelAndView say2(@PathVariable("msg") String msg) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", msg);
modelAndView.setViewName("index");
return modelAndView;
}
}

(1)浏览器访问:http://localhost:8888/hello/freemarker/hellorestful,结果如下图:这种风格类似于restful的支持

(四)使用@RequestParam注解获取请求参数

(1)在index.html中新增一个链接,如下图

(2)在FreemarkerAction新增方法 say3

package com.example.demo;

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("/freemarker")
public class FreemarkerAction { @RequestMapping("/say")
public ModelAndView say() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", "hello freemarker");
modelAndView.setViewName("index");
return modelAndView;
} @RequestMapping("/{msg}")
public ModelAndView say2(@PathVariable("msg") String msg) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", msg);
modelAndView.setViewName("index");
return modelAndView;
} @RequestMapping("/reqParam")
public ModelAndView say3(@RequestParam(value="msg",required=false) String msg) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", msg);
modelAndView.setViewName("index");
return modelAndView;
}
}

(2)浏览器上访问http://localhost:8888/hello/  点击链接  ,结果如下

SpringBoot入门学习(三)的更多相关文章

  1. SpringBoot入门(三)——入口类解析

    本文来自网易云社区 上一篇介绍了起步依赖,这篇我们先来看下SpringBoot项目是如何启动的. 入口类 再次观察工程的Maven配置文件,可以看到工程的默认打包方式是jar格式的. <pack ...

  2. SCARA——OpenGL入门学习三

    OpenGL入门学习[三] 在第二课中,我们学习了如何绘制几何图形,但大家如果多写几个程序,就会发现其实还是有些郁闷之处.例如:点太小,难以看清楚:直线也太细,不舒服:或者想画虚线,但不知道方法只能用 ...

  3. SpringBoot入门学习看这一篇就够了

    1.SpringBoot是什么? SpringBoot是一套基于Spring框架的微服务框架. 2.为什么需要SpringBoot 由于Spring是一个轻量级的企业开发框架,主要的功能就是用于整合和 ...

  4. 【Java】SpringBoot入门学习及基本使用

    SpringBoot入门及基本使用 SpringBoot的介绍我就不多说了,核心的就是"约定大于配置",接下来直接上干货吧! 本文的实例: github-LPCloud,欢迎sta ...

  5. dubbo入门学习(三)-----dubbo整合springboot

    springboot节省了大量的精力去配置各种bean,因此通过一个简单的demo来整合springboot与dubbo 一.创建boot-user-service-provider 本篇博文基于上篇 ...

  6. springboot入门学习1

    springboot学习1 SpringBoot对Spring的缺点进行的改善和优化,基于约定优于配置的思想,可以让开发人员不必在配置与逻辑 业务之间进行思维的切换,全身心的投入到逻辑业务的代码编写中 ...

  7. SpringBoot入门学习记录(一)

    最近,SpringBoot.SpringCloud.Dubbo等框架非常流行,作为Coder里的一名小学生,借着改革开放的东风,自然也是需要学习学习的,于是将学习经历记录于此,以备日后查看. 官网:h ...

  8. SpringBoot入门学习(一)

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.用我 ...

  9. SpringBoot入门教程(三)通过properties实现多个数据库环境自动切换配置

    前面的文章已经介绍了CentOS部署SpringBoot项目从0到1的详细过程,包括Linux安装ftp.Tomcat以及Java jdk的全部过程.这篇文章主要介绍关于springboot如何通过多 ...

随机推荐

  1. MyEclipse10.6 安装SVN插件方法及插件下载地址

        今天MyEclipse10.6出了点问题,所以重装了它,同一时候也把svn的插件重装了一次,把网上资源和自己的经历顺便在博客这里记录一下.建议直接看方法一好了,简单方便,不必要折腾太多. 下来 ...

  2. android硬件返回

    1.第一种 @Override    public boolean onKeyUp(int keyCode, KeyEvent event) {        //点击回退键        if(Ke ...

  3. css+jq写的小小的移动端按钮的动画改变(三个很闲变成一个叉号)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. maven 基本框架搭建

    在平时的开发中还是在写blog时,在项目实例开始都会需要一遍一遍的介绍maven框架搭建,重复性的工作让我觉得烦恼,现在展现一下Java的核心思想“重复利用”,将这个重复性的描述提取出来一次性介绍,以 ...

  5. LeetCode 笔记系列 18 Maximal Rectangle [学以致用]

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  6. 字符串与图片的Base64编码转换操作

    //图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = ne ...

  7. HBase简介及集群安装

    一.Hbase概述 Apache HBase™是Hadoop数据库,是一个分布式,可扩展的大数据存储. 当您需要对大数据进行随机,实时读/写访问时,请使用Apache HBase™.该项目的目标是托 ...

  8. 单舵轮(叉车)AGV里程计数据解算

    单舵轮(叉车)AGV里程计数据解算 2016-07 单舵轮AGV,一般包含一个驱动轮和两个从动轮,驱动轮是同时具备行走和转向两个功能的舵轮,因此,单舵轮AGV的运动学自由度为2个.舵轮线速度V1,舵轮 ...

  9. VoIP应用系统大盘点

    一.VoIP拓扑 PBX是程控交换机,程控交换机有实体交换机和软件模拟的交换机. 软件模拟的交换机,即交换机服务器,常用开源的sip服务器有asterisk,freepbx, opensip等,商用的 ...

  10. python学习笔记(九)函数返回多个值,列表生成式,循环多个变量,入参格式声明

    一.函数返回多个值 1.函数如果返回多个值的话,它会把这几个值放到一个元组里面2.函数如果返回多个值的话,也可以用多个变量来接收 def say(): num1 = num2 = num3 = ret ...