如何在eclipse里面创建一个maven web项目

最近在学习javaweb的时候,使用ecplise加maven的方式创建web项目,途中遇到了很多的坑,现在来总结一下,这里的要求是安装好了maven,配置好了tomcat,相关的文章很多,这里就不再复述了,总之可以在命令行使用javac和mvn -version两个命令就可以算是配置好了。同时,我们要记得关联我们的tomcat和maven,我们去修改我们的tomcat的conf文件夹里面的tomcat-users.xml文件,在里面添加一个用户,再把这个用户绑定到maven里面去。

在tomcat的tomcat-users文件的tomcat-users属性下添加

<role rolename="tomcat"/>
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="tomcat" password="123456" roles="manager-gui,manager-script"/>
 

这里的username就是我们的用户名,我们启动tomcat,打开localhost:8080,然后在那里点maner这个按钮输入我们的username和password如果进去了,就说明好了,

然后我们去修改我们maven的setting.xml文件。这个文件在我们根目录的.m2文件夹那里,如果没有,我们就复制一个到那里去,然后再那里修改,这样会比较的好。

在server那里添加一个server

	<server>
      <id>tomcat</id>
      <username>tomcat</username>
      <password>123456</password>
    </server>

这个id我们可以任意配置。

随后我们要给我们的eclipse安装tomcat插件,具体百度,反正eclipse上有个猫就好了


正题~

我们首先打开eclipse去创建一个maven的web项目

我们在file->new->maven project里面创建一个maven项目




然后我们next,选择web,这里我们可以在filter那里输入web得到


后面的maven就自己设置了


然后我们发现,诶,怎么有错误啊!


这个是因为我们没有在maven的pom文件里面添加httpServlet的依赖得到的,我们在pom文件里面添加依赖就好了

<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
随后我们就发现,红叉消失了。

but,but我们查看一下,却发现


嗯,maven默认的编译jdk是1.5。。。而我们用的是1.8~~,这个该怎么办呢。。。同时我们发现。。。好像少了点什么,对,为什么我们创建的maven项目没有java啊。。。这里我们当然可以去自己创建,这个时候我们要选择,resource folder去创建java文件夹,但是这个时候可能会告诉你已经存在了!what?明明没有啊。。。这个时候我们就要去修改一个东西。我们选择项目


进入Build Path

然后我们修改一下jre


选择

选择我们自己的jre就好了

再回来看,我们两个问题就都解决了


随后我们就要再去配置我们的pom文件,使得可以去把我们的项目发布在我们的tomcat容器里面去。

配置完了的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>ABoyL</groupId>
  <artifactId>mavenweb</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mavenweb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
  </dependencies>
  <build>
    <finalName>mavenweb</finalName>
    <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
					<showWarnings>false</showWarnings>
					<showDeprecation>false</showDeprecation>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<url>http://localhost:8080/manager/text</url>
					<path>/mavenweb</path>
					<uriEncoding>UTF-8</uriEncoding>
					<finalName>mavenweb</finalName>
					<server>tomcat</server>
				</configuration>
			</plugin>
			</plugins>
  </build>
  	<reporting>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<configuration>
					<charset>UTF-8</charset>
					<encoding>UTF-8</encoding>
					<docencoding>UTF-8</docencoding>
				</configuration>
			</plugin>
		</plugins>
	</reporting>
</project>
我们自行修改finlname和path,这个两个一样就好,然后我们发现。。程序又出错了qwq。。。因为我们配置了1.8的jdk,但是我们又不知道那里错了。。。这个还是因为我们的jdk版本有问题。。。因为maven。。。很坑。。。我们继续选择我们项目的属性。。进入Project Facets,我们发现。。怎么jdk又是1.5!!!

好好吧。。。我们快点改了。。。

apply,ok

然后我们神奇的发现。。还是没有用。。。怎么回事。。。不要慌。。。我们更新一个maven 工程


选择我们的project,就ok了


这个时候我们就可以进行发布了。。可以了吗?不。。。我们还有一个东西没有做,,,我们还没有安装启动tomcat,还没有给我们的项目加上一个tomcat。。。或者说给eclipse绑定一个tomcat,

我们进入windows->preference

选择server,进入runtime


add,然后添加我们的tomcat



选择我们的安装目录已经,我们安装的jdk

依旧是在这里我们选择tomcat


然后选择我们的版本和安装目录


这个时候我们就可以在我们的eclipse里面启动我们的tomcat了。点击我们的猫,就启动了


随后我们设置我们的运行的时候的tomcat


依旧是在我们刚刚修改jdk的那个地方,找到Project Facets,去修改Runtimes,打个勾就好了


随后我们新建一个servlet


我们使用eclipse自己生成,会自己在我们的web.xml那里配置好

package L;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class hello
 */
public class hello extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public hello() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("service");
		super.service(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		System.out.println("do Get");
	}

}
web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
  	<servlet-name>hello</servlet-name>
  	<display-name>hello</display-name>
  	<description></description>
  	<servlet-class>L.hello</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>hello</servlet-name>
  	<url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

然后我们进行maven部署

在项目那里右键,run as 


最后一个


这里我们要注意的是修改我们的Maven Runtimes为自己的maven,run,ok




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值