2012-10-01 47 views
0

我只是试图设置我的第一个spring-mvc应用程序,所以我可以玩这个框架。为什么spring-mvc dispatcherservlet无法启动?

但是,去这个URL:localhost:8080/springmvctest/home我得到一个404错误。但是,去localhost:8080/springmvctest我确实看到欢迎页面(index.jsp)

我在这里做错了什么?

Web.xml中

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>springmvctest</display-name> 
    <servlet> 
    <servlet-name>DispatcherServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/app-config.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>DispatcherServlet</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

应用-config.xml中

<!-- Scans the classpath of this application for @Components to deploy as beans --> 
<context:component-scan base-package="controller" /> 

<!-- Configures the @Controller programming model --> 
<mvc:annotation-driven /> 

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> 
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 

控制器

@Controller 
public class TestController { 

    @RequestMapping(value = {"/home","/","/index"}, method = RequestMethod.GET) 
    public String hello() { 
     return "home"; 
    } 
} 

针对home.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1>Hello home!</h1> 
    </body> 
</html> 

我的文件结构

├── pom.xml 
├── src 
│   ├── main 
│   │   ├── java 
│   │   │   └── com 
│   │   │    └── something 
│   │   │     └── springmvctest 
│   │   │      ├── testclass.java 
│   │   │      └── TestController.java 
│   │   └── webapp 
│   │    ├── index.jsp 
│   │    └── WEB-INF 
│   │     ├── app-config.xml 
│   │     ├── jsp 
│   │     │   └── home.jsp 
│   │     └── web.xml 
│   └── test 
│    └── java 
│     └── com 
│      └── something 
│       └── springmvctest 

回答

0

这种模式

<url-pattern>*.htm</url-pattern> 

不仅使在HTM结束的数学表达式和这个网址:

localhost:8080/springmvctest/home 

没有结束HTM因此它没有采取调度的春天。

解决方案:

变化

<url-pattern>*.html</url-pattern> 

<url-pattern>/*</url-pattern> 

或者

在你的控制

使用

@RequestMapping(value = {“/home.htm","/index.htm”},method = RequestMethod.GET)

+0

@Mike此解决方案适合您吗? – Jhonathan

相关问题