springboot实现返回视图而不是string的方法

 更新时间:2022年01月27日 16:53:53   作者:airuoflora  
这篇文章主要介绍了springboot实现返回视图而不是string的方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springboot返回视图而不是string

package com.example.demo.controller;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@EnableAutoConfiguration
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        System.out.println("进入controller");
        return "hello";
    }
}

注意释@Controller而不是@RestContreller

@RestController返回的是json(JSON 是 JS 对象的字符串表示法,它使用文本表示一个 JS 对象的信息,本质是一个字符串。)如果用了@RestController则不要用@Responsebody

还有一种就是通过ModelAndView

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
@EnableAutoConfiguration
public class HelloController {
    @RequestMapping("/hello")
    @ResponseBody
    public ModelAndView hello(){
        System.out.println("hello!");
        ModelAndView mode = new ModelAndView();
        mode.setViewName("hello");
        return mode;
    }
}

一般用于携带参数且返回视图,如果要带参数的话,加上mode.addObject()函数

另外需要注意一点,html文件中所有标签都必须要有结束符,idea有时候生成meta标签时会没有结束符,所以要加上

最终输入http://localhost:8080/hello就可以了 

springboot返回视图方式

Spring boot返回视图的方式

1.使用ModelAndView

在controller中

    @RequestMapping("toTest")
    public ModelAndView toTest(){
        ModelAndView mv = new ModelAndView();
        //视图名
        mv.setViewName("login");
        //想传的数据
        mv.addObject("o1","数据1");
        return mv;
    }

2.使用webMVC配置

创建配置类

package com.ch.exercise.config.webMvc;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * MVC配置
 * @author CH
 * @date 2021-08-19 11:45
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry
        //接收的请求
        .addViewController("/toLogin")
        //跳转的页面名
        .setViewName("login");
    }
}

补充一下

快速上手

1.在pom.xml添加依赖

		<!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.创建页面login.html

在这里插入图片描述

3.配置thymeleaf

在application.yml中添加上

spring:
  thymeleaf:
  	#页面存放位置
    prefix: classpath:/templates/
    #是否缓存 这里是否
    cache: false
    suffix: .html
    mode: LEGACYHTML5
    template-resolver-order: 0

再进行视图配置就可以访问到了

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • java开发gui教程之jframe监听窗体大小变化事件和jframe创建窗体

    java开发gui教程之jframe监听窗体大小变化事件和jframe创建窗体

    这篇文章主要介绍了java开发gui教程中jframe监听窗体大小变化事件和jframe创建窗体的示例,需要的朋友可以参考下
    2014-03-03
  • Jenkins 关闭和重启详细介绍及实现

    Jenkins 关闭和重启详细介绍及实现

    这篇文章主要介绍了Jenkins的关闭、重启的相关资料,用jar -jar jenkins.war来启动jenkins服务器,那么我们如何关闭或者重启jenkins服务器呢,这里就给出实现的方法,需要的朋友可以参考下
    2016-11-11
  • Linux中JDK安装配置教程

    Linux中JDK安装配置教程

    这篇文章主要为大家详细介绍了Linux中JDK安装配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • 超全MyBatis动态代理详解(绝对干货)

    超全MyBatis动态代理详解(绝对干货)

    这篇文章主要介绍了超全MyBatis动态代理详解(绝对干货),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • SpringBoot2整合activiti6环境搭建过程解析

    SpringBoot2整合activiti6环境搭建过程解析

    这篇文章主要介绍了SpringBoot2整合activiti6环境搭建过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • Java获取视频时长、大小的示例

    Java获取视频时长、大小的示例

    这篇文章主要介绍了Java获取视频时长、大小的示例,帮助大家利用Java处理视频,完成需求,感兴趣的朋友可以了解下
    2020-11-11
  • 浅谈java String.split丢失结尾空字符串的问题

    浅谈java String.split丢失结尾空字符串的问题

    下面小编就为大家带来一篇浅谈java String.split丢失结尾空字符串的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • springboot文件打包成jar或war的方法

    springboot文件打包成jar或war的方法

    这篇文章主要介绍了springboot文件打包成jar或war的方法以及相关知识点,需要的朋友们参考下。
    2019-08-08
  • Spring详解使用注解开发流程

    Spring详解使用注解开发流程

    这篇文章主要为大家详细介绍了Spring如何使用注解开发,文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的可以参考一下
    2022-05-05
  • Spring Boot 2.x基础教程之配置元数据的应用

    Spring Boot 2.x基础教程之配置元数据的应用

    这篇文章主要介绍了Spring Boot 2.x基础教程之配置元数据的应用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01

最新评论