zoukankan      html  css  js  c++  java
  • 【Maven】Eclipse 使用Maven创建SpringMVC Web项目

    • 创建环境

      系统:win 10

      软件:eclipse,maven.

    创建步骤

    •   创建一个Maven Web项目,可以参照:【Maven】Eclipse 使用Maven创建Java Web项目
    •   添加spring依赖的jar包,在pom文件中设置依赖即可
        1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        3     <modelVersion>4.0.0</modelVersion>
        4     <groupId>com.hd</groupId>
        5     <artifactId>test-spring</artifactId>
        6     <packaging>war</packaging>
        7     <version>0.0.1-SNAPSHOT</version>
        8 
        9 
       10     <!-- 定义maven变量 -->
       11     <properties>
       12         <!-- spring -->
       13         <spring.version>4.0.0.RELEASE</spring.version>
       14 
       15         <!-- log -->
       16         <commons-logging.version>1.1.3</commons-logging.version>
       17 
       18         <!-- Servlet -->
       19         <servlet.version>3.0.1</servlet.version>
       20         <jsp-api.version>2.2</jsp-api.version>
       21 
       22         <!-- jstl -->
       23         <jstl.version>1.2</jstl.version>
       24         <standard.version>1.1.2</standard.version>
       25 
       26         <!-- test -->
       27         <junit.version>3.8.1</junit.version>
       28 
       29         <!-- jdk -->
       30         <jdk.version>1.7</jdk.version>
       31         <maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version>
       32     </properties>
       33 
       34 
       35     <dependencies>
       36 
       37         <dependency>
       38             <groupId>org.springframework</groupId>
       39             <artifactId>spring-core</artifactId>
       40             <version>${spring.version}</version>
       41         </dependency>
       42 
       43         <dependency>
       44             <groupId>org.springframework</groupId>
       45             <artifactId>spring-beans</artifactId>
       46             <version>${spring.version}</version>
       47         </dependency>
       48 
       49         <dependency>
       50             <groupId>org.springframework</groupId>
       51             <artifactId>spring-context</artifactId>
       52             <version>${spring.version}</version>
       53         </dependency>
       54 
       55         <dependency>
       56             <groupId>org.springframework</groupId>
       57             <artifactId>spring-jdbc</artifactId>
       58             <version>${spring.version}</version>
       59         </dependency>
       60 
       61 
       62         <dependency>
       63             <groupId>org.springframework</groupId>
       64             <artifactId>spring-expression</artifactId>
       65             <version>${spring.version}</version>
       66         </dependency>
       67 
       68         <dependency>
       69             <groupId>org.springframework</groupId>
       70             <artifactId>spring-web</artifactId>
       71             <version>${spring.version}</version>
       72         </dependency>
       73 
       74         <dependency>
       75             <groupId>org.springframework</groupId>
       76             <artifactId>spring-webmvc</artifactId>
       77             <version>${spring.version}</version>
       78         </dependency>
       79 
       80         <dependency>
       81             <groupId>org.springframework</groupId>
       82             <artifactId>spring-tx</artifactId>
       83             <version>${spring.version}</version>
       84         </dependency>
       85 
       86 
       87 
       88 
       89         <!-- Servlet -->
       90         <dependency>
       91             <groupId>javax.servlet</groupId>
       92             <artifactId>javax.servlet-api</artifactId>
       93             <version>${servlet.version}</version>
       94             <scope>provided</scope>
       95         </dependency>
       96         <dependency>
       97             <groupId>javax.servlet.jsp</groupId>
       98             <artifactId>jsp-api</artifactId>
       99             <version>${jsp-api.version}</version>
      100             <scope>provided</scope>
      101         </dependency>
      102 
      103         <!-- jstl -->
      104         <dependency>
      105             <groupId>javax.servlet</groupId>
      106             <artifactId>jstl</artifactId>
      107             <version>${jstl.version}</version>
      108         </dependency>
      109 
      110         <dependency>
      111             <groupId>taglibs</groupId>
      112             <artifactId>standard</artifactId>
      113             <version>${standard.version}</version>
      114         </dependency>
      115 
      116         <!-- test -->
      117         <dependency>
      118             <groupId>junit</groupId>
      119             <artifactId>junit</artifactId>
      120             <version>${junit.version}</version>
      121             <scope>test</scope>
      122         </dependency>
      123 
      124     </dependencies>
      125 
      126 
      127     <build>
      128         <plugins>
      129             <!-- define the project compile level -->
      130             <plugin>
      131                 <groupId>org.apache.maven.plugins</groupId>
      132                 <artifactId>maven-compiler-plugin</artifactId>
      133                 <version>${maven.compiler.plugin.version}</version>
      134                 <configuration>
      135                     <source>${jdk.version}</source>
      136                     <target>${jdk.version}</target>
      137                 </configuration>
      138             </plugin>
      139         </plugins>
      140         <finalName>test-spring</finalName>
      141     </build>
      142 </project>
      pom.xml
    •   编辑web.xml文件
       1 <?xml version="1.0" encoding="UTF-8"?>
       2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       3     xmlns="http://java.sun.com/xml/ns/javaee"
       4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       5     id="WebApp_ID" version="3.0">
       6     <display-name>test-spring</display-name>
       7 
       8     <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
       9     <context-param>
      10         <param-name>contextConfigLocation</param-name>
      11         <param-value>classpath:spring/spring-context.xml</param-value>
      12     </context-param>
      13 
      14     <!-- Spring配置 -->
      15     <listener>
      16         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      17     </listener>
      18 
      19     <!-- Spring MVC配置 -->
      20     <servlet>
      21         <servlet-name>spring</servlet-name>
      22         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      23         <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml -->
      24         <init-param>
      25             <param-name>contextConfigLocation</param-name>
      26             <param-value>classpath:spring/spring-mvc.xml</param-value>
      27         </init-param>
      28         <load-on-startup>1</load-on-startup>
      29     </servlet>
      30 
      31     <servlet-mapping>
      32         <servlet-name>spring</servlet-name>
      33         <url-pattern>/</url-pattern>
      34     </servlet-mapping>
      35 
      36     <!-- 中文过滤器 -->
      37     <filter>
      38         <filter-name>CharacterEncodingFilter</filter-name>
      39         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      40         <init-param>
      41             <param-name>encoding</param-name>
      42             <param-value>UTF-8</param-value>
      43         </init-param>
      44         <init-param>
      45             <param-name>forceEncoding</param-name>
      46             <param-value>true</param-value>
      47         </init-param>
      48     </filter>
      49     <filter-mapping>
      50         <filter-name>CharacterEncodingFilter</filter-name>
      51         <url-pattern>/*</url-pattern>
      52     </filter-mapping>
      53 
      54     <welcome-file-list>
      55         <welcome-file>index.jsp</welcome-file>
      56     </welcome-file-list>
      57 </web-app>
    •   在src/main/resources中新建一个文件夹spring,在spring文件夹中新建一个spring-context.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:mvc="http://www.springframework.org/schema/mvc"
     3     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
     4     xmlns:aop="http://www.springframework.org/schema/aop"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     6         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd    
     7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
     8         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
     9         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    10         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    11 
    12     
    13     <!-- 注解注册 -->
    14     <context:annotation-config />
    15     
    16     <context:component-scan base-package="com.test" />
    17     
    18 </beans>
    spring-content.xml
    •     在src/main/resources/spring,新建一个spring-mvc.xml的配置文件,进行相关配置

        

     1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:context="http://www.springframework.org/schema/context"
     4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
     5     xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
     8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
     9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    10 
    11     <!-- 自动扫描的包名 -->
    12     <context:component-scan base-package="com.test.controller" />
    13 
    14     <!-- 默认的注解映射的支持 -->
    15     <mvc:annotation-driven>
    16         <mvc:message-converters>
    17             <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
    18             <bean
    19                 class="org.springframework.http.converter.ResourceHttpMessageConverter" />
    20         </mvc:message-converters>
    21     </mvc:annotation-driven>
    22 
    23 
    24 
    25     <!-- 视图解释类,定义跳转的文件的前后缀 -->
    26     <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    27         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" 
    28         /> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" 
    29         /> <property name="requestContextAttribute" value="rc" /> </bean> -->
    30     <bean id="viewResolver"
    31         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    32         <property name="viewClass"
    33             value="org.springframework.web.servlet.view.JstlView" />
    34         <property name="prefix" value="/view/" />
    35         <property name="suffix" value=".jsp" />
    36     </bean>
    37 
    38 
    39     <!-- 拦截器 -->
    40     <!-- <mvc:interceptors> <bean class="com.fpx.ce.foundation.preference.PreferenceChangeInterceptor" 
    41         /> <mvc:interceptor> <mvc:mapping path="/page/home"/> <bean class="com.fpx.ce.foundation.user.UserInterceptor" 
    42         /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/service/**" /> 
    43         <bean class="com.fpx.ce.foundation.log.LogHandlerInterceptor" /> </mvc:interceptor> 
    44         <mvc:interceptor> <mvc:mapping path="/test/**" /> <mvc:mapping path="/service/**" 
    45         /> <bean class="com.lemon.finder.web.SharedRenderVariableInterceptor" /> 
    46         </mvc:interceptor> </mvc:interceptors> -->
    47 
    48 
    49     <!-- 对静态资源文件的访问 方案一 (二选一) -->
    50     <mvc:default-servlet-handler />
    51 
    52     <!-- 对静态资源文件的访问 方案二 (二选一) -->
    53     <!-- <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/> 
    54         <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/> 
    55         <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/> -->
    56 </beans>
    spring-mvc
    •   在src/main/java的com.test.controller中新建一个类

          

     1 package com.test.controller;
     2 
     3 
     4 import javax.servlet.http.HttpServletRequest;
     5 
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.ResponseBody;
     9 import org.springframework.web.servlet.ModelAndView;
    10 
    11 @Controller
    12 @RequestMapping("/testController")
    13 public class TestController {
    14 
    15     @RequestMapping(value="/getView")
    16     @ResponseBody
    17     public ModelAndView getTest(HttpServletRequest request){
    18         ModelAndView modelAndView = new ModelAndView("test-jsp");
    19         return modelAndView;
    20     }
    21 }
    •   在webapp中新建一个文件夹view,在view中新建一个test-jsp.jsp

          

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     Hello World!!!
    11 </body>
    12 </html>
    •   搭建完成,如下图

        

    •   发布到tomcat中,并启动tomcat

        

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

        

  • 相关阅读:
    python enhanced generator - coroutine
    python yield generator 详解
    gunicorn syncworker 源码解析
    gunicorn 信号处理(SIGHUP,SIGUSR2)
    gunicorn Arbiter 源码解析
    gunicorn 简介
    kafka+zookeeper环境配置(linux环境单机版)
    在Linux中安装JDK的步骤
    Kafka安装及部署
    Zookeeper 安装和配置
  • 原文地址:https://www.cnblogs.com/h--d/p/6054813.html
Copyright © 2011-2022 走看看