Eclipse中创建Maven的SpringMVC项目

Eclipse for jee下载地址http://mirror.neu.edu.cn/eclipse/technology/epp/downloads/release/indigo/SR1/eclipse-jee-indigo-SR1-win32.zip
Eclipse中下载Maven插件地址:http://m2eclipse.sonatype.org/sites/m2e

Eclipse中在线更新Maven插件网址:http://m2eclipse.codehaus.org/update/


1、建立maven项目:

1.1、Create a Maven Project


 

 

1.2、Select project name and location

选择creat a simple project和Use default workspace location,点击next:


 

 

1.3、configure project:


 

1.4创建后的项目结构:



 

2、向maven项目中添加jar包

2.1 在pom.xml中添加所需要的jar包

打开pom.xml文件,选择Dependencies视图,以添加spring-web的jar包为例,在Dependencies下点击Add,输入spring-web,选择3.0.3版本的spring:


 

 

2.2 修改jar包的Dependency Details中的scope



 
当添加一个jar包时,有些scope属性需要修改:

compile:缺省值,适用于所有阶段,会随项目一起发布。
provided:类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
runtime:只在运行时使用,如JDBC驱动,适用运行和测试阶段。
test:只在测试时使用,用于编译和运行测试代码,不会随项目发布。
system:类似provided,需要显式提供包含依赖的jar,Maven不会在 Repository中查找它。


2.3 本项目中添加的jar包如下:



 
3、编辑配置文件:

3.1、编写web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	                         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	     version="2.5" >
	
	<!--Spring的log4j监听器-->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>

	<!--字符集 过滤器 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- Spring控制器 -->
	<servlet>
		<servlet-name>theDispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/theDispatcher-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>theDispatcher</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

</web-app>

 

3.2、编写Spring配置文件theDispatcher-servlet.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	   xmlns:aop="http://www.springframework.org/schema/aop" 
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:mvc="http://www.springframework.org/schema/mvc" 
	   xmlns:tx="http://www.springframework.org/schema/tx" 
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<mvc:annotation-driven />
	<context:component-scan base-package="com.well" />

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>



4、编写一个Controller代码:

 

package com.well;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MyController {

	@RequestMapping(value="index.do", method = RequestMethod.GET)
	public void index_jsp(Model model){
		model.addAttribute("well", "HelloWorld");
		System.out.println("index.jsp");
	}
}


5、编写index.jsp页面

   在/webapp/WEB-INF/jsp下创建index.jsp 
 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>index</title>
	</head>
	
	<body>
		<c:out value="${well}"></c:out>
	</body>
</html>

 

6、tomcat中部署testMaven

右击tomcat服务器---Add and Remove---选择testMaven--Add--Finish


 

 

7、测试地址:http://localhost:8080/testMaven/index.do



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值