我已经学会了如何用XML配置Spring应用程序,所以我决定继续。
我阅读了关于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>所以当我点击"Say Hello“链接时,我会得到404。整个项目,你可以下载从我的拖放框。
发布于 2012-11-19 20:07:45
我刚在你的dropbox里查过了这个项目。在我看来,这个项目的结构是错误的。你把maven-scructure和eclipse-结构混在一起。当你使用maven时,你把网页内容放在src/main/webapp中.在WebContent里可不像你那样。有关此主题的更多细节,您可以查看这里。
简写版:
将文件从WebContent移动到src/main/webapp,然后重试。
长版本:
如果运行mvn package并从/target目录提取结果*.war,您将看到它缺少WebContent目录中的文件。Maven希望这些文件在src/main/webapp中。我假设您从创建eclipse中的“动态Web项目”开始。Eclipse期待像*.jsp和co这样的资源。要位于WebContent中,这就是为什么调用index.jsp工作的原因。但是当涉及到春季时,它就失败了,因为hello-world.jsp不在它应该在的位置。
如何修复:
从将文件从WebContent移动到src/main/webapp开始。然后运行mvn eclipse:eclipse -Dwtpversion=2.0。它将生成eclipse的配置(.classpath、.project等)。在Eclipse中刷新项目。现在该起作用了。
https://stackoverflow.com/questions/13455314
复制相似问题