简体   繁体   English

Spring MVC TomCat 配置运行 jsp 和 ZFC35FDC70D5FC69D269883A822C 页面5

[英]Spring MVC TomCat configuration to run jsp and html pages

Running TomCat in Eclipse.在 Eclipse 中运行 TomCat。 I type in the browser http://localhost:8080/springmvc/index.jsp and see " Hello World.".我在浏览器中输入 http://localhost:8080/springmvc/index.jsp 并看到“Hello World.”。

But if I type http://localhost:8080/springmvc/hello, I see a 404 error但是如果我输入 http://localhost:8080/springmvc/hello,我会看到 404 错误

What am I doing wrong?我究竟做错了什么?

My Controller我的 Controller

@Controller
public class WebController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

My Config我的配置

@Configuration
@ComponentScan("ru.neutrino")
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    private final ApplicationContext applicationContext;

    @Autowired
    public WebConfig(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        registry.viewResolver(resolver);
    }
}

You are missing @RequestMapping annotation at the controller class level.您在 controller class 级别缺少 @RequestMapping 注释。

@Controller
@RequestMapping("/web")
public class WebController {

   @GetMapping("/hello")
   public String hello() {
      return "hello";
   }
 }

Try accessing http://localhost:8080/springmvc/web/hello尝试访问 http://localhost:8080/springmvc/web/hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM