3

I have already learned how to configure Spring MVC app with XML so I decided to go ahead.

I read documentation about WebApplicationInitializer and minimizing of XML in application configuration. But when I completed all preparations of the sample application I encountered with 404 page.

Further I put snippets of my code, please give me advices how to make @-based approach properly.

Config file:

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;
    }

}

Initializer:

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>

Controller:

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>

So when I click on "Say Hello" link I get 404. Entire project you can download from my drop-box.

4

1 回答 1

2

我刚刚在您的 Dropbox 中检查了该项目。在我看来,项目的结构是错误的。您将 maven-scructure 与 eclipse-structure 混合在一起。当您使用 Maven 时,您将网络内容放入src/main/webapp......而不是WebContent像您所做的那样。您可以在此处查看有关此主题的更多详细信息。

精简版:

将文件从WebContentto移动并重src/main/webapp试。

长版:

如果您运行mvn package并从目录中提取生成的 *.war /target,您将看到它缺少WebContent目录中的文件。Maven 期望这些文件位于src/main/webapp. 我假设您首先在 Eclipse 中创建了一个“动态 Web 项目”。Eclipse 期望像 *.j​​sp 和 co 这样的资源。位于WebContent,这就是为什么打电话index.jsp工作。但是当涉及到弹簧时它会失败,因为hello-world.jsp它不在它应该在的地方。

怎么修:

首先将文件从移动WebContentsrc/main/webapp. 然后运行mvn eclipse:eclipse -Dwtpversion=2.0。它将生成 Eclipse 的配置(.classpath、.project 等)。在 Eclipse 中刷新项目。现在它应该可以工作了。

于 2012-11-19T20:07:45.510 回答