0

我是 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 构建后使用码头运行我的项目时,它会在浏览器中显示以下输出.....

在此处输入图像描述

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

4

1 回答 1

1

我做了很多谷歌搜索,发现我需要在 WebAppConfig 中覆盖 WebMvcConfigurerAdapter 类的 addViewControllers 方法。和代码是............

@Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("home");
    }
于 2013-10-13T19:01:03.360 回答