为什么春季mvc调度员不会参加?

时间:2012-10-01 12:40:43

标签: java web-applications spring-mvc

我只是尝试设置我的第一个spring-mvc应用程序,以便我可以使用框架。

但是,转到此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

1 个答案:

答案 0 :(得分:0)

此模式

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

只会使数学表达式以htm和此url结尾:

localhost:8080/springmvctest/home

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

<强>解决方案:

更改

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

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

在您的控件中使用

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