在Intellij中使用Tomcat时遇到404错误

4

我正在尝试在IntelliJ中运行Tomcat,在IDEA中一切都正常运行,但当我尝试在浏览器中打开相应的页面时,我收到了HTTP状态404 - 未找到的错误提示,其中描述信息为:原始服务器未找到目标资源的当前表示形式,或者不愿意披露其存在。我已经搜索了所有地方,但没有找到答案,希望你能帮忙。

我的代码如下:

@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
}
}

我希望这足以帮助我解决问题。谢谢!
编辑:我在本地主机日志中得到了以下信息:
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()

对于遇到同样问题的人:我无法使这个项目工作,但我使用了来自http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example-annotation/的一个项目,并且在我的Intellij中运行得很好(在我从pom.xml文件中删除插件之后)。

检查浏览器是否启用了代理。 - exexzian
嘿,你解决了吗? - parsecer
1个回答

0
尝试将您的InternalResourceViewResolver的前缀更新为您的视图文件夹:
  resolver.setPrefix("/WEB-INF/views/");

你需要在那个“视图”文件夹中有一个名为hey.jsp的文件。

它仍然不起作用。我也尝试访问localhost:8081/,localhost:8081/{我的项目名称},但它仍然不起作用。如果我点击“部署全部”按钮,我确实可以得到Tomcat欢迎页面,但我仍然无法得到我的页面。 - Alexandra Rujita

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接