当前位置: 首页> 教育> 高考 > 比较好网站设计公司_百度seo效果_优秀营销软文100篇_宁波seo哪家好

比较好网站设计公司_百度seo效果_优秀营销软文100篇_宁波seo哪家好

时间:2025/7/12 15:10:56来源:https://blog.csdn.net/m0_73902080/article/details/142588574 浏览次数:0次
比较好网站设计公司_百度seo效果_优秀营销软文100篇_宁波seo哪家好

目录

ThymeleafView

转发视图

重定向视图

视图控制器view-controller


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

SpringMVC视图的种类很多,默认有转发视图InternalResourceView 和重定向视图RedirectView

当工程引入jstl的依赖,转发视图会自动转换为JstlView

若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的是ThymeleafView

ThymeleafView

当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析,视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class ViewController {@RequestMapping("/testThymeleafView")public String testThymeleafView(){return "success";}
}
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class TestController {@RequestMapping("/")public String index(){return "index";}@RequestMapping("/test_view")public String testView(){return "test_view";}
}

 test_view.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<a th:href="@{/testThymeleafView}">测试ThymeleafView</a>
</body>
</html>

运行: 

转发视图

SpringMVC中默认的转发视图是InternalResourceView

SpringMVC中创建转发视图的情况:

当控制器方法中所设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"forward:"去掉,剩余部分作为最终路径通过转发的方式实现跳转

例如"forward:/","forward:/employee"

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class ViewController {@RequestMapping("/testThymeleafView")public String testThymeleafView(){return "success";}@RequestMapping("/testForward")public String testForward(){return "forward:/testThymeleafView";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<a th:href="@{/testForward}">测试InternalResourceView</a>
</body>
</html>

 运行:

页面路径为/testForward

重定向视图

SpringMVC中默认的重定向视图是RedirectView

当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:"去掉,剩余部分作为最终路径通过重定向的方式实现跳转

例如"redirect:/","redirect:/employee"

重定向视图在解析时,会先将redirect:前缀去掉,然后会判断剩余部分是否以/开头,若是则会自 动拼接上下文路径

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class ViewController {@RequestMapping("/testThymeleafView")public String testThymeleafView(){return "success";}@RequestMapping("/testRedirect")public String testRedirect(){return "redirect:/testThymeleafView";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<a th:href="@{/testRedirect}">测试RedirectView</a>
</body>
</html>

 运行:

页面路径为/testThymeleafView

视图控制器view-controller

当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用viewcontroller标签进行表示

springMVC.xml:

<!--path:设置处理的请求地址view-name:设置请求地址所对应的视图名称
-->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>

代替:

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class TestController {@RequestMapping("/")public String index(){return "index";}
}

注: 当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效,此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签:

<!--开启MVC的注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
关键字:比较好网站设计公司_百度seo效果_优秀营销软文100篇_宁波seo哪家好

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: