当前位置: 首页 > 知识库问答 >
问题:

Spring Boot调度程序servlet无法找到/映射静态内容

谷梁博易
2023-03-14

我在用SpringBoot运行SpringMVC应用程序时遇到了问题。应用程序能够调用映射的控制器并成功解析视图,但无法解析静态内容。

我读过,Spring启动自动看到以下文件夹的静态资源:-静态公共Web_INF/资源等,

我曾尝试将我的文件保存在以下地方,但仍然没有帮助。

我在控制台上的输出是:

内部控制器 2016-08-23 17:22:12.002 [http-nio-9990-exec-1] [org.springframework.web.servlet.PageNotFoundn] [WARN ] org.springframework.web.servlet.PageNotFound: {} - 在名为“dispatcherServlet”的 DispatcherServlet 中找不到带有 URI [/springBootApp/templates/hello.html] 的 HTTP 请求的映射

其中springBootApp是我的上下文根

以下是我的课程:- Spring引导课程:-

@SpringBootApplication
public class ServiceApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(ServiceApplication.class);
        Set<ApplicationContextInitializer<?>> initLst = app.getInitializers();
        initLst.add(new PropertyPasswordDecodingContextInitializer());
        app.setInitializers(initLst);

        ApplicationContext ctx = app.run(args);

        System.out.println("Services Started");
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        application = application.initializers(new PropertyPasswordDecodingContextInitializer());

        return application.sources(ServiceApplication.class);
    }

    @Override
    protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
        //project specific config
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }   

}

MVCConfigClass:

@EnableWebMvc
@Configuration
@ComponentScan(basePackageClasses = ServiceApplication.class, includeFilters = @Filter(Controller.class), useDefaultFilters = false)
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Bean
    public ViewResolver configureViewResolver() {
        InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
        viewResolve.setPrefix("/templates/");
        viewResolve.setSuffix(".html");

        return viewResolve;
    }

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

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

Restful Web服务类-

@RestController
@RequestMapping(value = "/myapp")
public class TestWebService {

    @RequestMapping(value = { "/hello" }, method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView list(HttpServletRequest request) throws Exception {
        // HttpSession session = request.getSession();
        System.out.println("Inside Controller");
        ModelAndView modelView = new ModelAndView();
        ModelMap modelMap = modelView.getModelMap();
        modelView.setViewName("/hello");
        return modelView;
    }

}

我在调用时得到上述输出:-http://localhost:9990/springBootApp/myapp/hello

共有2个答案

郝乐心
2023-03-14

Spring boot自动在文件夹src/main/resources中找到资源。但是如果您为资源处理实现WebMvcConfigurationSupport方法,那么它将删除资源处理方法的默认实现。

董弘新
2023-03-14

扩展<code>WebMvcConfigurerAdapter</code>解决了问题

 类似资料:
  • 我正在尝试使用Spring MVC使用资源。我有一个WebApplicationContextConfig类,它扩展了WebMVCConfigureAdapter,并在其中重写了addResourceHandlers方法: 在文件夹资源中,我有三个图像。但是当我在浏览器中获取一些图像时,例如:http://localhost:8080/img/P123.jpeg我得到了404错误。 我错过什么了吗

  • Web 容器需要本章描述的映射技术去映射客户端请求到 Servlet(该规范2.5以前的版本,使用这些映射技术是作为一个建议而不是要求,允许servlet 容器各有其不同的策略用于映射客户端请求到 servlet)。

  • 我无法访问静态内容(angular app),甚至无法访问简单的索引。来自spring boot的html文件。我一直收到404错误。Spring没有为我提供这些静态文件。自从升级到Spring Boot 2.2.4后,我就遇到了这个问题。我必须升级以应对Zip64问题。 我的application.properties里有这样一句话: 我也有自己的staticResourceConfigurat

  • 我想创建Spring boot Web应用程序。 我有两个静态html文件:一个。html,两个。html。 我想将它们映射如下 不使用模板引擎(Thymeleaf)。 如何做到这一点?我已经尝试了很多方法来实现这一点,但我有404个错误或500个错误(循环视图路径[one.html]:将调度回当前处理程序URL)。 OneController.java是: 项目结构为

  • 问题内容: 我无法编译以下代码: 出现以下错误: 无法从静态内容引用非静态方法calcArea(int,int) 这是什么意思?我该如何解决该问题..? 编辑: 根据您的建议,我创建了一个新的test()实例,如下所示: 这样对吗?如果我这样做有什么区别… 问题答案: Nanne提出的建议绝对可以解决您的问题。但是,我认为如果现在就养成习惯,在学习Java的早期阶段,尝试尽可能少地使用静态方法(例

  • 问题内容: 我正在尝试将请求映射到servlet根(正确的术语?)。我正在将URL映射到正确的视图,但是找不到页面的一部分的所有静态内容-CSS,JavaScript,图像。 所以在我的web.xml中,我的servlet标记看起来像这样 我的控制器看起来像这样: MyBaseController非常简单。看起来像这样: 我在视图层中使用了Tiles。我的配置如下: /WEB-INF/tiles-