1. 程式人生 > >linux下的Java開發 intellij idea+tomcat+maven

linux下的Java開發 intellij idea+tomcat+maven

rac project article attribute tail 3.1 構圖 eat quest

前期準備:安裝intellij idea、下載tomcat、下載maven(註意我用的是tomcat6、maven 3.2.1、jdk1.6.0_45,之前maven用的3.5結果報錯,搞了好久,建議小夥伴們用穩定版本)

intellij idea官方網站:https://www.jetbrains.com/idea/download/#section=linux(註意要下載Ultimate版本,不然裏面的東西不全,比如tomcat插件...)

intellij idea破解:http://blog.csdn.net/zhangwenwu2/article/details/54948959

tomcat官方下載:http://tomcat.apache.org/download-60.cgi

maven官方下載:https://archive.apache.org/dist/maven/maven-3/

註意下載tar.gz後綴的文件

運行idea,我的idea安裝在了/usr/idea/idea-IU-172.3317.76目錄下,&指的是後臺運行

技術分享 create new project 創建一個新的項目 技術分享

選擇Maven ,勾選Create from archetype,之後next

技術分享

填寫相應信息

技術分享

技術分享

finish,下面這個選擇enable Auto-import

技術分享

按快捷鍵ctrl+alt+shift+s ,選擇Modules,點擊+號為項目添加web模板

技術分享技術分享

修改Web Resource Directory、Deployment Descriptors 為我圈寫的內容,如果不存在這些目錄和文件,就手動打進去就(比如說只能定位到/src/main,沒有webapp 你就自己手動在後面敲進去)

技術分享

Facts: 表示當前項目的適配服務組件。可看到此項目已是一個Web項目了。

技術分享

Aftifacts: 這個Aftifacts描述了當前項目發布的信息。現在進行添加,從Modeles中選擇。

技術分享

技術分享

點擊ok

說明:A: 現在Artifacts已有了發布的項目了(idea中準確的說應是Modele) B:output root目錄描述了當前項目的編譯目錄及適配服務。

技術分享

確定之後當前項目的結構:

技術分享

項目的部署

配置tomcat技術分享

在deploment面板中點擊+號,添加artifact

技術分享

配置server

技術分享

編寫代碼測試

寫一個servlet,如果報錯,是因為沒有引入庫文件

import
java.io.IOException; public class HelloController extends javax.servlet.http.HttpServlet { protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { doPost(request,response); } protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { request.setCharacterEncoding("UTF-8"); String name = (String) request.getParameter("name"); request.setAttribute("name",name); System.out.println(name); request.getRequestDispatcher("index.jsp").forward(request,response); } }

shift+ctrl+alt+s,modules中點擊自己的項目testmaven,在dependencies中點擊+號選擇第二個添加tomcat的庫文件按

技術分享

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>HelloController</servlet-name>
        <servlet-class>HelloController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloController</servlet-name>
        <url-pattern>/Hello</url-pattern>
    </servlet-mapping>
</web-app>

index.jsp

  Created by IntelliJ IDEA.
  User: zyh
  Date: 17-7-19
  Time: 下午8:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>testmave</title>
</head>
<body>
   your name is : ${name}
</body>
</html>

項目結構圖

技術分享技術分享

運行tomcat

技術分享

技術分享

項目我也傳到github上了,郵箱去的朋友可以瞅瞅

https://github.com/Demo233/testmaven/

linux下的Java開發 intellij idea+tomcat+maven