亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot實(shí)現(xiàn)返回視圖而不是string的方法

 更新時(shí)間:2022年01月27日 16:53:53   作者:airuoflora  
這篇文章主要介紹了springboot實(shí)現(xiàn)返回視圖而不是string的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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("進(jìn)入controller");
? ? ? ? return "hello";
? ? }
}

注意釋@Controller而不是@RestContreller

@RestController返回的是json(JSON 是 JS 對(duì)象的字符串表示法,它使用文本表示一個(gè) JS 對(duì)象的信息,本質(zhì)是一個(gè)字符串。)如果用了@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ù)

另外需要注意一點(diǎn),html文件中所有標(biāo)簽都必須要有結(jié)束符,idea有時(shí)候生成meta標(biāo)簽時(shí)會(huì)沒有結(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
        //接收的請(qǐng)求
        .addViewController("/toLogin")
        //跳轉(zhuǎn)的頁面名
        .setViewName("login");
    }
}

補(bǔ)充一下

快速上手

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

在這里插入圖片描述

3.配置thymeleaf

在application.yml中添加上

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

再進(jìn)行視圖配置就可以訪問到了

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java開發(fā)gui教程之jframe監(jiān)聽窗體大小變化事件和jframe創(chuàng)建窗體

    java開發(fā)gui教程之jframe監(jiān)聽窗體大小變化事件和jframe創(chuàng)建窗體

    這篇文章主要介紹了java開發(fā)gui教程中jframe監(jiān)聽窗體大小變化事件和jframe創(chuàng)建窗體的示例,需要的朋友可以參考下
    2014-03-03
  • Jenkins 關(guān)閉和重啟詳細(xì)介紹及實(shí)現(xiàn)

    Jenkins 關(guān)閉和重啟詳細(xì)介紹及實(shí)現(xiàn)

    這篇文章主要介紹了Jenkins的關(guān)閉、重啟的相關(guān)資料,用jar -jar jenkins.war來啟動(dòng)jenkins服務(wù)器,那么我們?nèi)绾侮P(guān)閉或者重啟jenkins服務(wù)器呢,這里就給出實(shí)現(xiàn)的方法,需要的朋友可以參考下
    2016-11-11
  • Linux中JDK安裝配置教程

    Linux中JDK安裝配置教程

    這篇文章主要為大家詳細(xì)介紹了Linux中JDK安裝配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 超全MyBatis動(dòng)態(tài)代理詳解(絕對(duì)干貨)

    超全MyBatis動(dòng)態(tài)代理詳解(絕對(duì)干貨)

    這篇文章主要介紹了超全MyBatis動(dòng)態(tài)代理詳解(絕對(duì)干貨),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • SpringBoot2整合activiti6環(huán)境搭建過程解析

    SpringBoot2整合activiti6環(huán)境搭建過程解析

    這篇文章主要介紹了SpringBoot2整合activiti6環(huán)境搭建過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java獲取視頻時(shí)長、大小的示例

    Java獲取視頻時(shí)長、大小的示例

    這篇文章主要介紹了Java獲取視頻時(shí)長、大小的示例,幫助大家利用Java處理視頻,完成需求,感興趣的朋友可以了解下
    2020-11-11
  • 淺談java String.split丟失結(jié)尾空字符串的問題

    淺談java String.split丟失結(jié)尾空字符串的問題

    下面小編就為大家?guī)硪黄獪\談java String.split丟失結(jié)尾空字符串的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • springboot文件打包成jar或war的方法

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

    這篇文章主要介紹了springboot文件打包成jar或war的方法以及相關(guān)知識(shí)點(diǎn),需要的朋友們參考下。
    2019-08-08
  • Spring詳解使用注解開發(fā)流程

    Spring詳解使用注解開發(fā)流程

    這篇文章主要為大家詳細(xì)介紹了Spring如何使用注解開發(fā),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-05-05
  • Spring Boot 2.x基礎(chǔ)教程之配置元數(shù)據(jù)的應(yīng)用

    Spring Boot 2.x基礎(chǔ)教程之配置元數(shù)據(jù)的應(yīng)用

    這篇文章主要介紹了Spring Boot 2.x基礎(chǔ)教程之配置元數(shù)據(jù)的應(yīng)用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01

最新評(píng)論