12 扩展Spring MVC

12.1 实现页面跳转功能

        页面跳转功能:访问localhost:8081/jiang会自动跳转到另一个页面。

        首先,在config包下创建一个名为MyMvcConfig的配置类:

         类上加入@Configuration注解,类实现WebMvcConfiger接口,实现里面的视图跳转方法addViewConrollers:

        注意:在转发的地址中,不用加"/"也可以。

package jiang.com.springbootstudy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    // 视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("jiang").setViewName("hello"); //访问localhost:8081/jiang后会跳转到hello.html这个页面
    }
}

        在访问http://localhost:8081/jiang后,会自动跳转到hello的页面。

猜你喜欢

转载自blog.csdn.net/no996yes885/article/details/131881063