资源不可用使用Spring Java Config时出现Tomcat 404错误

时间:2015-01-04 08:02:25

标签: spring spring-mvc tomcat

我正在

  

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

使用Spring Java Config时

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
  4. 当我查看我的控制台时,我看到了

    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 个答案:

答案 0 :(得分:0)

404错误是因为您触发的网址不存在或已映射。请仔细检查您所做的调度员配置。是否映射到/ SpringSecurity。并且还尝试在控制器中单独给予/欢迎而不是**。