静态资源放在static目录下,原本访问应用需要访问 ip+port+/module/+模块名称+/页面地址 进行访问具体页面,而正常访问域名时,会提示错误,需要输入全路径才能够访问到真实页面,所以需要进行特殊处理。
后端处理:
后端的springboot配置,是采用的继承WebMvcConfigurationSupport的方式实现的。增加addViewControllers方法重写,设置/转发到/index.html中。
import java.nio.charset.Charset;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* <p>类说明:MVC配置类
*
* @version 1.00
* @author:zhengjianhua
*/
@Configuration
public class MvcConfigurer extends WebMvcConfigurationSupport {
/**
* 上传路径 比如J:\\zxwUpload\\
*/
@Value("${com.zjh.uploadPath}")
private String uploadPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//静态资源访问路径配置
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//本地资源访问路径配置
registry.addResourceHandler("/uploadPath/**").addResourceLocations("file:" + uploadPath);
registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
/**
* 编码设置
* (non-Javadoc)
*
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#extendMessageConverters(java.util.List)
*/
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.forEach(converter -> {
if (converter instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) converter).setDefaultCharset(Charset.forName("UTF-8"));
}
});
}
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
// 默认跳转index页面
registry.addViewController("/").setViewName("forward:/index.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}前端处理:
在static目录下添加index.html文件,在代码中,根据缓存判断是跳转具体的index页面还是login页面。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智选网</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="Shortcut Icon" href="/favicon.ico"/>
<!-- load css -->
<link rel="stylesheet" type="text/css" href="../../plugins/layui2.2.5/css/layui.css" media="all">
<link rel="stylesheet" type="text/css" href="../common/css/global.css" media="all">
<link rel="stylesheet" type="text/css" href="css/font_bmgv5kod196q1tt9.css">
<link rel="stylesheet" type="text/css" href="css/index.css" media="all">
<script type="text/javascript" src="../../plugins/jquery/jquery.2.1.4.min.js"></script>
</head>
<body>
<script type="text/javascript">
// 设置用户信息到缓存中
var adminUser = window.sessionStorage.getItem('adminUser')
if(adminUser){
window.location.href = '/module/index/index.html';
} else {
window.location.href = '/module/login/login.html';
}
</script>
</body>
</html>最终实现,访问域名地址,直接跳转对应的页面




发表评论