在eclipse上使用Maven创建动态web项目

    在eclipse上使用Maven创建web工程和使用Maven创建普通Java工程一样,不过在Packaging一栏选择“war”,这个过程具体就不多说了,详情见我上一篇文章。

    我们使用eclipse创建出来的web项目其实并不完整,你会发现刚创建出来就报错,对,没错,不是因为没有使用Maven Update它,而是真的发生错误了,发生错误的原因是:没有 web.xml 文件!而解决这个问题的方法也很简单,手动在 src/main/webapp 下创建一个 WEB-INF 目录,然后拷贝一份 web.xml 文件到 src/main/webapp/WEB-INF 下就可以了,如果你找不到 web.xml 文件去拷贝,那么手动创建一个web.xml文件,然后把下面的内容复制进去就可以了

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

    首先,创建web项目肯定需要在pom.xml中引入第三方jar包依赖的,我这里使用tomcat7.0,下面是我的整个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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>me.maven.test</groupId>
    <artifactId>MyWebTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <defaultGoal>compile</defaultGoal>
	<finalName>MyWebTest</finalName>
	<plugins>
	    <plugin>
	        <groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<configuration>
		    <source>1.8</source>
		    <target>1.8</target>
		    <encoding>${project.build.sourceEncoding}</encoding>
		</configuration>
	    </plugin>	
	    <plugin>  
               <groupId>org.apache.tomcat.maven</groupId>  
               <artifactId>tomcat7-maven-plugin</artifactId>   
               <version>2.2</version>
               <configuration>  
                   <path>/${project.build.finalName}</path>  
                   <server>mytomcat7</server>  
                </configuration>  
           </plugin>
	</plugins>
    </build>
	
    <dependencies>
	<dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit</artifactId>
	    <version>4.10</version>
	    <scope>test</scope>
	</dependency>	
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>javax.servlet-api</artifactId>
	    <version>4.0.1</version>
	    <scope>provided</scope>
	</dependency>
	<dependency>
	    <groupId>javax.servlet.jsp</groupId>
	    <artifactId>jsp-api</artifactId>
	    <version>2.2</version>
	    <scope>provided</scope>
	</dependency>	
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>jstl</artifactId>
	    <version>1.2</version>
	    <scope>runtime</scope>
	</dependency>
    </dependencies>
</project>

其次,在 src/main/webapp 里创建两个测试的页面,以及在 src/mian/java 下创建一个servlet来测试

<!-- index.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=ISO-8859-1">
        <title>测试页面1</title>
    </head>
    <body>
	<a href="${pageContext.request.contextPath}/Test1">click me to test</a>
    </body>
</html>
<!-- test.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	${test}
</body>
</html>
package me.maven.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Test1")
public class Test1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("test", "this is Test forward page");
		request.getRequestDispatcher("test.jsp").forward(request, response);
	}
}

最后运行:

    1) 在项目上鼠标右键 -> Run As -> Maven built,

    2) 在Goals里输入 tomcat7:run

    3) 打开浏览器输入路径测试


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值