栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springmvc的视图

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springmvc的视图

springmvc的视图

在SpringMVC中的视图实现View接口,视图的作用是渲染数据,将模型Model中的数据展示给用户。

SpringMVC中视图的种类很多,默认有转发视图(internalResourceView)和重定向视图(RedirectView)两种。
但当我们的工程引入jstl的依赖的时候,转发视图会自动转换为JstlView。
当我们使用Thymeleaf技术的时候,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后得到的是ThymeleafView。

thymeleafview

thymeleaf是一个视图解析器,它可以解析springmvc中的model中的数据来发送给用户,当不存在前缀的时候解析的视图就是thymeleafview

package com.jin.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping("/testThymeleafview")
    public String test1(){
        return "success";
    }
}



    
    Title


测试thymeleafview


重新部署tomcat,然后点击超链接

这是当没有前缀时候的视图解析

internalResourceView(转发视图)

给返回的值添加一个forward前缀,实现internalResourceview

package com.jin.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping("/testForword")
    public String test2(){
        return "forward:/testThymeleafview";
    }
}



    
    Title


测试internalResourceview


可以发现跳转的是第一个页面,但是上面的连接依然是testForword而不是testThymeleafview,可知实现了视图的转发

RedirectView

实现的是视图的重定向,前缀为redirect

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping("/testRedirect")
    public String test3(){
        return "redirect:/testThymeleafview";
    }
}




    
    Title


测试redirect


点击超链接

可以发现连接栏里面的链接不是testRedirect而是testThymeleafview,实现了重定向

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/727097.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号