我只是想设置我的第一个 spring-mvc 应用程序,以便我可以使用该框架。
但是,转到此网址: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>
应用程序配置.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";
}
}
主页.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