溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot怎么實現(xiàn)返回視圖

發(fā)布時間:2022-01-28 09:05:31 來源:億速云 閱讀:533 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“springboot怎么實現(xiàn)返回視圖”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“springboot怎么實現(xiàn)返回視圖”吧!

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 對象的信息,本質(zhì)是一個字符串。)如果用了@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;
    }
}

一般用于攜帶參數(shù)且返回視圖,如果要帶參數(shù)的話,加上mode.addObject()函數(shù)

另外需要注意一點,html文件中所有標(biāo)簽都必須要有結(jié)束符,idea有時候生成meta標(biāo)簽時會沒有結(jié)束符,所以要加上

最終輸入http://localhost:8080/hello就可以了 

springboot返回視圖方式

Spring boot返回視圖的方式

1.使用ModelAndView

在controller中

    @RequestMapping("toTest")
    public ModelAndView toTest(){
        ModelAndView mv = new ModelAndView();
        //視圖名
        mv.setViewName("login");
        //想傳的數(shù)據(jù)
        mv.addObject("o1","數(shù)據(jù)1");
        return mv;
    }

2.使用webMVC配置

創(chuàng)建配置類

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")
        //跳轉(zhuǎn)的頁面名
        .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.創(chuàng)建頁面login.html

springboot怎么實現(xiàn)返回視圖

3.配置thymeleaf

在application.yml中添加上

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

再進行視圖配置就可以訪問到了

到此,相信大家對“springboot怎么實現(xiàn)返回視圖”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI