【问题标题】:XML-free Spring 3.1 No mapping found for HTTP request无 XML 的 Spring 3.1 找不到 HTTP 请求的映射
【发布时间】:2012-11-14 05:05:12
【问题描述】:

我已经搜索了 google、stackoverflow 和我可以查看的每个论坛几天,我的键盘很可能会成为头槌的目标。

我正在运行一个非常小的 Spring 3.1 MVC,它没有 XML 设置。问题是当我启动它时,我看到了;

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/start.action],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.xxxxxx.info.HomeController.start(javax.servlet.http.HttpServletRequest)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/*],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.xxxxxx.info.HomeController.home(java.util.Locale,org.springframework.ui.Model)

然而,当我尝试访问其中任何一个 URL 时,我看到我的控制器内的日志语句触发,然后立即获取;

INFO : com.xxxxxx.info.HomeController - Welcome home! the client locale is en_US
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WEB-INF/jsps/home.jsp] in DispatcherServlet with name 'dispatcher'

这是我的源文件。

初始化器 -

public class Initializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
        mvcContext.register(MvcConfig.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
    }
}

MvcConfig -

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.xxxxxx.info")    
public class MvcConfig {
    @Bean
    public InternalResourceViewResolver configureInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsps/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

控制器 -

@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /** Simply selects the home view to render by returning its name. */
    @RequestMapping(value = "/*")
    public ModelAndView home(Locale locale, Model model) {
        logger.info("Welcome home! the client locale is " + locale.toString());

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate);
        return new ModelAndView( "home", model.asMap() );
    }

    @RequestMapping( value = "/start.action")
    public ModelAndView start(HttpServletRequest request) {
        logger.info("Starting!");
        return new ModelAndView( "start", null );
    }
}

将调度程序映射更改为“/”,正如我发现的许多帖子所建议的那样,似乎完全破坏了它。如果我用“/”重新启动服务器,那么 Spring 不会报告任何内容,也不会映射任何内容,只有一个 tomcat 启动日志,其他任何操作都没有。

我的文件结构是 -

| com.xxxxxx.info
    - Initializer.java
    - MvcConfig.java
    - HomeController.java
| src
    | main
        | webapp
            | WEB-INF
                | jsps
                    - home.jsp
                    - start.jsp

所以它似乎正确地击中了我的控制器,但是当它获得视图名称时,它并没有解析到正确的位置。我在这里错过了什么,这似乎是我忽略的一些简单的事情......

【问题讨论】:

    标签: java spring spring-mvc tomcat7 url-mapping


    【解决方案1】:

    我猜这个jsp是由DispatcherServlet渲染的,在similar question中讨论过

    您应该考虑在HomeControllerWebApplicationInitializer 中使用更具体的映射。

    【讨论】:

    • @Sarophym 不清楚调度程序 servlet 的 / 映射会出现什么问题,您应该首先检查控制器映射是否适用于非 root 请求,例如 hello.html
    • 非常感谢您的链接。那里的解决方案已经死了。我将调度员代码更改为dispatcher.addMapping("*.action");
    【解决方案2】:

    您正在向DispatcherServlet 和您的HomeController 发送\*

    dispatcher.addMapping("/*");
    
    @RequestMapping(value = "/*")
        public ModelAndView home(Locale locale, Model model)
    

    我认为这是造成问题的原因。将您的控制器映射更改为类似

    @RequestMapping(value = "/home")
        public ModelAndView home(Locale locale, Model model)
    

    希望对你有帮助。

    【讨论】:

    • 这并没有解决问题,但我认为您遇到了正确的问题,事实证明更改调度程序映射解决了问题。
    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多