不管是使用springMVC还时springBoot,在进行简单的页面跳转的时候,有两种情况:
1.在进行页面跳转的时候回携带请求数据,后端需要进行业务处理。
2.只是单纯的进行页面的跳转,不会进行任何业务的处理
对比使用springMVC和springboot对这两种情况的处理:
情况一:携带数据:
springMVC和springBoot大致相同,都是通过controller层进行页面的跳转:
示例代码:
@controller
pubilc String pageControlelr(){
// ... 这里是业务代码
// 返回值就是要跳转的视图页面的名称
return "index";
}
情况二:就是跳转页面啥都不做
对于springMVC提供了配置文件中进行配置的方式:
<mvc:view-controller path="page.html" view-name="iindex"/>
其中 page.html就是请求url,index就是要跳转的视图
对于springboot没有常规配置文件,提供的是注解配置类的方式。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
// 这是一个view-controller,进行简单的页面的跳转
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// 浏览器访问的地址
String urlPath = "/auth/to/reg.html";
// 要跳转的视图页面的名称,会加载配置文件中的视图前后缀
String viewName = "register";
registry.addViewController(urlPath).setViewName(viewName);
}
// 这是拦截器,如果定制拦截器就是重写这个方法
/*@Override
public void addInterceptors(InterceptorRegistry registry) {
}*/
}
在这里还可以进行拦截器的配置。