eclipse+maven springMVC搭建
1.新建项目: 选择Maven Project
选择项目位置,这里我选择的是C:\Users\admin\workspace\practice
选择maven项目类型,这里选择webapp:
填写Group Id 和Artifact Id:
我这里不知道为什么,建立项目后,源代码文件夹有三个却只显示了一个,这里我把他们全部删除并重新创建四个源代码文件夹如下:
右键项目-properties-Deployment Assembly:
删除两个test文件夹以及target文件夹,使之成为下面的样子:
配置web.xml:
主要是filter以及servlet的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 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"> 4 <display-name>practice2</display-name> 5 6 <filter> 7 <filter-name>CharacterEncodingFilter</filter-name> 8 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 9 <init-param> 10 <param-name>encoding</param-name> 11 <param-value>utf-8</param-value> 12 </init-param> 13 </filter> 14 <filter-mapping> 15 <filter-name>CharacterEncodingFilter</filter-name> 16 <url-pattern>/*</url-pattern> 17 </filter-mapping> 18 19 <servlet> 20 <servlet-name>practice2</servlet-name> 21 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 22 <init-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>classpath:spring-servlet.xml</param-value> 25 </init-param> 26 <load-on-startup>1</load-on-startup> 27 </servlet> 28 <servlet-mapping> 29 <servlet-name>practice2</servlet-name> 30 <url-pattern>/</url-pattern> 31 </servlet-mapping> 32 33 </web-app>
在resources文件夹下创建spring-servlet.xml文件(此处文件名与web.xml中的配置相同即可,但后缀一定是 -servlet.xml)
配置spring-mvc.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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" 3 xsi:schemaLocation=" 4 http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context 7 http://www.springframework.org/schema/context/spring-context.xsd 8 http://www.springframework.org/schema/tx 9 http://www.springframework.org/schema/tx/spring-tx.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop.xsd 12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 14 15 16 <mvc:annotation-driven /> 17 <!-- 该配置为自动扫描配置的包下所有使用@Controller注解的类 --> 18 <context:component-scan base-package="" />(base-packge暂且留白) 19 <!-- 跳转页面使用,如果不配置的话,那么我们springmvc返回页面的时候,会被认为是请求url处理,所以就无法到达想要跳转的页面。这是因为web.xml里面配置的访问路径为'/'也就是所有访问路径都被认为是请求url --> 20 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" p:order="5" /> 21 <mvc:resources mapping="/js/**" location="/WEB-INF/js/" /> 22 <mvc:resources mapping="/css/**" location="/WEB-INF/css/" /> 23 <mvc:resources mapping="/images/**" location="/WEB-INF/images/" /> 24 <mvc:resources mapping="/assets/js/**" location="/WEB-INF/assets/js/" /> 25 <mvc:resources mapping="/assets/css/**" location="/WEB-INF/assets/css/" /> 26 <mvc:resources mapping="/assets/fonts/**" location="/WEB-INF/assets/fonts/" /> 27 <mvc:resources mapping="/view/**" location="/WEB-INF/view/" /> 28 </beans>
此时项目结构如下:
配置 pom.xml文件,引入依赖:
1 <dependencies> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>3.8.1</version> 6 <scope>test</scope> 7 </dependency> 8 <dependency> 9 <groupId>org.springframework</groupId> 10 <artifactId>spring-web</artifactId> 11 <version>4.2.4.RELEASE</version> 12 </dependency> 13 <dependency> 14 <groupId>org.springframework</groupId> 15 <artifactId>spring-webmvc</artifactId> 16 <version>4.2.4.RELEASE</version> 17 </dependency> 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId>spring-core</artifactId> 21 <version>4.2.4.RELEASE</version> 22 </dependency> 23 <dependency> 24 <groupId>org.apache.geronimo.specs</groupId> 25 <artifactId>geronimo-servlet_2.5_spec</artifactId> 26 <version>1.2</version> 27 </dependency> 28 </dependencies>
在src/main/java文件夹下创建目录:com.practice.practice2.controller
顺便把之前留白的base-packge路径改成:com.practice.practice2.*
在目录下创建TestController:
1 package com.practice.practice2.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.ModelMap; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.servlet.ModelAndView; 10 11 @Controller 12 @RequestMapping(value = "practice/") 13 public class TestController { 14 15 @RequestMapping(value = "index") 16 public ModelAndView showIndex(HttpServletRequest request,HttpServletResponse response){ 17 ModelMap map = new ModelMap(); 18 String string = "Welcome to my page!"; 19 map.put("string", string); 20 return new ModelAndView("/index",map); 21 } 22 23 }
在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>
此时项目源文件目录如下:
把practice2项目放入tomcat:
显示如下:
搭建成功!