在servlet'DispatcherServlet'中找不到HandlerMappings:使用默认

时间:2015-07-22 11:20:16

标签: java spring spring-mvc servlets

我正在尝试使用Spring MVC(入门项目STS 3.7.0)进行webapp基本项目。 Java 1.7
WebConfig.java

package com.terafast;

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

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.terafast")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver getInternalViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/view/");
        resolver.setSuffix(".jsp");

        return resolver;
    }
}

WebAppInitializer.java

package com.terafast;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("*.html");
    }

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new  AnnotationConfigWebApplicationContext();
        context.setConfigLocation("com.terafast.WebConfig");

        return context;
    }

}

的HomeController

package com.terafast.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {
    @RequestMapping(value = "/index")
    public String index(Model model) {

        return "welcome";
    }
}

我在webapp / WEB-INF / view / path中创建了welcome.jsp。当我将项目作为服务器运行时,http://localhost:8080/TEM/上没有任何内容。当我使用http://localhost:8080/TEM/index.html时,我也什么都没有。

18:18:29.911 [http-nio-8080-exec-4] WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/TEM/index] in DispatcherServlet with name 'DispatcherServlet' 
18:18:29.911 [http-nio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request

enter image description here 有人能解释一下这是什么问题吗?

1 个答案:

答案 0 :(得分:0)

嘿,我遇到同样的问题,你可以做的是转到你的项目属性>> DeploymentAssembly然后将DispatcherServlet库添加到任何位置。

这将解决您的问题。