1.新建项目: 选择Maven Project
选择项目位置,这里我选择的是C:\Users\admin\workspace\practice
选择maven项目类型,这里选择webapp:
填写Group Id 和Artifact Id:
我这里不知道为什么,建立项目后,源代码文件夹有三个却只显示了一个,这里我把他们全部删除并重新创建四个源代码文件夹如下:
右键项目-properties-Deployment Assembly:
删除两个test文件夹以及target文件夹,使之成为下面的样子:
配置web.xml:
主要是filter以及servlet的配置
 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>practice2</display-name> <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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet>
<servlet-name>practice2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>practice2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

web.xml

在resources文件夹下创建spring-servlet.xml文件(此处文件名与web.xml中的配置相同即可,但后缀一定是 -servlet.xml)
配置spring-mvc.xml:
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven />
<!-- 该配置为自动扫描配置的包下所有使用@Controller注解的类 -->
<context:component-scan base-package="" />(base-packge暂且留白)
<!-- 跳转页面使用,如果不配置的话,那么我们springmvc返回页面的时候,会被认为是请求url处理,所以就无法到达想要跳转的页面。这是因为web.xml里面配置的访问路径为'/'也就是所有访问路径都被认为是请求url -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" p:order="5" />
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
<mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
<mvc:resources mapping="/assets/js/**" location="/WEB-INF/assets/js/" />
<mvc:resources mapping="/assets/css/**" location="/WEB-INF/assets/css/" />
<mvc:resources mapping="/assets/fonts/**" location="/WEB-INF/assets/fonts/" />
<mvc:resources mapping="/view/**" location="/WEB-INF/view/" />
</beans>

spring-servlet.xml

此时项目结构如下:
配置 pom.xml文件,引入依赖:
   <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_2.5_spec</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

pom.xml

在src/main/java文件夹下创建目录:com.practice.practice2.controller
顺便把之前留白的base-packge路径改成:com.practice.practice2.*
在目录下创建TestController:
 package com.practice.practice2.controller;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "practice/")
public class TestController { @RequestMapping(value = "index")
public ModelAndView showIndex(HttpServletRequest request,HttpServletResponse response){
ModelMap map = new ModelMap();
String string = "Welcome to my page!";
map.put("string", string);
return new ModelAndView("/index",map);
} }

TestController.java

在WEB-INF目录下创建view文件夹
在该文件夹下创建index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${string}
</body>
</html>

index.jsp

此时项目源文件目录如下:
把practice2项目放入tomcat:
显示如下:
搭建成功!

eclipse+maven springMVC搭建的更多相关文章

  1. eclipse +maven+ssm搭建矿建

    记录一下搭建框架的过程1.下载最新的eclipse   https://www.eclipse.org/downloads/download.php?file=/oomph/epp/neon/R/ec ...

  2. 【Web】Eclipse + Maven + Struts搭建服务器

    一.环境 系统:Windows7 IDE:Eclipse-Kepler Service Release 2 使用插件:Maven(请预先在电脑上安装Maven) 二.搭建 在Eclipse中新建一个M ...

  3. Log4j2 配置笔记(Eclipse+maven+SpringMVC)

    Log4j2相关介绍可以百度看下,这里只注重配置Log4j2 能够马上跑起来: 1.pom.xml文件中添加Log4j2的相关Maven配置信息 <!-- log4j2 --> <d ...

  4. windows 环境下 eclipse + maven + tomcat 的 hello world 创建和部署

    主要记录自己一个新手用 eclipse + maven + tomcat 搭建 hello world 的过程,以及遇到的问题.讲真都是自己通过百度和谷歌一步步搭建的项目,没问过高手,也没高手可问,由 ...

  5. eclipse下maven插件搭建springmvc之helloworld

    这几天学习了怎样使用maven,最终还是要回归web项目嘛,所以主要还是使用eclipse插件. 1 下载eclipse maven插件. 其实新版的eclipse已经集成了maven:lunar.m ...

  6. Eclipse利用Maven2搭建SpringMVC框架的Web工程

    一.准备工作: 下载apache-maven--> 配置Maven_home -->下载Eclipse Maven插件 二.新建工程:   选择新建Maven Project  arche ...

  7. 使用intellij idea搭建MAVEN+springmvc+mybatis框架

    原文:使用intellij idea搭建MAVEN+springmvc+mybatis框架 1.首先使用idea创建一个maven项目 2.接着配置pom.xml,以下为我的配置 <projec ...

  8. Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)

    Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...

  9. JavaEE开发基于Eclipse的环境搭建以及Maven Web App的创建

    本篇博客就完整的来聊一下如何在Eclipse中创建的Maven Project.本篇博客是JavaEE开发的开篇,也是基础.本篇博客的内容干货还是比较多的,而且比较实用,并且都是采用目前最新版本的工具 ...

随机推荐

  1. SQL 触发器的缺点 坏处 弊端 哼╭(╯^╰)╮

    (自己总结,有误请不吝赐教) 1.如果触发频率高,占用内存,降低数据访问速度 2.相对不灵活,一旦触发马上执行,不能排除特殊情况 3.一定程度上打乱代码结构,相关的代码都需要特别注释,否则造成阅读和维 ...

  2. hihocode 股票价格 优先队列+map

    股票价格 时间限制:20000ms 单点时限:2000ms 内存限制:256MB 描述 小Hi最近在分析一支股票的价格走势,他需要一个程序来辅助分析.这个程序会接收3种消息(指令): 价格信息,格式是 ...

  3. 雷林鹏分享:Ruby File 类和方法

    Ruby File 类和方法 File 表示一个连接到普通文件的 stdio 对象.open 为普通文件返回该类的一个实例. 类方法 序号方法 & 描述 1File::atime( path) ...

  4. CentOS下安装docker,docker-compose

    1.查看系统发行版本: lsb_release -a 2.安装docker:Docker 是一个开源的应用容器引擎,可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行 ...

  5. 通过git-bash一句话获得当前目录的全部csproj文件绝对路径

    #!/usr/bin/env bash %.sh}.txt 保存为 csprojfilelist.sh,注意换行符使用LF,如果git-bash关联了sh文件,直接双击就可以得到csprojfilel ...

  6. HDU-5492 Find a path (枚举+DP)

    Problem Description Frog fell into a maze. This maze is a rectangle containing N rows and M columns. ...

  7. call()与apply()用法

    call()和apply()的作用都是一样的——通过改变函数体内部 this 的指向,借用对象的方法的目的 还是举个栗子吧: function Cat(){ this.food = 'fish'; t ...

  8. 知识梳理——CSS篇

    css引入方法 内嵌 <head> <meta charset="UTF-8"> <title>Document</title> & ...

  9. MVC4 绑定下拉框方法,解决编辑时不绑定值

    方法一  Controller 部分代码: public ActionResult Modify(int id) { //3.1.1 检查id //3.1.2根据id查询数据 Models.Stude ...

  10. FireFox和IE下使用Date来构造新Date对象的BUG

    正常方式 我们都知道可以使用new Date()来创建一个Date对象. new Date();//Date {Mon Jun 15 2015 15:53:16 GMT+0800} 也可以用new D ...