springboot實(shí)現(xiàn)返回視圖而不是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("進(jìn)入controller");
? ? ? ? return "hello";
? ? }
}注意釋@Controller而不是@RestContreller
@RestController返回的是json(JSON 是 JS 對(duì)象的字符串表示法,它使用文本表示一個(gè) JS 對(duì)象的信息,本質(zhì)是一個(gè)字符串。)如果用了@RestController則不要用@Responsebody
還有一種就是通過(guò)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ì)沒(méi)有結(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)的頁(yè)面名
.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)建頁(yè)面login.html

3.配置thymeleaf
在application.yml中添加上
spring:
thymeleaf:
#頁(yè)面存放位置
prefix: classpath:/templates/
#是否緩存 這里是否
cache: false
suffix: .html
mode: LEGACYHTML5
template-resolver-order: 0
再進(jìn)行視圖配置就可以訪問(wèn)到了
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
ssm框架controller層返回json格式數(shù)據(jù)到頁(yè)面的實(shí)現(xiàn)
這篇文章主要介紹了ssm框架controller層返回json格式數(shù)據(jù)到頁(yè)面的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java關(guān)鍵字volatile知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于Java關(guān)鍵字volatile知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-01-01
java簡(jiǎn)單實(shí)現(xiàn)數(shù)組的增刪改查方法
這篇文章主要介紹了Java數(shù)組的增刪改查的示例,幫助大家更好的利用Java處理數(shù)據(jù),感興趣的朋友可以了解下,希望能給你帶來(lái)幫助2021-07-07
簡(jiǎn)單了解SpringMVC緩存對(duì)靜態(tài)資源有什么影響
這篇文章主要介紹了簡(jiǎn)單了解SpringMVC緩存對(duì)靜態(tài)資源有什么影響,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java中的java.lang.reflect.Type簡(jiǎn)介
在 Java 中,java.lang.reflect.Type 是一個(gè)接口,代表所有類型的通用超類型,它包括原始類型、參數(shù)化類型、數(shù)組類型、類型變量和基本類型,本文給大家講解Java中的java.lang.reflect.Type是什么,需要的朋友可以參考下2024-06-06
SpringCloud使用CircuitBreaker實(shí)現(xiàn)熔斷器的詳細(xì)步驟
在微服務(wù)架構(gòu)中,服務(wù)之間的依賴調(diào)用非常頻繁,當(dāng)一個(gè)下游服務(wù)因高負(fù)載或故障導(dǎo)致響應(yīng)變慢或不可用時(shí),可能會(huì)引發(fā)上游服務(wù)的級(jí)聯(lián)故障,最終導(dǎo)致整個(gè)系統(tǒng)崩潰,熔斷器是解決這類問(wèn)題的關(guān)鍵模式之一,Spring Cloud提供了對(duì)熔斷器的支持,本文將詳細(xì)介紹如何集成和使用它2025-02-02
Java實(shí)現(xiàn)Excel轉(zhuǎn)PDF的兩種方法詳解
使用具將Excel轉(zhuǎn)為PDF的方法有很多,在這里我給大家介紹兩種常用的方法:使用spire轉(zhuǎn)化PDF、使用jacob實(shí)現(xiàn)Excel轉(zhuǎn)PDF,分別應(yīng)對(duì)兩種不一樣的使用場(chǎng)景,需要的可以參考一下2022-01-01
Java Swing組件JFileChooser用法實(shí)例分析
這篇文章主要介紹了Java Swing組件JFileChooser用法,結(jié)合實(shí)例形式分析了java Swing組件JFileChooser文件選擇器的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-11-11
Hibernate框架數(shù)據(jù)分頁(yè)技術(shù)實(shí)例分析
這篇文章主要介紹了Hibernate框架數(shù)據(jù)分頁(yè)技術(shù),結(jié)合實(shí)例形式分析了Hibernate框架實(shí)現(xiàn)數(shù)據(jù)分頁(yè)的原理,步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03

