我正在编写一个部署在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)
最后,我有一个@Controller包com.example.controllers
@Controller
public class ExampleController {
@RequestMapping(path = "/home", …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用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)
我用它来运行它 …