【问题标题】:unable to load home view using AbstractAnnotationConfigDispatcherServletInitializer in Spring MVC无法在 Spring MVC 中使用 AbstractAnnotationConfigDispatcherServletInitializer 加载主视图
【发布时间】:2013-10-08 12:12:38
【问题描述】:

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

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

【问题讨论】:

  • 你在问什么?当您向/ 发出请求时会发生什么?
  • 我想在应用启动时使用 home.jsp,当我向 / 它在浏览器中显示一些我上面提到的消息时,请最后签入
  • 你要部署到哪台服务器上?
  • 我使用 jetty 在 tomcat 7 上部署

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多