2013-10-28 210 views
0

我知道類似的問題已被問到,但我似乎無法找到與我的配置相同的問題。Tomcat + Spring錯誤404

我有一個Eclipse Java EE項目設置了Maven和春天。我跟着這些教程進行設置: http://fruzenshtein.com/setup-of-dynamic-web-project-using-maven/ http://fruzenshtein.com/spring-mvc-creation-of-simple-controller-with-java-based-config/

當我啓動的項目,我得到一個錯誤404

的教程沒有提到調度-servlet.xml中的創造,所以也許是是問題,但我不確定我是否需要Maven。

我的配置如下:

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>FindLove</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

的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>FindLove</groupId> 
    <artifactId>FindLove</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <properties> 
    <spring.version>3.1.1.RELEASE</spring.version> 
    </properties> 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.1</version> 
     <configuration> 
        <source>${jdk.version}</source> 
        <target>${jdk.version}</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.3</version> 
     <configuration> 
      <warSourceDirectory>WebContent</warSourceDirectory> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
    <dependencies> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <!-- CGLIB is required to process @Configuration classes --> 
    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib</artifactId> 
     <version>2.2.2</version> 
    </dependency> 
    <!-- Servlet API, JSTL --> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>jstl</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 
</dependencies> 
</project> 

配置類

@Configuration //Specifies the class as configuration 
@ComponentScan("com.sprmvc") //Specifies which package to scan 
@EnableWebMvc //Enables to use Spring's annotations in the code 
public class WebAppConfig { 

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

} 

初始化器類

public class Initializer implements WebApplicationInitializer { 

    public void onStartup(ServletContext servletContext) throws ServletException { 

     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 
     ctx.register(WebAppConfig.class); 

     ctx.setServletContext(servletContext);  

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

} 

控制器

@Controller 
public class LinkController { 

    @RequestMapping(value="/hello-page") 
    public ModelAndView goToHelloPage() { 
     ModelAndView view = new ModelAndView(); 
     view.setViewName("hello"); //name of the jsp-file in the "page" folder 

     String str = "MVC Spring is here!"; 
     view.addObject("message", str); //adding of str object as "message" parameter 

     return view; 
    } 

} 

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<h1>Home page</h1> 
<p>This is a Home Page.</p> 
<p><a href="hello-page.html">Hello world link</a></p> 

的hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<p>Hello world: ${message}</p> 
<p>Well done!</p> 

有什麼建議?

在此先感謝

+1

tomcat日誌中的任何錯誤? – NickJ

回答

0

我不認爲

<p><a href="hello-page.html">Hello world link</a></p> 

可以達到Servlet映射到

servlet.addMapping("/"); 

,並調用映射到

@RequestMapping(value="/hello-page") 
一個 @Controller處理方法

更改鏈接

<p><a href="hello-page">Hello world link</a></p> 

理想情況下,你會希望把它放到

<p><a href="<c:url value="/hello-page" />">Hello world link</a></p> 

,使其相關的上下文路徑。不要忘記添加標籤庫聲明。