首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《Springboot极简教程》继承WebMvcConfigurerAdapter: 一行代码写Controller文章概要常用的写Controller类方法继承 WebMvcConfigurerAd

《Springboot极简教程》继承WebMvcConfigurerAdapter: 一行代码写Controller文章概要常用的写Controller类方法继承 WebMvcConfigurerAd

作者头像
一个会写诗的程序员
发布2018-08-20 10:27:25
3390
发布2018-08-20 10:27:25
举报

文章概要

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
@ComponentScan
public 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
 */
@Configuration
public 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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.03.29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章概要
  • 常用的写Controller类方法
  • 继承 WebMvcConfigurerAdapter的Controller写法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档