【Maven】Eclipse 使用Maven创建SpringMVC Web项目

时间:2023-03-08 16:56:03
  • 创建环境

  系统:win 10

  软件:eclipse,maven.

创建步骤

  •   创建一个Maven Web项目,可以参照:【Maven】Eclipse 使用Maven创建Java Web项目
  •   添加spring依赖的jar包,在pom文件中设置依赖即可
     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hd</groupId>
    <artifactId>test-spring</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version> <!-- 定义maven变量 -->
    <properties>
    <!-- spring -->
    <spring.version>4.0.0.RELEASE</spring.version> <!-- log -->
    <commons-logging.version>1.1.3</commons-logging.version> <!-- Servlet -->
    <servlet.version>3.0.1</servlet.version>
    <jsp-api.version>2.2</jsp-api.version> <!-- jstl -->
    <jstl.version>1.2</jstl.version>
    <standard.version>1.1.2</standard.version> <!-- test -->
    <junit.version>3.8.1</junit.version> <!-- jdk -->
    <jdk.version>1.7</jdk.version>
    <maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version>
    </properties> <dependencies> <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
    </dependency> <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
    </dependency> <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
    </dependency> <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
    </dependency> <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>${spring.version}</version>
    </dependency> <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
    </dependency> <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
    </dependency> <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
    </dependency> <!-- Servlet -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>${servlet.version}</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>${jsp-api.version}</version>
    <scope>provided</scope>
    </dependency> <!-- jstl -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>${jstl.version}</version>
    </dependency> <dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>${standard.version}</version>
    </dependency> <!-- test -->
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
    </dependency> </dependencies> <build>
    <plugins>
    <!-- define the project compile level -->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven.compiler.plugin.version}</version>
    <configuration>
    <source>${jdk.version}</source>
    <target>${jdk.version}</target>
    </configuration>
    </plugin>
    </plugins>
    <finalName>test-spring</finalName>
    </build>
    </project>

    pom.xml

  •   编辑web.xml文件
     <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>test-spring</display-name> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring-context.xml</param-value>
    </context-param> <!-- Spring配置 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> <!-- Spring MVC配置 -->
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml -->
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet> <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping> <!-- 中文过滤器 -->
    <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> <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>
  •   在src/main/resources中新建一个文件夹spring,在spring文件夹中新建一个spring-context.xml的配置文件,进行相关配置

    【Maven】Eclipse 使用Maven创建SpringMVC Web项目

 <?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.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-4.0.xsd"> <!-- 注解注册 -->
<context:annotation-config /> <context:component-scan base-package="com.test" /> </beans>

spring-content.xml

  • 在src/main/resources/spring,新建一个spring-mvc.xml的配置文件,进行相关配置

    【Maven】Eclipse 使用Maven创建SpringMVC Web项目

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 自动扫描的包名 -->
<context:component-scan base-package="com.test.controller" /> <!-- 默认的注解映射的支持 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class="org.springframework.http.converter.ResourceHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven> <!-- 视图解释类,定义跳转的文件的前后缀 -->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"
/> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp"
/> <property name="requestContextAttribute" value="rc" /> </bean> -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean> <!-- 拦截器 -->
<!-- <mvc:interceptors> <bean class="com.fpx.ce.foundation.preference.PreferenceChangeInterceptor"
/> <mvc:interceptor> <mvc:mapping path="/page/home"/> <bean class="com.fpx.ce.foundation.user.UserInterceptor"
/> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/service/**" />
<bean class="com.fpx.ce.foundation.log.LogHandlerInterceptor" /> </mvc:interceptor>
<mvc:interceptor> <mvc:mapping path="/test/**" /> <mvc:mapping path="/service/**"
/> <bean class="com.lemon.finder.web.SharedRenderVariableInterceptor" />
</mvc:interceptor> </mvc:interceptors> --> <!-- 对静态资源文件的访问 方案一 (二选一) -->
<mvc:default-servlet-handler /> <!-- 对静态资源文件的访问 方案二 (二选一) -->
<!-- <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/> -->
</beans>

spring-mvc

  •   在src/main/java的com.test.controller中新建一个类

    【Maven】Eclipse 使用Maven创建SpringMVC Web项目  

 package com.test.controller;

 import javax.servlet.http.HttpServletRequest;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/testController")
public class TestController { @RequestMapping(value="/getView")
@ResponseBody
public ModelAndView getTest(HttpServletRequest request){
ModelAndView modelAndView = new ModelAndView("test-jsp");
return modelAndView;
}
}
  •   在webapp中新建一个文件夹view,在view中新建一个test-jsp.jsp

    【Maven】Eclipse 使用Maven创建SpringMVC Web项目  

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
Hello World!!!
</body>
</html>
  •   搭建完成,如下图

    【Maven】Eclipse 使用Maven创建SpringMVC Web项目

  •   发布到tomcat中,并启动tomcat

    【Maven】Eclipse 使用Maven创建SpringMVC Web项目

  •   在浏览器中访问 http://localhost:8080/test-spring/testController/getView

    【Maven】Eclipse 使用Maven创建SpringMVC Web项目