《Springboot极简教程》继承WebMvcConfigurerAdapter: 一行代码写Controller
来源:互联网 发布:ubuntu wget安装 编辑:程序博客网 时间:2024/05/22 05:30
文章概要
registry.addViewController("/login").setViewName("login");
常用的写Controller类方法
我们通常这样写一个直接跳转view的Controller
package com.restfeel.controller;import java.util.Map;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@EnableAutoConfiguration@ComponentScanpublic class LoginController { @RequestMapping("/login") public String login(Map<String, Object> model) { return "login"; }}
要添加一个新页面访问总是要新增一个Controller或者在已有的一个Controller中新增一个方法,然后再跳转到设置的页面上去。考虑到大部分应用场景中View和后台都会有数据交互,这样的处理也无可厚非,不过我们肯定也有只是想通过一个URL Mapping然后不经过Controller处理直接跳转到页面上的需求!
继承 WebMvcConfigurerAdapter的Controller写法
package com.restfeel.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/** * Created by jack on 2017/3/28. * WebMvcConfig配置总类 * * @author jack * @date 2017/03/28 */@Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { //这一段等同于com.restfeel.controller.LoginController,静态资源的拦截处理在com.restfeel.config.security.SecurityConfig设置 registry.addViewController("/login").setViewName("login"); }}
这一段等同于com.restfeel.controller.LoginController,静态资源的拦截处理在com.restfeel.config.security.SecurityConfig设置。
源码:https://github.com/Jason-Chen-2017/restfeel/blob/master/src/main/java/com/restfeel/config/WebMvcConfig.java
阅读全文
0 0
- 《Springboot极简教程》继承WebMvcConfigurerAdapter: 一行代码写Controller
- springBoot 使用继承WebMvcConfigurerAdapter 添加多个拦截器和继承HandlerInterceptorAdapter创建拦截器
- 《Springboot极简教程》问题解决:Spring MVC 关于controller的字符编码
- Springboot极简教程
- Springboot极简教程
- WebMvcConfigurerAdapter
- 一行代码写python
- 《Springboot极简教程》运行SpringBoot程序
- 《Springboot极简教程》SpringBoot配置文件PropertySourcesPlaceholderConfigurer
- springboot controller
- springboot-Controller
- SpringBoot Controller
- 图片式教程:不写一行代码,实现 DropDownList 和 GridView 联动
- java 方式配置spring mvc 继承WebMvcConfigurerAdapter和继承WebMvcConfigurationSupport
- 《Springboot极简教程》 Springboot plus Kotlin :Hello,World
- 《Springboot极简教程》SpringBoot热部署应用Developer tools
- 每写一行代码,就有一行代码的自信。
- 《Springboot极简教程》使用@SpringBootApplication annotation
- wait-notify 实现多线程交叉备份
- Myeclipse2015把web项目添加至tomcat中,添加列表中却没有想要添加的项目
- 《Springboot极简教程》问题解决:Springboot启动报错 Whitelabel Error Page: This application has no explicit mapping for
- 《Springboot极简教程》问题解决:javax.servlet.ServletException: Circular view path [login]: would dispatch back to the current handler URL
- 原声JS判断程序运行平台Pc OR Mobile
- 《Springboot极简教程》继承WebMvcConfigurerAdapter: 一行代码写Controller
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 搭建Maven(搭建+问题笔记)
- 《Springboot极简教程》问题解决:Idea解决Gradle库依赖冲突问题:Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting
- 传递WinMain的参数给main
- 《Springboot极简教程》问题解决:org.apache.catalina.LifecycleException: A child container failed during start
- 邮件发送和接收原理
- 【tyvj1098】任务安排(dp)
- 《Springboot极简教程》Springboot使用Kotlin和Java混合编程