2012-11-19 48 views
3

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

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

此外,我把我的代码片段,请给我建议如何使基于@的方法正确。

配置文件:

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> 

所以,当我点击“打招呼”的链接,我得到404 整个项目,你可以从我的下拉框download

+0

我认为你在配置中缺少folloging xml的等价物: Yevgeniy

+0

但我认为如果我使用基于注解的配置方法I不需要使用XML等价物,对吗? –

+0

不知何故,你需要告诉春天要寻找注释类,不是吗? – Yevgeniy

回答

2

我刚刚在您的保存箱中检查了项目。在我看来,这个项目的结构是错误的。你将maven-scructure与eclipse-structure混合在一起。当你使用maven的时候,你把webcontent放在src/main/webapp ......不是像WebContent那样。您可以看看here以了解关于此主题的更多详细信息。

短版:

移动文件从WebContentsrc/main/webapp,然后再试一次。

长版:

如果您运行mvn package/target目录提取形成的*的.war,你会看到它从WebContent目录缺少文件。 Maven期望这些文件位于src/main/webapp。我假设你在eclipse中创建了一个“Dynamic Web Project”。 Eclipse期望像* .jsp和co这样的资源。位于WebContent,这就是为什么拨打index.jsp工作。但是,当它涉及到春天它失败,因为hello-world.jsp不是它应该在的地方。

如何解决:

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

+0

不幸的是,改变项目结构后,我[404](http://dl.dropbox.com/u /29572172/404.jpg),但现在甚至在主页 –

+0

我刚刚建立并运行您的项目和每一个工作都很好......我会将我的答案延长一点点。 – Yevgeniy

+0

谢谢你一步一步的详细解答,但是我觉得自己像个虚拟人物......我完成了你所说的一切,但又一次[没有任何东西](http://dl.dropbox.com/u/29572172/ 404.png) –

相关问题