一、创建maven的web应用
eclipse->file->new->other




二、在web.xml配置DispatcherServlet
在web.xml中添加如下代码:
<!-- DispatcherServlet, Spring MVC的核心 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认的配置文件路径为:/WEB-INF/<servlet-name>-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 项目启动时加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!-- 拦截所有的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
contextConfigLocation参数用来指定springmvc的配置文件路径
默认的配置文件路径为:/WEB-INF/[servlet-name]-servlet.xml
比如我配置的DispatcherServlet的默认配置文件路径就是:/WEB-INF/springDispatcherServlet-servlet.xml
关于contextConfigLocation的spring的原文注释如下
* <p>Passes a "contextConfigLocation" servlet init-param to the context instance,
* parsing it into potentially multiple file paths which can be separated by any
* number of commas and spaces, like "test-servlet.xml, myServlet.xml".
* If not explicitly specified, the context implementation is supposed to build a
* default location from the namespace of the servlet.
*
* <p>Note: In case of multiple config locations, later bean definitions will
* override ones defined in earlier loaded files, at least when using Spring's
* default ApplicationContext implementation. This can be leveraged to
* deliberately override certain bean definitions via an extra XML file.
*
* <p>The default namespace is "'servlet-name'-servlet", e.g. "test-servlet" for a
* servlet-name "test" (leading to a "/WEB-INF/test-servlet.xml" default location
* with XmlWebApplicationContext). The namespace can also be set explicitly via
* the "namespace" servlet init-param.
三、编写springMvc配置文件
注意:springMvc的配置文件要和DispatcherServlet中的contextConfigLocation对应上
- 创建spring的配置文件,并添加上aop、beans、context、mvc这四个命名空间
- 在spring配置文件中加入如下代码
<!-- 配置自定义扫描的包 -->
<context:component-scan base-package="${被扫描的包路径}"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 返回路径的前缀 -->
<property name="prefix" value="/WEB-INF/views/"></property>
<!-- 返回路径的后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
视图解析器会根据配置的返回路径的前缀和后缀生成返回的URL
四、编写控制器
注意:如果该控制器来所在的包没有在配置文件配置的自定义扫描的包里面,该控制器将失去控制器的功能
示例代码:
@Controller
public class Hello {
@RequestMapping("/hello")
public String sayHello() {
System.out.println("hello world");
return "hello";
}
}
@Controller:声明该类是一个控制器
@RequestMapping:该方法的映射路径
五、编写返回的界面
示例代码(/WEB-INF/views/hello.jsp):
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello</title>
</head>
<body>
<h2>世界你好</h2>
</body>
</html>
html>

本文介绍了如何在Eclipse中利用Maven创建一个Spring MVC项目。首先,通过新建Maven Web Application开始,然后配置web.xml以设置DispatcherServlet,并指定其配置文件路径。接着,编写Spring MVC配置文件,包含aop、beans、context、mvc命名空间。控制器的编写需要注意包的扫描,使用@Controller和@RequestMapping注解。最后,创建返回的界面,例如hello.jsp。
695

被折叠的 条评论
为什么被折叠?



