zoukankan      html  css  js  c++  java
  • Eclipse+maven 构建第一个简单的springmvc项目

    先给出项目的目录:

    在eclipse下使用maven构建第一个springmvc项目步骤如下:

    1.创建maven project(此处默认你已了解maven),此处需要注意以下两点

     

     

    2.创建完毕后会看到一个 pom.xml的配置文件,此时需要引入spring web mvc的相关maven依赖,具体版本请看:MVNRepository ,一般,在这里,你可以搜索相关的maven依赖,copy到pom.xml文件即可

    (copy保存后就会下载相关的包了)

    我的pom.xml如下:

     1 <project xmlns="http://maven.apache.org/POM/4.0.0"
     2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5     <groupId>com.example</groupId>
     6     <artifactId>springmvc-maven</artifactId>
     7     <packaging>war</packaging>  <!-- 使用的是war包 -->
     8     <version>0.0.1-SNAPSHOT</version>
     9     <name>springmvc-maven Maven Webapp</name>
    10     <url>http://maven.apache.org</url>
    11     <dependencies>
    12         <dependency>
    13             <groupId>junit</groupId>
    14             <artifactId>junit</artifactId>
    15             <version>3.8.1</version>
    16             <scope>test</scope>
    17         </dependency>
    18 
    19         <dependency>
    20             <groupId>javax.servlet</groupId>
    21             <artifactId>javax.servlet-api</artifactId>
    22             <version>4.0.1</version>
    23             <scope>provided</scope>
    24         </dependency>
    25 
    26         <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    27         <!-- 导入springmvc的相关依赖,RELEASE是稳定版 -->
    28         <dependency>
    29             <groupId>org.springframework</groupId>
    30             <artifactId>spring-webmvc</artifactId>
    31             <version>5.1.5.RELEASE</version>
    32         </dependency>
    33 
    34     </dependencies>
    35 
    36     <!-- 配置maven插件 -->
    37     <build>
    38         <!-- java编译插件 -->
    39         <!-- eclipse默认使用的jdk是1.5的 -->
    40         <plugins>
    41             <plugin>
    42                 <groupId>org.apache.maven.plugins</groupId>
    43                 <artifactId>maven-compiler-plugin</artifactId>
    44                 <version>3.2</version>
    45                 <configuration>
    46                     <source>1.8</source>
    47                     <target>1.8</target>
    48                     <encoding>UTF-8</encoding>
    49                 </configuration>
    50             </plugin>
    51         </plugins>
    52     </build>
    53 
    54     <!-- <build>
    55         <finalName>springmvc-maven</finalName>
    56     </build> -->
    57 </project>

     

     

    3.配置web.xml文件,此时的web.xml在WEB-INF 目录下,在本例子中web.xml主要是servlet的基本配置。如下:

     1 <!DOCTYPE web-app PUBLIC
     2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
     4 
     5 <web-app>
     6     <display-name>Archetype Created Web Application</display-name>
     7 
     8     <context-param>
     9         <param-name>contextConfigLocation</param-name>
    10         <param-value>classpath:example-servlet.xml</param-value>
    11     </context-param>
    12     <!--  Could not open ServletContext resource [/WEB-INF/example-servlet.xml] -->
    13     <!-- 不过这个貌似不是必须的?只要下面那个就可以?虽然会waring? -->
    14     <!-- servlet的配置 -->
    15     <servlet>
    16         <servlet-name>example</servlet-name>
    17         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    18         <!-- 在web.xml里配置需要加载的spring配置文件。 如果要装入多个配置文件,在<param-value>标记中用逗号作分隔符即可。 -->
    19         <init-param>
    20             <param-name>contextConfigLocation</param-name>
    21             <param-value>classpath:example-servlet.xml</param-value>
    22         </init-param>
    23 
    24         <load-on-startup>1</load-on-startup>  <!-- 配置servlet的启动时刻 -->
    25     </servlet>
    26     <servlet-mapping>
    27         <servlet-name>example</servlet-name>
    28         <url-pattern>/</url-pattern>  <!-- 系统中的请求经过的 -->
    29     </servlet-mapping>
    30 
    31 
    32 </web-app>

    解析一下上面代码:

    第16行,建立一个名为example的servlet,根据官方文档,你要建立一个与之对应的example-servlet.xml(记住这点)

    第24行,好像是决定servlet的启动时刻,是随着服务器启动还是等请求到来才启动,上面的设置是随着服务器启动而启动(这点不是很确定)

    第8-11行和19-22行结合使用,因为此时使用的是maven来管理项目,它有个专门存放资源文件的目录 src/main/resources,上面数说的建立的example-servlet.xml不是很往常一样,直接放在WEB-INF目录下,而是放在src/main/resources下,这两部分是为了避免出现<!--  Could not open ServletContext resource [/WEB-INF/example-servlet.xml] --> 这个错误(建议读一下官方文档

     

     

    4.接着就是在src/main/resources目录下建立example-servlet.xml配置文件,并进行以下的配置

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:p="http://www.springframework.org/schema/p"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xsi:schemaLocation="
     7         http://www.springframework.org/schema/beans
     8         https://www.springframework.org/schema/beans/spring-beans.xsd
     9         http://www.springframework.org/schema/context
    10         https://www.springframework.org/schema/context/spring-context.xsd">
    11 
    12     <!-- 配置controller层路径扫描与视图解析器 -->
    13     <!--扫描controller所在的包 -->
    14     <context:component-scan
    15         base-package="com.example.springweb.mvc" />
    16 
    17     <!-- 视图解析器 -->
    18     <bean
    19         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    20         <property name="prefix" value="/WEB-INF/jsp/" />  <!--前缀-->
    21         <property name="suffix" value=".jsp" />     <!-- 后缀 -->
    22     </bean>
    23 
    24 
    25     <!-- ... -->
    26 </beans>

    上面的xml中,12行-22行是新增的配置,剩余部分都是基本的,其实这部分你可以在官网找到的

     

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

     

    在example-servlet.xml中有这样一句配置

    <context:component-scan
            base-package="com.example.springweb.mvc" />

    因此,要在src/main/java下新建一个 com.example.springweb.mvc 包,并在其下建立controller类

     

    5.建立controller类

    在src/main/java下新建一个名为 com.example.springweb.mvc 的包,并新建一个名为IndexController的java类。

    IndexController.java

    package com.example.springweb.mvc;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class IndexController {
    
        @RequestMapping("/home")   //这里路由映射为/home,所以http://localhost:8080/springmvc-maven/不能访问到
        public String home() {    //这 里方法是Sring类型,因此要在WEB-INF创建一个home.jsp的页面
            return "home";
        }
    
    }

     

    此时在IndexController.java定义了一个String类型返回值的方法,对应的要在 在WEB-INF创建一个名为home.jsp的页面(原理我也不懂)

    <%@ 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>
    <h1>你好</h1>
    </body>
    </html>

     

    最后,右键项目Run on Server 即可。

     

     

     

  • 相关阅读:
    使用awk根据多维度统计系统tps
    Java实现身份证号码校验
    Java分布式数据导出实践
    Java服务器端消息队列实战
    Java获取当前服务器IP实现
    Jvm dump介绍与使用(内存与线程)
    Java进行身份证格式强校验(准)
    Java性能监控之Instrumentation
    基于linux操作系统安装、使用memcached详解
    基于linux操作系统安装、使用redis详解
  • 原文地址:https://www.cnblogs.com/Guhongying/p/10613644.html
Copyright © 2011-2022 走看看