加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在intellij上从tomcat获取错误404

发布时间:2020-12-15 02:19:02 所属栏目:Java 来源:网络整理
导读:我试图在intellij中运行tomcat,在IDEA中一切运行正常但是当我尝试在浏览器中打开相应的页面时我得到 HTTP状态404 – 找不到描述源服务器没有找到目标资源的当前表示,或者不愿意透露存在该资源.我到处寻找并没有找到答案,我希望你能提供帮助. 我的代码如下:
我试图在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
}
}

我希望这足以帮助解决我的问题.谢谢 !

编辑:我在Localhost日志中收到以下消息:

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中删除了插件之后)

解决方法

尝试将您的InternalResourceViewResolver的前缀更新到您的views文件夹:

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

你需要在那个views文件夹中有hey.jsp.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读