【问题标题】:Resource Not Available Tomcat 404 error when using Spring Java Config使用 Spring Java Config 时资源不可用 Tomcat 404 错误
【发布时间】:2015-01-04 16:36:07
【问题描述】:

我来了

“请求的资源(/SpringSecurity/welcome)不可用。”

使用 Spring Java 配置时

SpringInitializer.java

package com.security.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpringInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    // TODO Auto-generated method stub
    return new Class[] { AppConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected String[] getServletMappings() {
    // TODO Auto-generated method stub
    return new String[] { "/" };
}
}

AppConfig.java

package com.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc
@Configuration
@ComponentScan({ "com.security" })
@Import({ SecurityConfig.class })
public class AppConfig {
  @Bean
  public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/jsp/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
  }
}

带有映射的HelloController 类

package com.security.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

@RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public ModelAndView welcomePage() {
    ModelAndView model = new ModelAndView();
    model.addObject("title", "Spring Security Hello World");
    model.addObject("message", "This is welcome page!");
    model.setViewName("hello");
    return model;

}
}
  1. 我的 JSP 文件位于“/WEB-INF/jsp/”
  2. 我在控制台中没有收到任何错误
  3. 我使用的是 Tomcat 7

当我检查我的控制台时,我看到了

INFO: Mapped "{[/ || /welcome**],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.security.controller.HelloController.welcomePage()

所以映射似乎已经完成。但是当我尝试使用 url 访问时,我的 tomcat 服务器仍然显示 404 错误:http://localhost:8081/SpringSecurity/welcome

我还尝试了“清理 Tomcat 工作目录”。但我不知道问题所在。

【问题讨论】:

  • 1.检查jsp是否真的在你的战争中。 2. 您可以随时从 Controller 更深入地调试,以查看异常真正发生的位置。
  • 您提出请求时是否收到任何日志?还是什么都没有?

标签: spring spring-mvc tomcat


【解决方案1】:

404 错误是因为您触发的 url 不存在或未映射。因此,请检查您所做的 dispatcherservlet 配置。它是否映射到 /SpringSecurity。并尝试在控制器中单独提供 /welcome 而不是 **。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 2015-08-20
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多