• 使用JetBrains Intellij IDEA 开发Java EE应用


    Developing a Java EE Application

    This tutorial illustrates the Java EE application development workflow.

    The application that we will develop will be a minimal one. It'll be a one JSP page Java web application. However, the IntelliJ IDEA features discussed here are applicable to Java EE applications of any complexity.

    Before you start

    Make sure that the following software is installed on your computer:

    • IntelliJ IDEA ULTIMATE Edition.
    • Java SE Development Kit (JDK), version 6 or later. Download Oracle JDK.
    • GlassFish Server, version 3.0.1 or later. Download GlassFish. (You can use any other Java EE-enabled application server. GlassFish is used here just as an example.) (可选:可以使用apache tomcat代替)
    • A web browser.
    • 推荐使用Maven管理项目依赖

    Creating a project

    1. Click Create New Project on the Welcome screen, or select File | New | Project.

      The New Project wizard opens.

    2. In the left-hand pane, select Java Enterprise.
    3. If the JDK that you want to use is already defined in IntelliJ IDEA, select that JDK from the Project SDK list and proceed to the next step.

      Otherwise, click New, select JDK, and select the JDK installation folder in the dialog that opens.

    4. Specify the application server that you are going to use. (We'll use GlassFish Server.)

      If GlassFish is not defined in IntelliJ IDEA yet, click New to the right of theApplication Server field and select Glassfish Server.

      In the Glassfish Server dialog, specify the GlassFish Server installation directory.

    5. Under Additional Libraries and Frameworks, select the Web Application check box.HWJEE009NewProjectFirstPageFinal

      Click Next.

    6. Specify the name for your new project (e.g. JavaEEHelloWorld).HWJEE010NewProjectName

      Click Finish and wait while IntelliJ IDEA is creating the project.

    Exploring the project structure

    When the project is created, you'll see something similar to this in the Project tool window.

    HWJEE011ProjectInit

    • JavaEEHelloWorld is a module folder (which in this case coincides with the project folder). The .idea folder and the file JavaEEHelloWorld.iml contain configuration data for your project and module respectively. The folder srcis for your Java source code. The folder web is for the web part of your application. At the moment this folder contains the deployment descriptorWEB-INFweb.xml and the file index.jsp intended as a starting page of your application.
    • External Libraries include your JDK and the JAR files for working with GlassFish.

    Developing source code

    Our application will be a single JSP page application. Its only function will be to output the text Hello World!  HWJEEIndexJSPHelloWorld

     

    选用tomcat做本地服务器

    点击 Run-> Edit Configurations进入项目设置页面。

    image

    点击绿色的+号,下拉选项菜单到最后添加 tomcat server; tomcat –> local server. 然后选择本地tomcat主目录位置,让它自动识别配置即可。 apply-》 ok .退出

    Running the application

    In the upper-right part of the workspace, clickrun.

    HWJEE019Run

    IntelliJ IDEA compiles your source code and builds an application artifact. (We'll discuss artifacts a bit later. For now, the artifact is your application in deployment-ready format.)

    After that, the Run tool window opens. IntelliJ IDEA starts the server and deploys the artifact onto it.

    HWJEE020RunToolWindow

    Finally, your default web browser starts and you see the application output Hello World!there.

    HWJEE021HelloWorldInBrowser

    Modifying the code and observing the changes

    See also, Updating Applications on Application Servers.

    Exploring a run configuration. Fixing debug settings

    When creating the project, we specified GlassFish as an application server. As a result, IntelliJ IDEA created a run configuration for GlassFish.

    When we performed the Run command (run), we started that run configuration. Now let's take a look at the run configuration and see how its settings map onto the events that we've just observed.

    1. Click the run configuration selector and select Edit Configurations.HWJEE026EditConfigurations

      The Run/Debug Configurations dialog opens and the settings for the GlassFish run configuration are shown.

      HWJEE027RunDebugConfigurations

      The Before launch task list (in the lower part of the dialog) specifies that the application code should be compiled and the corresponding artifact should be built prior to executing the run configuration.

      HWJEE028RunConfigBeforeLaunch

    2. Select the Startup/Connection tab to see how the server is started in the run, debug and code coverage modes.HWJEE030RunConfigStartupConnection
    3. Select the Deployment tab to see which artifacts are deployed after the server is started.HWJEE029RunConfigDeployment
    4. Go back to the Server tab.

      The settings under Open browser specify that after launch (i.e. after the server is started and the artifacts are deployed onto it) the default web browser should start and go to the specified URL (http://localhost:8080/JavaEEHelloWorld_war_exploded).

      The settings to the right of On 'Update' action specify that on clickingupdate_iconin the Run tool window the Update dialog should be shown and the Update resources option should be used by default. (The last used update option becomes the default one).

      HWJEE028RunConfigServer

    5. Note the warning Debug settings are invalid in the lower part of the dialog. Click Fix. (IntelliJ IDEA makes the necessary changes to GlassFish domain.xmlconfiguration file.)HWJEE031RunConfigFix
    6. Click OK.

    Exploring an artifact configuration

    The word artifact in IntelliJ IDEA may mean one of the following: 1) an artifact configuration, i.e. a specification of the output to be generated for a project; 2) an actual output generated according to such a specification (configuration).

    When creating the project, we indicated that we were going to develop a web application. As a result, IntelliJ IDEA, among other things, created a configuration for building a web application. Let's have a look at this configuration.

    1. Open the Project Structure dialog: File | Project Structure or Ctrl+Shift+Alt+S.
    2. Under Project Settings, select Artifacts.

      The available artifact configurations are shown in the pane to the right underaddanddelete. (There's only one configuration at the moment.)

      HWJEE032ProjectStructureArtifacts

      The artifact settings are shown in the right-hand part of the dialog.

      HWJEE033ProjectStructureArtifactSettings

      Type. The artifact type is Web Application: Exploded. This is a decompressed web application archive (WAR), a directory structure that is ready for deployment onto a web server.

      Output directory. The artifact, when built, is placed into<project_folder>outartifactsJavaEEHelloWorld_war_exploded.

      Output Layout. The artifact structure is shown in the left-hand pane of the Output Layout tab.

      The <output root> corresponds to the output directory. Other elements have the following meanings:

      • 'JavaEEHelloWorld' compile output represents compiled Java classes whose sources are located in the src directory. These are placed intoWEB-INFclasses in the output directory.
      • 'Web' facet resources represent the contents of the web directory.

        使用IDE运行程序的时候,默认不会将maven 管理的Jar包复制到WEB-INF/lib目录下,将会出现找不到依赖和类的错误。下面给出一个解决方法:

        

        新建一个lib目录,将右侧maven管理的依赖全都copy到左边。 (点击lib,右键可以add all)。注意,最好删除红色的依赖jar包,如果出现的话。

        可参考: http://stackoverflow.com/a/37084407/4385714

    Packaging the application into a WAR file

    When you get to the stage when you are happy with your application, you may want to place it in a WAR (web application archive). To do that, you should create an appropriate artifact configuration and then build the artifact:

    1. Clickadd, point to Web Application: Archive and select For 'JavaEEHelloWorld: war exploded'.HWJEE034ProjectStructureNewArtifact

      A new artifact configuration is created and its settings are shown in the right-hand part of the dialog.

      HWJEE035ProjectStructureNewArtifactSettings

    2. If necessary, create a manifest file for your archive: click Create Manifest and agree to the location suggested by IntelliJ IDEA (webMETA-INFMANIFEST.MF).
    3. Click OK in the Project Structure dialog.
    4. Select Build | Build Artifacts.HWJEE036BuildArtifacts
    5. In the Build Artifact popup, point to JavaEEHelloWorld:war and select Build.HWJEE037BuildArtifactPopup

      Now if you look at the outartifactsJavaEEHelloWorld_war folder in the Projecttool window, you'll see the archive there.

      HWJEE038ArtifactInProjectToolWindow

    Last modified: 14 July 2015

    摘录于jetbrains官方文档

  • 相关阅读:
    HttpClient4.5 post请求xml到服务器
    关于spring mybateis 定义resultType="java.util.HashMap"
    Git安装和使用(谨记)
    使用 Lombok 简化项目中无谓的Java代码
    为什么JSP会比Beetl慢
    Apache Commons Codec 与消息摘要算法(hash算法)
    加密解密基础问题:字节数组和(16进制)字符串的相互转换
    Spring AOP 不同配置方式产生的冲突问题
    Java 权限框架 Shiro 实战二:与spring集成、filter机制
    Java 权限框架 Shiro 实战一:理论基础
  • 原文地址:https://www.cnblogs.com/7explore-share/p/4778028.html
Copyright © 2020-2023  润新知