Java – get Error 404 from Tomcat on IntelliJ

I tried to run Tomcat in IntelliJ, and everything worked well in idea, but when I tried to open the corresponding page in the browser, I got

@EnableWebMvc
@Configuration
@ComponentScan({"com.asign.controller"})
public class WebConfig extends WebMvcConfigurerAdapter {

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

@Bean
public InternalResourceViewResolver resolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setViewClass(JstlView.class);
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".jsp");

    return resolver;
}
}


@Configuration
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return null;//new Class<?>[]{RootConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[]{WebConfig.class};
}

@Override
protected String[] getServletMappings() {
    return new String[]{"/"};
}
}

@Controller
public class AppController {

@RequestMapping(value = "/")
public String helloWorld(Model model) {
    //let’s pass some variables to the view script
    model.addAttribute("wisdom","Goodbye XML");

    return "hey"; // renders /WEB-INF/views/hey.jsp
}
}

I hope this is enough to help solve my problem thank you!

Edit: I received the following message in the localhost log:

INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()

For anyone facing the same problem: I can't make this project effective, but I use that http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example-annotation/ It works well in my IntelliJ (after I removed the plug-in from POM. XML)

Solution

Try to update the prefix of your internalresourceviewresolver to your views folder:

resolver.setPrefix("/WEB-INF/views/");

You need to have hey in that views folder jsp.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>