eclipse新建maven工程(web工程)

参考:https://www.cnblogs.com/zzvar/articles/14596750.html

下载与安装maven

提示:下面是maven3.6.3版本百度云链接,记住maven3.3以上版本必须安装jdk1.7及以上版本,否则会出错。
链接:https://pan.baidu.com/s/1J0FNIBlF0JIm3QB9sIIlaA
提取码:4gud

或者:https://archive.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip

直接解压下载的maven-3.6.3,如

 

 

 1. 配置环境变量:变量名为MAVEN_HOME 变量值为maven的解压路径。如

 

 

2. 设置path,找到path——编辑path变量——在弹出的框中点击新建—— %MAVEN_HOME%\bin——确定。

3. 检验maven的配置情况:点击开始(或win10桌面左下脚任务栏的搜索框或者直接win+R)——在搜索框中输入cmd——回车 

输入 mvn -version

显示如下,表示安装成功。

 

 

 

4. 配置setting.xml。新建自己的maven仓库下载的位置。和配置阿里maven镜像。新建maven工程默认的jdk版本。

找到你maven的安装路径,进入conf文件夹,用编辑器打开setting.xml文件。

配置maven本地仓库:
maven本地仓库也可以不配置,如果不配置,默认本地仓库地址为C:\Users\Administrator\.m2\repository
配置方法:我们直接找到52行,然后复制第53行代码,粘贴到注释外面(必须粘贴到注释外面,不然是无效代码),最后把<localRepository></localRepository>中间的路径修改成你自己创建的本地仓库路径就好了

配置maven阿里云镜像:
接下来我们需要配置maven镜像,如果我们不配置,我们下载jar包是从国外下载的,下载速度很慢,所以我们配置一下国内的阿里云镜像
配置方法:找到未在注释标签内的mirrors,大约在145行左右,接着我们往mirrors标签内添加阿里云镜像,然后保存

<mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>

配置maven全局jdk版本
接下来我们可以为maven配置全局的jdk,这样我们在创建maven项目时就会自动使用该jdk版本,我配置的是jdk1.8
配置方法:找到未在注释标签内的<profiles></profiles>,滑到最底部往上找很容易找到,然后在他们之中添加指定jdk代码,和配置阿里云镜像类似。

<!-- java版本 --> 
<profile>
  <id>jdk-1.8</id>
  <activation>
    <activeByDefault>true</activeByDefault>
    <jdk>1.8</jdk>
  </activation>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
  </properties>
</profile>

 



 

在eclipse中配置自己安装的maven

 

 

2.

 

 

 新建maven工程(Web工程)

 

 

 

 

 

 

 

 

 

2、调整web目录结构,在项目上点右键 -> properties->Project Facets -> 把Dynamic Web Module 勾选去掉,并Apply -> 将Dynamic Web Module 重新勾选 -> 点击Further configuration available -> 修改 Content directory为src/main/webapp -> Apply 即在Maven工程上生成动态Web目录结构

 

 

 

 

 

 

 

 


3、新建jsp文件发现报错:The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
因为缺少Tomcat 运行时环境,可以添加compile path -》 add library -> tomact 运行时。 或者直接添加servlet-api的maven配置:

    <!-- ServletAPI -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

 

最后的结果,类似

 

 --------------------------

如何使用eclipse运行Maven项目,以及运行maven项目出现的各种错误

看2

 


 

常见springmvc的配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">


    <!--配置springMVC的编码过滤器 -->
    <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>forceResponseEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 -->
        <init-param>
            <!-- contextConfigLocation为固定值 -->
            <param-name>contextConfigLocation</param-name>
            <!-- 使用classpath:表示从类路径查找配置文件,例如maven工程中的 src/main/resources -->
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <!--设置springMVC的核心控制器所能处理的请求的请求路径 /所匹配的请求可以是/login或.html或.js或.css方式的请求路径 
            但是/不能匹配.jsp请求路径的请求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springMVC.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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    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:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- 配置组件扫描 -->
    <context:component-scan base-package="cn.love" />
    <!-- 配置MVC注解扫描 -->
    <mvc:annotation-driven />
    <!-- 配置视图解析器, -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/" /><!-- 前缀 -->
        <property name="suffix" value=".jsp" /><!-- 后缀 -->
    </bean>

    <!-- 配置Thymeleaf视图解析器
    <bean id="viewResolver"
        class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1" />
        <property name="characterEncoding" value="UTF-8" />
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <property name="prefix" value="/WEB-INF/templates/" />
                        <property name="suffix" value=".html" />
                        <property name="templateMode" value="HTML5" />
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
     -->
    <!-- 配置拦截器
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/*"/>
            <bean class="interceptors.SomeInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors> -->
</beans>

 

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.love</groupId>
  <artifactId>MvnDemo01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
    <!-- SpringMVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.1</version>
    </dependency> 
    <!-- 日志 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency> 
    <!-- ServletAPI
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency> -->
    <!-- Spring5和Thymeleaf整合包
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.12.RELEASE</version>
    </dependency> -->
  </dependencies>
</project>

 

 

 

 

 

 



 

posted @ 2021-11-14 14:33  htj10  阅读(163)  评论(0编辑  收藏  举报
TOP