我是Java和Spring的新手.如何将我的应用程序根映射http://localhost:8080/到静态index.html?如果我导航到http://localhost:8080/index.html它的工作正常.
我的app结构是:

我config\WebConfig.java看起来像这样:
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
}
Run Code Online (Sandbox Code Playgroud)
我试图添加registry.addResourceHandler("/").addResourceLocations("/index.html");但它失败了.
我只是迁移到spring mvc版本5.0.1.RELEASE但突然在eclipse中STS WebMvcConfigurerAdapter被标记为已弃用
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
// to serve static .html pages...
registry.addResourceHandler("/static/**").addResourceLocations("/resources/static/");
}
....
}
Run Code Online (Sandbox Code Playgroud)
我怎么能删除这个!
我正在尝试创建一个Web应用程序,您可以在其中上传保存在我的文件系统中的图像,然后在某个地址(有点像imgur)提供给用户.
在将图像上传并保存到我的系统后,我一直在处理图像时遇到麻烦.有人向我指出,当我将它们存储在我的项目目标文件夹中时,问题可能是我将图像存储为源树的一部分.现在,我首先将我的图像存储在project/src/main/resources/static/images中的原因是因为我无法在其他任何地方在我的网站上提供静态内容.
我一直在搜索和阅读这几天,我还没有找到任何适用的解决方案.这些问题的答案通常涉及我的项目没有的文件(比如application-servlet.xml或web.xml)以及我之前从未使用过的文件(我是Spring,Spring-Boot和一般的Java Web开发).
我的项目的基础是由github上的一个导师制作的,然后我克隆了它,包括Application.java,所以我用Spring Configuration看到的解决方案也不合适.
以下是我的项目(希望是相关的)部分:
这是我的Application.java文件.我没有做任何事情,它与我克隆的原始github中的文件完全相同.
package project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
/**
* The main class of the project.
* By running the main class of {@link Application} then you start the Spring Boot system
*/
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder){
return applicationBuilder.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的application.properties.这里我刚刚添加了数据库内容:
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
multipart.maxFileSize=-1
debug=true
spring.datasource.url=jdbc:postgresql://localhost:5432/finaltest
spring.datasource.username=myusername
spring.datasource.password=mypassword
Run Code Online (Sandbox Code Playgroud)
这是我的pom.xml(我项目中唯一的xml文件).这里我只添加了数据库依赖项:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" …Run Code Online (Sandbox Code Playgroud) 我想用spring boot开发web应用程序,我想在jsp文件中解决我的javascript和css资源.我从dispatcher-servlet.xml中的jsp配置我对这些文件的访问,如下所示:
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
Run Code Online (Sandbox Code Playgroud)
在我的jsp文件中,我可以使用下面的代码来访问:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<img class="first-slide home-image" src="<spring:url value="/resources/images/back1.jpg"/>" alt="First slide">
Run Code Online (Sandbox Code Playgroud)
我怎么做配置mvc:资源映射在春季启动?
我有一个 Spring Boot 2 应用程序,其中静态资源是:
src
|- main
|-resources
|-static
|-js/myjs.js
|-style
|-css/mycss.css
Run Code Online (Sandbox Code Playgroud)
在我的模板文件中:
<link rel="stylesheet" type="text/css" href="/style/css/mycss.css">
<script src="/js/myjs.js"></script>
Run Code Online (Sandbox Code Playgroud)
这工作正常。
不过我想启用浏览器缓存和 gzip 传输。为此,我创建了以下 WebConfig:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/static/**")
.addResourceLocations("/static/")
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new GzipResourceResolver())
.addResolver(new PathResourceResolver());
}
}
Run Code Online (Sandbox Code Playgroud)
该应用程序仍然可以运行,但不会缓存或压缩静态内容:
知道我做错了什么吗?