阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Eclipse里面使用Maven搭建Web工程

108次阅读
没有评论

共计 11864 个字符,预计需要花费 30 分钟才能阅读完成。

本文相关代码下载:

—————————————— 分割线 ——————————————

FTP 地址:ftp://ftp1.linuxidc.com

用户名:ftp1.linuxidc.com

密码:www.linuxidc.com

在 2015 年 LinuxIDC.com\2 月 \Eclipse 里面使用 Maven 搭建 Web 工程

下载方法见 http://www.linuxidc.com/Linux/2013-10/91140.htm

—————————————— 分割线 ——————————————

一、建立 Maven 项目

使用 Eclipse 的 maven 构建一个 web 项目,以构建 SpringMVC 项目为例:

1.1 选择建立 Maven Project

选择 File -> New -> Other,在 New 窗口中选择 Maven -> Maven Project。点击 newxt

Eclipse 里面使用 Maven 搭建 Web 工程

1.2 选择项目路径

Use default Workspace location 默认工作空间。

Eclipse 里面使用 Maven 搭建 Web 工程

1.3 选择项目类型

在 Artifact Id 中选择 maven-archetype-webapp

Eclipse 里面使用 Maven 搭建 Web 工程

1.4 输入 Group ID 和 Artifact ID,以及 Package

Group ID 一般写大项目名称。Artifact ID 是子项目名称。

例如 Spring 的 web 包,Group ID:org.springframework,artifactId:spring-web。

Package 是默认给你建一个包,不写也可以。

Eclipse 里面使用 Maven 搭建 Web 工程

1.5 配置之后的图

Eclipse 里面使用 Maven 搭建 Web 工程

如果这里显示的内容多,一般是 Filters 设置的问题。或 perspective 为 JavaEE 模式,改成 Java 模式就可以了。

二、配置 maven 项目

2.1 添加文件夹

2.1.1 在 src 的 main 目录下面添加 java 目录

2.1.2 在 src 下面添加 test 目录,并且在 test 目录下面添加 java 和 resources 目录

Eclipse 里面使用 Maven 搭建 Web 工程

2.2 修改 class 路径

右键项目,Java Build Path -> Source

下面应该有 4 个文件夹。src/main/java,src/main/resources,src/test/java,src/test/resources。

双击每个文件夹的 Output folder,选择路径。

src/main/java,src/main/resources,选择 target/classes;

src/test/java,src/test/resources, 选择 target/test-classes;

选上 Allow output folders for source folders.

Eclipse 里面使用 Maven 搭建 Web 工程

在此处还要更改:

更改文件夹显示的顺序:点击 Order and Export。

更改 JDK 版本:在 Libraries 双击 JRE System Library,要 1.6 版本。

2.3 把项目变成 Dynamic Web 项目

2.3.1 右键项目,选择 Project Facets,点击 Convert to faceted from

Eclipse 里面使用 Maven 搭建 Web 工程

2.3.2 配置 Project Facets

更改 Dynamic Web Module 的 Version 为 2.5。(3.0 为 Java7 的)。

如果提示错误,可能需要在 Java Compiler 设置 Compiler compliance level 为 1.6。或者需要在此窗口的 Java 的 Version 改成 1.6。

Eclipse 里面使用 Maven 搭建 Web 工程

2.3.3 配置 Modify Faceted Project
点击 Further configuration available…,弹出 Modify Faceted Project 窗口。此处是设置 web.xml 文件的路径,我们输入 src/main/webapp。Generate web.xml deployment descriptor 自动生成 web.xml 文件,可选可不选。

2.4 设置部署程序集 (Web Deployment Assembly)

上面步骤设置完成后,点击 OK,Properties 窗口会关闭,在右键项目打开此窗口。在左侧列表中会出现一个 Deployment Assembly,点击进去后,如下图:

Eclipse 里面使用 Maven 搭建 Web 工程

此处列表是,部署项目时,文件发布的路径。

我们删除 test 的两项,因为 test 是测试使用,并不需要部署。

Hadoop Eclipse 插件编译安装 1.2.0 http://www.linuxidc.com/Linux/2013-07/87428.htm

Hadoop 在 Eclipse 中的插件编译 http://www.linuxidc.com/Linux/2013-04/83295.htm

Hadoop 1.2.1 编译 Eclipse 插件 http://www.linuxidc.com/Linux/2013-10/91666.htm

Ubuntu 13.10 安装 JDK、Eclipse for C/C++(解决全局菜单问题)http://www.linuxidc.com/Linux/2013-11/92305.htm

如何在 Ubuntu 14.04 中安装最新版 Eclipse http://www.linuxidc.com/Linux/2014-08/105090.htm

 

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2015-02/113103p2.htm

三、向 maven 项目中添加 jar 包

maven 可以管理项目依赖的 jar 包,通过 groupID、artifactId 以及版本号可以唯一确定一个 jar 包。这样可以防止老式 Web 项目中 WEB-INF/lib 下 jar 包不一致的问题。并且 maven 还会自动下载添加进的 jar 包所依赖的 jar 包。

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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>liming.maven.example</groupId>
    <artifactId>maven-example</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>maven-example Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-asm</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.0.3.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        
        <dependency>
            <groupId>Javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>maven-example</finalName>
    </build>
</project>

四、构建 SpringMVC 框架

4.1 编辑 web.xml 文件

需要添加 log4j,字符过滤,Spring 的 dispatcher 等。webx.xml 代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd" 
         version="2.5" >
    
    <!-- 区分项目名称,防止默认重名 -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>maven.example.root</param-value>
    </context-param>

    <!-- Spring 的 log4j 监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!-- 字符集 过滤器  -->
    <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>

    <!-- Spring view 分发器 -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>

4.2 编写 Spring 配置文件 dispatcher-servlet.xml

如要添加 MVC 驱动、注解检测、视图解析等。dispatcher-servlet.xml 代码如下:

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

    <mvc:annotation-driven />
    <context:component-scan base-package="com.qunar.check" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

4.3 编写一个 Controller 层测试类

编写一个 SpringMVC 的 Controller 层测试类。此类只有一个方法做地址映射,并向页面传递一个数据。代码如下:

package Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {@RequestMapping(value = "index.do")
    public void index_jsp(Model model) {model.addAttribute("name", "李秋你好");
        System.out.println("index.jsp");
    }
}

4.4 编写 index.jsp 页面

首先在 src/main/webapp/WEB-INF 下建文件夹 views。此处和 dispatcher-servlet.xml 配置文件中的 prefix 属性路径要一样。

在 views 下建 index.jsp 文件

我们使用 jstl 获取 Controlleradd 的数据。

Jsp 页面代码如下:

 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>
        <c:out value="${name}"></c:out>
    </body>
</html>

五、测试

运行代码

Eclipse 里面使用 Maven 搭建 Web 工程

本文相关代码下载:

—————————————— 分割线 ——————————————

FTP 地址:ftp://ftp1.linuxidc.com

用户名:ftp1.linuxidc.com

密码:www.linuxidc.com

在 2015 年 LinuxIDC.com\2 月 \Eclipse 里面使用 Maven 搭建 Web 工程

下载方法见 http://www.linuxidc.com/Linux/2013-10/91140.htm

—————————————— 分割线 ——————————————

一、建立 Maven 项目

使用 Eclipse 的 maven 构建一个 web 项目,以构建 SpringMVC 项目为例:

1.1 选择建立 Maven Project

选择 File -> New -> Other,在 New 窗口中选择 Maven -> Maven Project。点击 newxt

Eclipse 里面使用 Maven 搭建 Web 工程

1.2 选择项目路径

Use default Workspace location 默认工作空间。

Eclipse 里面使用 Maven 搭建 Web 工程

1.3 选择项目类型

在 Artifact Id 中选择 maven-archetype-webapp

Eclipse 里面使用 Maven 搭建 Web 工程

1.4 输入 Group ID 和 Artifact ID,以及 Package

Group ID 一般写大项目名称。Artifact ID 是子项目名称。

例如 Spring 的 web 包,Group ID:org.springframework,artifactId:spring-web。

Package 是默认给你建一个包,不写也可以。

Eclipse 里面使用 Maven 搭建 Web 工程

1.5 配置之后的图

Eclipse 里面使用 Maven 搭建 Web 工程

如果这里显示的内容多,一般是 Filters 设置的问题。或 perspective 为 JavaEE 模式,改成 Java 模式就可以了。

二、配置 maven 项目

2.1 添加文件夹

2.1.1 在 src 的 main 目录下面添加 java 目录

2.1.2 在 src 下面添加 test 目录,并且在 test 目录下面添加 java 和 resources 目录

Eclipse 里面使用 Maven 搭建 Web 工程

2.2 修改 class 路径

右键项目,Java Build Path -> Source

下面应该有 4 个文件夹。src/main/java,src/main/resources,src/test/java,src/test/resources。

双击每个文件夹的 Output folder,选择路径。

src/main/java,src/main/resources,选择 target/classes;

src/test/java,src/test/resources, 选择 target/test-classes;

选上 Allow output folders for source folders.

Eclipse 里面使用 Maven 搭建 Web 工程

在此处还要更改:

更改文件夹显示的顺序:点击 Order and Export。

更改 JDK 版本:在 Libraries 双击 JRE System Library,要 1.6 版本。

2.3 把项目变成 Dynamic Web 项目

2.3.1 右键项目,选择 Project Facets,点击 Convert to faceted from

Eclipse 里面使用 Maven 搭建 Web 工程

2.3.2 配置 Project Facets

更改 Dynamic Web Module 的 Version 为 2.5。(3.0 为 Java7 的)。

如果提示错误,可能需要在 Java Compiler 设置 Compiler compliance level 为 1.6。或者需要在此窗口的 Java 的 Version 改成 1.6。

Eclipse 里面使用 Maven 搭建 Web 工程

2.3.3 配置 Modify Faceted Project
点击 Further configuration available…,弹出 Modify Faceted Project 窗口。此处是设置 web.xml 文件的路径,我们输入 src/main/webapp。Generate web.xml deployment descriptor 自动生成 web.xml 文件,可选可不选。

2.4 设置部署程序集 (Web Deployment Assembly)

上面步骤设置完成后,点击 OK,Properties 窗口会关闭,在右键项目打开此窗口。在左侧列表中会出现一个 Deployment Assembly,点击进去后,如下图:

Eclipse 里面使用 Maven 搭建 Web 工程

此处列表是,部署项目时,文件发布的路径。

我们删除 test 的两项,因为 test 是测试使用,并不需要部署。

Hadoop Eclipse 插件编译安装 1.2.0 http://www.linuxidc.com/Linux/2013-07/87428.htm

Hadoop 在 Eclipse 中的插件编译 http://www.linuxidc.com/Linux/2013-04/83295.htm

Hadoop 1.2.1 编译 Eclipse 插件 http://www.linuxidc.com/Linux/2013-10/91666.htm

Ubuntu 13.10 安装 JDK、Eclipse for C/C++(解决全局菜单问题)http://www.linuxidc.com/Linux/2013-11/92305.htm

如何在 Ubuntu 14.04 中安装最新版 Eclipse http://www.linuxidc.com/Linux/2014-08/105090.htm

 

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2015-02/113103p2.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计11864字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中