oto*_*nan 31 java spring spring-mvc gradle spring-boot
我正在尝试使用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)
我用它来运行它 gradle bootRun
这是代码的repo:https://github.com/driver-pete/spring-mvc-example
以下是一些更多线索:
我的假设是调度程序servlet以某种方式被配置为在/*而不是/喜欢这里和任何地方服务.但是我没有web.xml,因此这些建议不适用于此处.我看到很多例子如何以编程方式配置调度程序servlet,但我想保持我的应用程序最小化,我怀疑spring boot应该配置好,因为它与thymeleaf一起工作正常.
Bij*_*men 47
您只需要启用默认servlet,这可以通过将以下内容添加到您的MvcConfiguration
:
@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".html");
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Run Code Online (Sandbox Code Playgroud)
基本上发生了什么是Spring不知道如何本地处理这样的内容(可能是一个jsp说),并且这种配置是告诉它将它委托给容器的方法.
oto*_*nan 13
在研究了更多之后,我发现了一种可以在不添加configureDefaultServletHandling方法的情况下工作的替代解决方案.您需要向build.gradle添加嵌入式tomcat jsp引擎:
compile("org.apache.tomcat.embed:tomcat-embed-jasper")
Run Code Online (Sandbox Code Playgroud)
与configureDefaultServletHandling方法相反,此解决方案不仅适用于普通html,还适用于jsp.
所有解决方案均可在以下网址获得:https://github.com/driver-pete/spring-mvc-example 此解决方案可在master上获得.Biju的解决方案是在DefaultServletHandling_solution分支上.
小智 9
View-resolver也可以在application.properties
Spring-Boot Web应用程序的文件中配置,如下所示:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
88559 次 |
最近记录: |