摘要:本文将带你了解WEBAPP开发教程11、 maven发布web项目,希望本文对大家学WEBAPP有所帮助。
本文将带你了解WEBAPP开发教程11、 maven发布web项目,希望本文对大家学WEBAPP有所帮助。
1、新建maven project,然后选择webapp的框架
2、写一个Servlet
package com.onyas.servlet; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import com.onyas.user.model.User; public class HelloServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setAttribute("user", new User("admin","123","管理员")); req.getRequestDispatcher("/hello.jsp").forward(req, resp); } }
3、在src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="//java.sun.com/xml/ns/javaee" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//java.sun.com/xml/ns/javaee //java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.onyas.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello.do</url-pattern> </servlet-mapping></web-app>
4、在src/main/webap下新建hello.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "//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> Hello---->${user.nickname }</body></html>
5、修改因为有引入User类,所以要添加对其它模块的依赖,如果这时候报找不到自己写的jar包时,是因为没有把自己写的模块安装到本地仓库中,需要在自己的模块pom.xml中执行clean install.
<project xmlns="//maven.apache.org/POM/4.0.0" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.onyas.user</groupId> <artifactId>user-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../user-parent/pom.xml</relativePath> </parent> <artifactId>user-web</artifactId> <packaging>war</packaging> <name>user-web Maven Webapp</name> <url>//maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</sc//localhost:8080/user-web/hello.doope> </dependency> <!-- 对servlet的依赖 --> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>servlet-api</artifactId> <version>2.5.20110712</version> </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>user-services</artifactId> <version>${project.version}</version> </dependency> </dependencies> <build> <finalName>user-web</finalName> </build></project>
6、在pom.xml上右键run as -->maven build..-->输入clean package..会在target包下生成war包,把这个war包拷贝到tomcat下webapp目录下,启动tomcat就可以了
在 浏览器中输入//localhost:8080/user-web/hello.do,即可看到结果。
7、这样做每次修改后都要执行clean package,然后拷贝war包,然后运行tomcat,很麻烦,所以现在有一个插件,搜索copy maven plugin就可以。
更好的一种做好是用jetty发布,首先在父pom.xml中配置对jetty插件:
<!-- 配置jetty的插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/hello</contextPath> </webApp> <!-- 配置监听端口 --> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8082</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> </configuration> </plugin>
然后在web项目中加入jetty插件:
<build> <finalName>user-web</finalName> <plugins> <!-- 添加jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> </plugin> </plugins> </build>
在web项目的pom.xml中右键run as -->maven builder..->clean compile jetty:run,就会看到控制台启动jetty,启动完成后,即可在浏览器中试验
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之WebApp频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号