无法使用Spring MVC中的AbstractAnnotationConfigDispatcherServletInitializer加载主视图

时间:2013-10-08 12:12:39

标签: spring-mvc maven-3 spring-3

我是Spring MVC的新手,我在eclipse中使用maven创建了spring 3.2项目。我正在为基于java的配置实现AbstractAnnotationConfigDispatcherServletInitializer类。

我在pom.xml中有以下插件和依赖项

<build>
        <plugins>
            <!--source level should be 1.6 (which is not Maven default) for java EE 
                6 projects, so let's change it -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <!-- When using xml-less approach, you need to disable Maven's warning 
                about missing web.xml -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
      <!--We need servlet API for compiling the classes. Not needed in runtime
-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--adding spring mvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.4.RELEASE</version>            
        </dependency>       
        <!-- Add Taglib support -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>           
    </dependencies>

初始化程序类为......

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer{

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

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebappConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }       
}

WebappConfig类是......

@Configuration
@ComponentScan(basePackages={"com.sandip.controllers"})
@EnableWebMvc
public class WebappConfig extends WebMvcConfigurerAdapter{


    //add view Resolver, Tell SpingMVC where to find view scripts
        @Bean
        public InternalResourceViewResolver setupViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");

            return resolver;
        }
}

最后我有一个HomeController ..........

@Controller
public class HomeContoller {      

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String home() {
    return "home";
  }  
}

当我在maven构建后使用jetty运行此项目时,它会在浏览器中显示以下输出.....

enter image description here

springZeroXml是我的项目名称,在控制台中没有错误,请帮助..........

1 个答案:

答案 0 :(得分:1)

我做了大量的googleing并发现我需要覆盖WebAppConfig中WebMvcConfigurerAdapter类的addViewControllers方法。代码是..........

@Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("home");
    }