相关疑难解决方法(0)

为什么Spring MVC用404响应并报告"在DispatcherServlet中找不到带有URI [...]的HTTP请求的映射"?

我正在编写一个部署在Tomcat上的Spring MVC应用程序.请参阅以下最小,完整且可验证的示例

public class Application extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { };
    }
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { SpringServletConfig.class };
    }
    protected String[] getServletMappings() {
        return new String[] { "/*" };
    }
}
Run Code Online (Sandbox Code Playgroud)

哪里SpringServletConfig

@Configuration
@ComponentScan("com.example.controllers")
@EnableWebMvc
public class SpringServletConfig {
    @Bean
    public InternalResourceViewResolver resolver() {
        InternalResourceViewResolver vr = new InternalResourceViewResolver();
        vr.setPrefix("/WEB-INF/jsps/");
        vr.setSuffix(".jsp");
        return vr;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,我有一个@Controllercom.example.controllers

@Controller
public class ExampleController {
    @RequestMapping(path = "/home", …
Run Code Online (Sandbox Code Playgroud)

java spring servlets spring-mvc

82
推荐指数
2
解决办法
5万
查看次数

使用Spring Boot和annotations配置ViewResolver为HTTP请求找不到URI错误的映射

我正在尝试使用gradle,spring boot和spring mvc以最简单的视图解析器和html制作"hello world"应用程序.

我从thymeleaf spring boot示例开始,我只想删除thymeleaf,使用纯html和InternalResourceViewResolver创建一个更简单的mvc应用程序.我有一个greeting.html我想要服务,它位于src/main/webapp/WEB-INF.当我运行应用程序时,我得到了

No mapping found for HTTP request with URI [/WEB-INF/greeting.html] in DispatcherServlet with name 'dispatcherServlet'
Run Code Online (Sandbox Code Playgroud)

这是一个常见的错误,网上有很多答案,但似乎没有任何帮助.

这是我的Application.java

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的GreetingController.java

@Controller
public class GreetingController {
    @RequestMapping("/greeting")
    public String greeting() {
        return "greeting";
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的MvcConfiguration.java

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".html");
        return resolver;
    }
}
Run Code Online (Sandbox Code Playgroud)

我用它来运行它 …

java spring spring-mvc gradle spring-boot

31
推荐指数
3
解决办法
9万
查看次数

标签 统计

java ×2

spring ×2

spring-mvc ×2

gradle ×1

servlets ×1

spring-boot ×1