Spring boot返回视图的方式

本文介绍了SpringBoot中两种返回视图的方法:使用ModelAndView和WebMvc配置。通过ModelAndView,可以设置视图名和传递数据。而WebMvc配置则通过ViewControllerRegistry实现请求与页面的映射。此外,还提供了快速上手步骤,包括添加SpringBoot的web和Thymeleaf依赖,创建login.html页面,并配置Thymeleaf模板路径和缓存设置,以便访问视图。

一、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

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

Spring Boot对接视图接口有多种实现方法,以下详细介绍: ### 配置视图控制器 在Spring Boot中,可通过实现`WebMvcConfigurer`接口的`addViewControllers`方法,将URL直接映射到特定视图,无需编写控制器类,适合处理简单页面跳转。例如,把根路径`"/"`映射到`welcome.html`视图,当访问应用根URL时,Spring MVC会自动渲染该页面。具体操作步骤如下: 1. 在`templates`目录下创建欢迎页面`welcome.html`。 2. 编辑MVC配置类,添加视图控制器映射。 3. 启动应用后,访问`http://localhost:8080`就能看到欢迎页面,点击链接可跳转到登录页面。这种方式简化了视图映射配置,提高了开发效率 [^1]。 ### 创建控制器类 创建控制器类可以处理更复杂的页面逻辑。例如创建`HomeController.java`控制器类,代码如下: ```java package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.security.Principal; @Controller public class HomeController { @GetMapping("/") public String index() { return "index"; } @GetMapping("/home") public String home(Model model, Principal principal) { model.addAttribute("name", principal.getName()); return "home"; } @GetMapping("/login") public String login() { return "login"; } } ``` 在上述代码中,通过`@GetMapping`注解将不同的URL映射到不同的视图方法,可根据业务需求在方法中添加更多逻辑 [^2]。 ### 创建简单控制器处理页面 创建简单的控制器来处理应用的主页面和登录成功后的页面。例如`MainController`类: ```java package cn.juwatech.controller; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MainController { @RequestMapping("/") public String index() { return "index"; } @GetMapping("/home") @ResponseBody public String home(Authentication authentication) { return "Welcome, " + authentication.getName(); } } ``` 此控制器使用`@RequestMapping`和`@GetMapping`注解将URL映射到相应方法,`@ResponseBody`注解可直接返回字符串作为响应 [^3]。 ### 基于Spring的三方平台接口对接(含视图) 基于Spring的三方平台接口对接方法涉及视图相关操作。在原表建立视图,并创建新用户,赋予只读权限,使用`@DataSource`来切换数据库查询。具体步骤如下: 1. 创建视图和用户并赋予权限: ```sql grant select on [view] to [username]; -- 查询视图权限 grant connect to [username]; -- 连接数据库权限 -- 创建设备视图 create or replace view factory_device_view as select * from factory_device; comment on table factory_device_view is '设备视图'; SELECT * FROM all_tab_cols WHERE table_name = 'FACTORY_DEVICE_VIEW'; -- 创建用户 create user jtgViewer identified by password123$ -- 赋予权限 GRANT SELECT ON jtg.factory_device_view TO jtgViewer; grant connect to jtgViewer ``` 2. 配置文件: ```yaml # 从库数据源 slave: # 从数据源开关/默认关闭 enabled: true url: jdbc:mysql://192.168.100.200:3306/esis_tjzg?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true username: root password: Inspur@2023 ``` 3. 查询使用: ```java @DataSource(DataSourceType.SLAVE) public List<Map<String,Object>> selectOutAmountByMaterial(TImMaterialrequestbill tImMaterialrequestbill); ``` 这种方式适用于需要与三方平台进行数据交互并使用视图的场景 [^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值