用Maven创建第一个web项目

Maven3路程(三)用Maven创建第一个web项目(1)

一.创建项目

1.Eclipse中用Maven创建项目

上图中Next

 

2.继续Next

 

3.选maven-archetype-webapp后,next

 

4.填写相应的信息,Packaged是默认创建一个包,不写也可以

 

5.创建好项目后,目录如下:

至此,项目已经创建完毕,下边可是配置。

二.项目配置

1.添加Source Folder

Maven规定,必须创建以下几个Source Folder

src/main/resources

src/main/java

src/test/resources

src/test/java

添加以上的Source Folder

创建好后的目录如下:

2.配置Build Path

 

3.设定4个文件夹的输出Output folder,双击修改

分别修改输出路径为

src/main/resources  对应  target/classes

src/main/java  对应  target/classes

src/test/resources  对应  target/test-classes

src/test/java  对应  target/test-classes

4.修改后如下图

 

5.设定Libraries

 

6.配置完Build Path后目录如下:

7.将项目转换成Dynamic Web Project

在项目上右键Properties

在左侧选择 Project Facets,单击右侧的 ”Convert faceted from “

 

8.修改Java为你当前项目的JDK,并添加Dynamic Web Module ,最后单击”Further Configuration available“ 链接:

 

9.修改Content directory 为 src/main/webapp ,单击OK:

 

10.设置完Content directory,ok后再次点击前一界面ok,完成转换成Dynamic Web Project项目

 

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

在项目上右键单击,选择Properties,在左侧选择Deployment Assembly

 

12.设置部署时的文件发布路径

  1,我们删除test的两项,因为test是测试使用,并不需要部署。
  2,设置将Maven的jar包发布到lib下。 
    Add -> Java Build Path Entries -> Maven Dependencies -> Finish

设置完成后如图

 

ok后,web项目就创建完毕了,目录机构如图

运行后访问工程成功!

下一章将测试一个servlet




Maven3路程(三)用Maven创建第一个web项目(2)servlet演示

上一章用Maven新建了web项目成功后,本文演示在此基础上应用servlet。

1.首先修改pom.xml文件,添加servlet依赖

复制代码
<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>com.lei.demo</groupId>
  <artifactId>maven-web-demo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>maven-web-demo Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
      <!-- JUnit配置 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- 添加Servlet -->  
    <dependency>    
        <groupId>javax.servlet</groupId>    
        <artifactId>servlet-api</artifactId>    
        <version>2.5</version>    
        <scope>provided</scope>    
    </dependency>
  </dependencies>
  <build>
    <finalName>maven-web-demo</finalName>
  </build>
</project>
复制代码

修改完保存后,项目会从Maven仓库中自动添加servlet-api-2.5.jar包的引用,如果仓库中没有,会自动下载。

引用后见图。

 

2.新建一个Servlet

在src/main/java下创建一个Servlet,ServletDemo

复制代码
package com.sulei.demo;

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 ServletDemo
 */
public class ServletDemo extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public ServletDemo() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  
        response.setContentType("text/html;charset=utf-8");
        
        String action = request.getParameter("action");  
        if("login_input".equals(action)) {  
            request.getRequestDispatcher("login.jsp").forward(request , response);  
        } else if("login".equals(action)) {  
            String name = request.getParameter("name");  
            String password = request.getParameter("password");  
              
            System.out.println("name->" + name + ",password->" + password);
        }
    }

}
复制代码

 

3.修改Web.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <javaee:display-name>Archetype Created Web Application</javaee:display-name>
  <servlet>
    <javaee:description></javaee:description>
    <javaee:display-name>ServletDemo</javaee:display-name>
    <servlet-name>ServletDemo</servlet-name>
    <servlet-class>com.sulei.demo.ServletDemo</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletDemo</servlet-name>
    <url-pattern>/demo</url-pattern>
  </servlet-mapping>
</web-app>
复制代码

 

4.创建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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <p>用Maven创建web项目,测试Servlet</p>
    <a href="demo?action=login_input">登录(demo?action=login_input)</a>
</body>
</html>
复制代码

 

5.创建login.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>
    <form action="demo?action=login" method="post">  
        Name:<input type="text" name="name" />  
        Password:<input type="password" name="password" />  
              
        <input type="submit" value="登录" />  
    </form>  
</body>
</html>
复制代码

 

OK,可以测试一下

 






Maven3路程(四)用Maven创建Struts2项目

采用struts版本:struts-2.3.8

一.创建一个web项目

参考前面文章,项目名:maven-struts-demo

二.配置pom.xml文件添加struts2依赖

复制代码
<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>com.lei.demo</groupId>
  <artifactId>maven-struts-demo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>maven-struts-demo 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>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.3.8</version>
</dependency>
  </dependencies>
  <build>
    <finalName>maven-struts-demo</finalName>
  </build>
</project>
复制代码

保存后maven会自动下载相应的jar包。

下载完成后查看jar包,如图

 

三.新建JSP页面

1.index.jsp页面,点“去登录界面”后,去struts.xml中找对应的anction中name=user_login_go的路径

复制代码
<%@ page 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>
<body>
    <h2>Struts2-Demo</h2>
    <a href="user_login_go">去登录界面</a>
</body>
</html>
复制代码

 

2.login.jsp页面,输入name和password后,去struts.xml中找对应的anction中name=login_go的路径

复制代码
<%@ page 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>struts2-Demo-登录界面</title>
</head>
<body>
    <p>struts2-Demo-登录界面</p>
    <form action="login_go" method="post">
        name:<input type="text" name="name" />
        password<input type="password" name="password" />
        <input type="submit" value="登录" />
    </form>
</body>
</html>
复制代码

 

3.welcome.jsp页面,输入name和password后,去struts.xml中找对应的anction中name=login_go的路径

复制代码
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Struts2-Demo-欢迎页面</title>
</head>
<body>
    Welcome:
    <br>
    <h1>name=<s:property value="name" /></h1>
    <h1>password=<s:property value="password" /></h1>    
    <h1>重新登录</h1>
    <s:form action="login_go" namespace="/" method="post">
        <s:textfield name="name" label="name"></s:textfield>  
        <s:password name="password" label="password"></s:password>  
              
        <s:submit value="Login"></s:submit>
    </s:form>
</body>
</html>
复制代码

 

4.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>Struts2 Web Application</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
复制代码

 

5.struts.xml,放在src/main/resources目录下

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "http://struts.apache.org/dtds/struts-2.0.dtd">  
<struts>
    <package name="user" namespace="/"  extends="struts-default">
        <action name="user_login_go" class="com.sulei.action.UserLoginAction" method="user_login">
            <result name="success">/login.jsp</result>
        </action>
        <action name="login_go" class="com.sulei.action.UserLoginAction" method="login">
            <result name="success">/welcome.jsp</result>
        </action>
    </package>
</struts>
复制代码

 

目录结构:

运行效果如下

 

 





引用地址:


http://www.cnblogs.com/leiOOlei/p/3374463.html

 

 


  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值