Java Web Project

Source: Internet
Author: User
Tags apache tomcat

Objective

goal of this article: use Eclipse to build a maven-based Web project for the IDE environment , explain the build process, project structure, program run, Debug and test process, and use maven as a continuous integration tool.

??

Object-oriented: a . NET developer who transforms Java

??

knowledge Point:eclipse,maven,jfinal,jetty,tomcat,junit

??

Environment preparation

Maven

    1. Install maven-3.3.9, recommended 3.0+ version
    2. Check the installation version, command line input : mvn-version

??

??

??

Tomcat

    1. Download TOMCAT-V7, unzip to local.

??

Eclipse

    1. Install the Eclipse-neon version. Make sure that the plugins in the diagram are installed.

      ??

    2. Configure Maven

      ??

    3. Configure the server

??

??

??

??

Create a project

Use Eclipse to create a MAVEN-based Web project

Slightly

To create a Web project on the command line using MAVEN

??

  1. To Create a Web project:
    1. Execute command : mvn archetype:generate, the console will not respond after displaying the following message (cause :mvn try to list all the project templates for the server, Too many types and too slow access to Maven repositories, why too slow? You know, the Great Firewall .

      ??

      ??

    2. Adding parameters makes MVN to list only the native templates: -darchetypecatalog=internal
    3. template is still a lot, it is recommended to add parameters, such as to create Web items, available parameters -dfilter=web
    4. Once you have determined the template type, you can specify the template directly in the parameter: -darchetypeartifactid=maven-archetype-webapp
    5. MVN The interactive mode is used by default, and parameters are specified as non-interactive mode: -dinteractivemode=false
    6. Create a Web site project ( package name Com.demo, project name Websample) The complete command: MVN Archetype:generate-darchetypeartifactid=maven-archetype-webapp-dgroupid=com.demo-dartifactid=websample- Dinteractivemode=false-darchetypecatalog=internal

      ??

      ??

    7. viewing compilation, testing, packaging effects , Command : mvn compile test Package
    8. Generate the relevant files (. project,.classpath) for the Eclipse Project,
      1. to enter the project directory, command: CD Websample
      2. Execute command: mvn eclipse:eclipse
    9. Other
      1. Most of the online cases did not set the parameters -darcheypecatalog, due to national conditions, the basic card in a).
      2. Set the-darchetypecatalog parameter even if the template is specified , otherwise it will be stuck in a).

Open Project in Eclipse

Importing Maven projects in eclipse

Maven builds project files after importing

  1. Generate the relevant files (. project,.classpath) for the Eclipse Project,
    1. to enter the project directory, command: CD Websample
    2. Execute command: mvn eclipse:eclipse
  2. Eclipse Import in

    ??

    ??

  3. compiler error handling , Makers window prompt error
    1. the superclass " Javax.servlet.http.HttpServlet "wasn't found on the Java Build Path

      workaround :

      1. right click on Project
      2. select Properties
      3. select Project Facets
      4. select Apache Tomcat as Runtime server
      5. click OK

Precautions

    1. Import Project (Java World called Widget ) The name cannot be the same as an existing artifact in the workspace. (artifactid node value in pom.xml)

??

Source Code Development

Refer to the Jfinal manual to add the MVC structure source code.

    1. Modify the project compilation output directory to src/main/webapp/web-inf/classes(maven default is target/classes)
    2. Modify the Pom. xml file to add dependencies.
    3. Modify the Web. xml file to specify a filter.
    4. adding Java files
      1. Create a Java folder (the source location in the MAVEN structure is Src/main/java).
      2. Add configuration file ,Democonfig.java file, inherit jfinalconfig.
      3. Add controller files, Hellocontroller.java files.

??

Run debugging

There are 2 modes of operation in the jfinal manual

    1. App runs "Java application"
      1. This approach relies on jetty, a lightweight, embedded Servle container.
    2. Deploy to server to run the "Run on server"
      1. It is recommended that you use the Tomcat directory configuration directly, and note that modifying the port does not cause it to start

??

??

jfinal Test Extension Jfinal-ext

    1. the extension does not support jfinal-2.1+, which modifies the logger class in the version refactoring, and other incompatibilities are not known.
    2. Open source software upgrade compatibility is poor

??

Writing unit tests using JUnit

Junit3 vs JUNIT4

    • Maven template creation project in default dependent JUNIT3
    • The default unit test plug-in for Eclipse uses JUNIT4
    • Junit3.8 Requirements for test code
      • public.
      • void.
      • No method parameters.
      • Method name must begin with test
    • The difference between the two is very large, V4 uses a more concise annotation feature (JAVA5) to simplify the test code

Add test Code

JUNIT3 Test Code

Import Junit.framework.TestCase;

Import static org.junit.assert.*;

public class Addoperationtest extends testcase{

public void SetUp () throws Exception {

}

public void TearDown () throws Exception {

}

public void Testadd () {assertequals (Expresult, result);}

}

JUNIT4 Test Code

Import Junit.framework.TestCase;

Import Org.junit.After;

Import Org.junit.Before;

Import Org.junit.Test;

Import static org.junit.assert.*;

public class Addoperationtest extends testcase{

@before

public void SetUp () throws Exception {}

@after

public void TearDown () throws Exception {}

@test

public void Testadd () {assertequals (Expresult, result);}

}

??

unit tests in maven,

    1. Command : mvn test

??

unit tests in Eclipse

  1. The default output folder for Java Build Path modified in the above steps is src/main/webapp/web-inf/classes, where you need to modify the test source directory to output it to the target/ Test-classes, maintaining consistency with Maven

    ??

    ??

  2. New configuration file
  3. Modify Classpath, or you will be prompted when you run the unit test:eclipse-java.lang.classnotfoundexception
    1. Open Your Run configurations
    2. Click on the junit-test want to start
    3. Go to the Classpath tab
    4. Try to add a folder (click on user entries, click on Advanced, click on Add Folders, click on OK and search the Outputfold Er for your test classes (those you find under Projektproperties Java build path, source))

    ??

    ??

Web Program Packaging

    1. command: MVN Package
    2. The above command will output the Websample.war and Websample folders in the target/directory.
    3. Websample.war contains all the files required for the project

??

Continuous integration

Continuous integration practices

    • Maintain only one source repository
    • Automated Build
    • Let build self-test
    • Each person submits code to the backbone every day
    • Each commit should build the backbone on the continuous integration machine
    • Maintain a fast build
    • Testing in a simulated production environment
    • Make it easy for everyone to get the latest executables
    • Everyone can see the progress.
    • Automating deployment

??

Extended Reading

The relationship between Maven and eclipse

??

??

??

Resources

    1. Creating Simple Web application Using Apache Maven
    2. How to use Maven Creating a Web project (concrete steps)
    3. Convert a maven-generated Java project into a project that supports the Eclipse IDE
    4. Using Maven in Eclipse
    5. MAVEN-based Continuous integration practices
    6. JUNIT4 detailed

Java Web Project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.