【问题标题】:Intellij-idea's Intellisense cannot recognize Spring's static resource mapping in htmlIntellij-idea 的 Intellisense 无法识别 html 中 Spring 的静态资源映射
【发布时间】:2017-05-12 02:14:23
【问题描述】:

我希望 Intellisense 在我的 jsp/html 视图中使用 spring 管理的指向 静态资源 的链接正常工作,如下所示:

<img src="/public/image.jpg"/>

即使正在运行的应用程序运行时没有错误,Intellisense 也会警告我:

Cannot resolve controller URL '/public/image.jpg'

以编程方式添加资源映射:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/public/**")
            .addResourceLocations("/WEB-INF/resources/");

}

我什至尝试在 spring-mvc 的配置文件中定义映射

<mvc:resources mapping="/public/**" location="/WEB-INF/resources"/>

并通过 @ImportResource 注释在网络环境中导入它:

@ImportResource("/META-INF/spring-resources.xml")

整体 Spring facet 工作正常,但这个。

编辑: 我尝试了所有配置弹簧面的组合 - 没有效果。

pom.xml

<modelVersion>4.0.0</modelVersion>

<groupId>com.name</groupId>
<artifactId>name</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <!--Springframework-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.3.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>1.4.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-bom</artifactId>
            <version>4.2.0.Final</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!--Springframework-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency>

    <!--Servlet Api-->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-servlet-api</artifactId>
        <version>9.0.0.M13</version>
    </dependency>

    <!--JSTL-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!--Bootstrap-->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.1.1</version>
    </dependency>

    <!--DATABASE-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>

    <!--Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
        <scope>compile</scope>
    </dependency>

    <!--TEST-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

【问题讨论】:

  • 检查项目的源文件夹 - 确保 /public/ 文件夹在此处列出 - File &gt; Project Structure 然后在对话框中从左侧面板中选择 Modules;中间面板中的项目(顶层);在右侧面板的“来源”选项卡上
  • @ochi 很抱歉,但我不想手动包含文件夹,我希望 spring facet 通过声明资源映射来自动解决它
  • 我明白了。这是一个maven项目吗?我的项目似乎没有这个问题(但它们都是基于 maven 的)
  • @ochi 是的,它是一个 maven 项目。如果有帮助,我可以添加 pom.xml 到帖子中

标签: java html spring spring-mvc intellij-idea


【解决方案1】:
猜你喜欢
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 2022-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多