WebApplicationInitializer的Spring MVC实现

时间:2012-11-19 13:57:43

标签: spring-mvc

我已经学会了如何使用XML配置Spring MVC应用程序,所以我决定继续。

我阅读了有关WebApplicationInitializer的文档,并最小化了应用程序配置中的XML。但是当我完成404页面遇到的示例应用程序的所有准备工作时。

此外,我提供了我的代码片段,请给我建议如何正确地使用@ -based方法。

配置文件:

package com.onet.init;

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.view.UrlBasedViewResolver;

@Configuration
@ComponentScan("com.onet")
@EnableWebMvc
public class BaseConfig {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

}

初​​始化器:

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

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

public class Initializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(BaseConfig.class);

        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("*.html");
        servlet.setLoadOnStartup(1);
    }

}

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>oneTest</groupId>
    <artifactId>oneTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

</project>

控制器:

package com.onet.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

    @RequestMapping(value="/hello")
    public ModelAndView goToHelloWorld() {
        return new ModelAndView("hello-world");
    }

}

的index.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home page</title>
</head>
<body>
<h1>Home page</h1>
<p>This is a home page.</p>
<p><a href="hello.html">Say Hello</a></p>
</body>
</html>

所以当我点击“Say Hello”链接时,我得到404。 您可以从我的投递箱中download完成整个项目。

1 个答案:

答案 0 :(得分:2)

我刚检查了你的Dropbox中的项目。在我看来,项目的结构是错误的。你混合了maven-scructure和eclipse-structure。当您使用maven时,您将webcontent放在src/main/webapp中...而不是像WebContent那样放在WebContent中。您可以查看here了解有关此主题的更多详细信息。

简短版本:

将文件从src/main/webapp移至mvn package,然后重试。

长版:

如果您运行/target并从WebContent目录中提取生成的* .war,您将看到它缺少src/main/webapp目录中的文件。 Maven期望这些文件位于WebContent。我假设您首先创建了一个&#34; 动态Web项目&#34;在日食。 Eclipse期望像* .jsp和co这样的资源。位于index.jsp,这就是调用hello-world.jsp的原因。但是当涉及到春天它会失败,因为WebContent不在它应该的位置。

如何解决:

首先将文件从src/main/webapp移至mvn eclipse:eclipse -Dwtpversion=2.0。然后运行{{1}}。它将为eclipse生成配置(.classpath,.project等)。在Eclipse中刷新项目。现在应该可以了。