eclipse使用maven创建springmvc项目

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

一、创建maven的web应用

eclipse->file->new->other

创建maven项目步骤一
创建maven项目步骤二
创建maven项目步骤三
创建maven项目步骤四

二、在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对应上

  1. 创建spring的配置文件,并添加上aop、beans、context、mvc这四个命名空间
  2. 在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>

hello

世界你好

```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值