2015-11-01 80 views
0

嗨,我是新的Spring MVC 4我想做的Java配置設置,但它似乎春天沒有找到我的控制器它運行良好沒有啓動錯誤我甚至可以明確地調用一個jsp,但如果我試圖打電話給我的控制器,它什麼也不做春天MVC 4配置找不到我的控制器

eg localhost:8080/apollo/hello.jsp < - 這使它的罰款,如果我把我的JSP的Web應用程序目錄

我要的是用我的控制器來叫我的login.jsp

我的項目結構是

com 
    +apollo 
    -WebAppInitializer.java 
    -WebConfig.java 
src 
+main 
    +webapp 
    -**hello.jsp** 
    +WEB-INF 
     +view 
     -**login.jsp** 

這裏是我的配置

package com.apollo;  
import java.util.Locale; 
import org.springframework.context.MessageSource; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.support.ResourceBundleMessageSource; 
import org.springframework.web.servlet.LocaleResolver; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; 
import org.springframework.web.servlet.i18n.SessionLocaleResolver; 
import org.springframework.web.servlet.view.InternalResourceViewResolver; 

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

    @Bean 
    public MessageSource messageSource() { 
     ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); 
     messageSource.setBasename("messages"); 
     return messageSource; 
    }  

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

     return resolver; 
    } 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     /*registry.addResourceHandler("/pdfs/**").addResourceLocations("/WEB-INF/pdf/"); 
      registry.addResourceHandler("/css/**").addResourceLocations("/WEB-INF/css/");*/ 
     registry.addResourceHandler("/static/**").addResourceLocations("/static/"); 
    } 

} 

這裏是我的初始化器類

package com.apollo; 

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.register(WebConfig.class); 
     return context; 
    } 

} 

這裏是我的控制器

package com.apollo.controller; 

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


@Controller 
public class LoginController { 

    @RequestMapping(value="/login") 
    public String greeting (Model model){ 
     System.out.println("controller???"); 
     model.addAttribute("greeting" , "Hello World"); 
     return "login"; 
    } 
} 

回答

0

basePackages意味着包含控制器

現在的問題是:

@ComponentScan(basePackages = "com.apollo") 

英語新HOULD變化

@ComponentScan(basePackages = "com.apollo.controller") 

如果你把控制器模塊,您應該掃描這樣

@ComponentScan(basePackages = "com.apollo.**.controller")