【问题标题】:Cannot access static build.html from my project root无法从我的项目根目录访问静态 build.html
【发布时间】:2016-12-14 19:07:41
【问题描述】:

我正在尝试访问包含构建过程信息的静态 HTML 页面,它是使用 maven war 插件生成的。但是,当我尝试访问在我的项目根目录中生成的build.html 时,什么都没有显示或无法访问。但是,当我尝试访问 src/main/webapp/static 下的任何 HTML 文件时,可以使用 URL:http://localhost:8180/MyProject/static/about/build.html 访问。

但无法通过这种方式访问​​,我可能正在寻找:http://localhost:8180/MyProject/build.html

MyProject.war 中的结构

MyProject.war

META-IN

static

WEB-INF

build.html

Maven 战争插件

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>src/main/webapp/static/about</directory>
                                <filtering>true</filtering>
                            </resource>
                        </webResources>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>

WebConfiguration 类 (web.xml)

public class AppWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { AppWebConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

AppWebConfiguration(弹簧配置)

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mypackages")
public class AppWebConfiguration extends WebMvcConfigurerAdapter {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");

    }


    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

}

Spring 安全配置

 @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
            .authorizeRequests()
            .antMatchers("/login").anonymous()
            .antMatchers("/", "/home").hasRole(ADMIN)
            .antMatchers("/admin/**").hasRole(ADMIN)

            .and().formLogin().loginPage("/login")
            .usernameParameter("ssoId").passwordParameter("password")
            .and().csrf()
            .and().exceptionHandling().accessDeniedPage("/errorpage");  

        }

请提出建议。提前致谢!

【问题讨论】:

  • 请出示您的 web.xml
  • 谢谢。。我又添加了几个配置文件,请指教。

标签: spring spring-mvc spring-boot spring-security maven-plugin


【解决方案1】:
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/");

【讨论】:

  • 不走运!我相信只有在文件位于 WEB-INF、registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/"); 时,以下行才会有帮助。如果它在 WEB-INF 之外呢?由于 build.html 是在根级别或 WEB-INF 之外生成的
  • 我的 Spring Security 是否会导致访问 build.html 出现问题?
  • 是的,因为spring security,所以你要覆盖目标目录吗?你可以配置&lt;targetPath&gt;yourPath e.g.WEB-INF&lt;/targetPath&gt;Detail see here
  • 是的,我正在尝试覆盖目标目录。我最初是在我的 maven war 插件配置中使用 src/main/webapp ,但这并没有创建 build.html。但是,当我设置为 WEB-INF 时,它正在创建 build.html 文件,但当我尝试通过 url localhost/MyProject/build.html 访问时无法访问
  • 如果我不使用 targetPath,那么它会在 WEB-INF 之外创建文件,该文件位于根级别,就像我上面提到的关于我的战争的结构,但无法使用 url localhost 访问/MyProject/build.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
相关资源
最近更新 更多