一、需求:直接通过域名访问首页(同一应用下,多个首页,包括PC端、手机端首页)

  方法:采用多个域名绑定同一IP下同一应用,不同域名对应不同产品(PC、手机端)的方法,在后台通过拦截器判断 request 中的host字段值,进而重定向到不同的页面

@Configuration
@EnableWebMvc
public class InterceptorConfig extends WebMvcConfigurerAdapter { @Bean
public HostInterceptor hostInterceptor() {
return new HostInterceptor();
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(hostInterceptor()).addPathPatterns("/");
super.addInterceptors(registry);
}
}
package com.zxguan.thymeleaf.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* @author zxguan
* @description
* @create 2018-03-16 10:28
*/
public class HostInterceptor implements HandlerInterceptor { @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String host = request.getHeader("host");
if (host.equals("localhost")) {
response.sendRedirect("/home");
} else {
response.sendRedirect("/index");
}
return false;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
}
    @RequestMapping(value = {"/", "/index"})
public String test() {
return "test";
} @RequestMapping(value = "/home")
public String home() {
return "home";
}

最新文章

  1. [XAF] How to use the Allow/Deny permissions policy in the existing project
  2. 【position也可以很复杂】当弹出层遇上了鼠标定位(下)
  3. Python开发【第十七篇】:MySQL(一)
  4. SQLServer学习笔记<>相关子查询及复杂查询
  5. Jdbc 连接MySQL数据库的方法和问题
  6. Eclipse 快捷键 快捷输入
  7. dubbo服务+Spring事务+AOP动态数据源切换 出错
  8. AI自动寻路
  9. 08JS高级 ——“继承”
  10. JAVA入门[9]-mybatis多表关联查询
  11. [BZOJ]1069 最大土地面积(SCOI2007)
  12. ListView下拉刷新上拉加载更多实现
  13. python之读取配置文件模块configparser(三)高级使用---非标准配置文件解析
  14. [转]Laravel - Where null and Where not null eloquent query example
  15. stm32 启动文件 C和汇编交叉嵌入
  16. 使用小技巧加快IDEA的开发速度
  17. [HNOI2013]比赛 (用Hash实现记忆化搜索)
  18. SpringBoot添加对Log4j2的支持
  19. 【Java】通用版URLConnection 带cookie下载PDF等资源文件
  20. liferay项目经验之BasePortlet

热门文章

  1. 使用HttpClient调用WebAPI接口,含WebAPI端示例
  2. vue.js中如何使用scss
  3. ORA-00911: invalid character 错误解决集锦
  4. 【pep8规范】arc diff 不符合 pep 8 规范
  5. php判断进程是否存在
  6. MySQL复制表结构
  7. MySQL中使用replace into语句批量更新表数据
  8. 实现一个Promise
  9. 云计算服务模式(SaaS/PaaS/IaaS)
  10. rest_framework之三种分页器使用方法