千家信息网

springboot怎么实现返回视图

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,本篇内容主要讲解"springboot怎么实现返回视图",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot怎么实现返回视图"吧!spring
千家信息网最后更新 2025年02月03日springboot怎么实现返回视图

本篇内容主要讲解"springboot怎么实现返回视图",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot怎么实现返回视图"吧!

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@EnableAutoConfigurationpublic 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@EnableAutoConfigurationpublic 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 */@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {    @Override    public void addViewControllers(ViewControllerRegistry registry) {        registry        //接收的请求        .addViewController("/toLogin")        //跳转的页面名        .setViewName("login");    }}

补充一下

快速上手

1.在pom.xml添加依赖

                               org.springframework.boot            spring-boot-starter-web                                    org.springframework.boot            spring-boot-starter-thymeleaf        

2.创建页面login.html

3.配置thymeleaf

在application.yml中添加上

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

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

到此,相信大家对"springboot怎么实现返回视图"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0