In the Java World, Web applications occupy a large place, and the standard way of packaging is war. The next step is to build a war application through MAVEN, but because of the space, this article first introduces the structure of the Web project and the building of the service module in Maven, and the war application is presented in the next article.
- The directory structure of the Web project
A web app is packaged like a jar, but it contains more content, such as JSP files, Servlets, Web. XML configuration files, static website resources (such as HTML,CSS,JS), and so on. A war package contains at least two subdirectories: Meta-inf and Web-inf. The former contains some packaging metadata information, which is the core of the war package, Web-inf must contain a Web resource representation file, XML, whose subdirectory classes contains all of the Web project's classes, while the other subdirectory lib contains the dependent jar packages for all the Web projects, and the classes and LIB directories are added to Classpath at runtime. In addition to Meta-inf and Web-inf, the General war package contains a lot of web resources, such as HTML and JSP files, and sometimes some folders such as Img,css and JS, which contain the corresponding files for the page to use. As with other MAVEN projects, MAVEN has a common convention for the layout structure of Web projects. First, the user must explicitly specify the package method as the war for the Web project. If you do not explicitly specify the packaging type, MAVEN uses the default packaged-by jar to not properly package the Web project. The Web project's class and resource files are the same as the generic Jar project, with the same default location. The Web project is a special way: It also has a Web resource directory, the default location is src/main/webapp/. Under this directory, you must include a subdirectory, Web-inf, which must contain the Web. xml file. The other files in the src/main/webapp/directory are exactly the same as the resources in the war package. Before you build a Web project using MAVEN, you must understand the construction of this MAVEN project structure and the war package structure. One thing to note is that there must be a Lib directory in the war package that contains all the dependent jar packages, but there is no such directory in the MAVEN project structure, because all dependencies are configured in the POM, and MAVEN will copy the corresponding package from the local repository according to the POM configuration when it is packaged in the war mode.
The following is a Account-service module for encapsulating the Account-email,account-persist and Account-captcha three modules.
Account-service's Pom.xml
<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/xsd/maven-4.0.0.xsd" >< Modelversion>4.0.0</modelversion><parent><groupid>com.juvenxu.mvnbook.account</groupid ><artifactid>account-parent</artifactid><version>1.0.0-snapshot</version></ Parent><artifactid>account-service</artifactid><name>account Service</name>< Properties><greenmail.version>1.4.1</greenmail.version></properties><dependencies> <dependency><groupid>${project.groupid}</groupid><artifactid>account-email</ artifactid><version>${project.version}</version></dependency><dependency>< Groupid>${project.groupid}</groupid><artifactid>account-persist</artifactid><version >${project.version}</version></dependency><dependency><groupid>${project.groupid}</groupid><artifactid> account-captcha</artifactid><version>${project.version}</version></dependency>< Dependency><groupid>junit</groupid><artifactid>junit</artifactid></dependency ><dependency><groupid>com.icegreen</groupid><artifactid>greenmail</artifactid ><version>${greenmail.version}</version><scope>test</scope></dependency></ dependencies><build><testresources><testresource><directory>src/test/resources</ Directory><filtering>true</filtering></testresource></testresources></build> </project>
Like other modules, the Account-service module inherits from Account-parent, which relies on the account-email,account-persist and Account-captcha three modules. Because the GroupId and version are identical in other modules of the same project, you can replace them with the MAVEN properties ${project.groupid} and ${project.version} to reduce the changes when upgrading the project version. Other configurations in the project, such as JUnit and Greenmail dependencies, are intended to facilitate unit testing.
The purpose of the Account-service is to encapsulate the lower details and expose the interface as simple as possible externally. First, write a simple service interface://accountservice.java
package Com.juvenxu.mvnbook.account.service;public interface Accountservice {/** * Generates a unique identifier for a verification code and returns the identifier of the * * @return Captcha * @throws accountserviceexception */string Generatecaptchakey () throws Accountservic eexception;/** * Generate a CAPTCHA image based on the identifier, return * * @param captchakey * CAPTCHA identifier * @return picture byte stream in byte stream * @throws Accountservice Exception */byte[] Generatecaptchaimage (String captchakey) throws accountserviceexception;/** * Register according to registration * * @param SIGNU Prequest * Package Registration information * @throws accountserviceexception */void signUp (signuprequest signuprequest) throws AccountS erviceexception;/** * Activate account based on activation code * * @param activationnumber * Activation code * @throws accountserviceexception */void Act Ivate (String activationnumber) throws accountserviceexception;/** * Login based on user ID and password * * @param ID * User Registration ID * @pa Ram Password * User password * @throws accountserviceexception */void Login (string ID, string password) throws Accountse rviceexception;}
The exception class is defined as follows://accountserviceexception.java
Package Com.juvenxu.mvnbook.account.service, @SuppressWarnings ("Serial") public class Accountserviceexception Extends Exception {public accountserviceexception (String message) {super (message);} Public accountserviceexception (String message, Throwable throwable) {super (message, throwable);}}
The Signuprequest is a pojo, a simple Java object that contains only attributes and Getter,setter://signuprequest.java
Package Com.juvenxu.mvnbook.account.service;public class Signuprequest {private string id;//user idprivate String email; /user mailbox private String name; User name private String password; Account password Private String ConfirmPassword; Confirm Password Private String Captchakey; Authenticode identifier private String captchavalue; Verification code value private String Activateserviceurl; Activate link public String getId () {return ID;} public void SetId (String id) {this.id = ID;} Public String Getemail () {return email;} public void Setemail (String email) {this.email = email;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public String Getconfirmpassword () {return confirmpassword;} public void Setconfirmpassword (String confirmpassword) {This.confirmpassword = ConfirmPassword;} Public String Getcaptchakey () {return captchakey;} public void Setcaptchakey (String captchakey) {this.captchakey = Captchakey;} PUblic String Getcaptchavalue () {return captchavalue;} public void Setcaptchavalue (String captchavalue) {this.captchavalue = Captchavalue;} Public String Getactivateserviceurl () {return activateserviceurl;} public void Setactivateserviceurl (String activateserviceurl) {this.activateserviceurl = Activateserviceurl;}}
In the Accountservice class, the user is required to register using the signup () method, and the registration information is encapsulated by the signuprequest. After successful registration, the user will get an activation link that contains an activation code that will be used by the user to activate the account using the Activate () method. Finally, the login () method is used to log on. Next is the implementation class for the service interface://accountserviceimpl.java
Package Com.juvenxu.mvnbook.account.service;import Java.util.hashmap;import Java.util.map;import Com.juvenxu.mvnbook.account.captcha.accountcaptchaexception;import Com.juvenxu.mvnbook.account.captcha.accountcaptchaservice;import Com.juvenxu.mvnbook.account.captcha.randomgenerator;import Com.juvenxu.mvnbook.account.email.accountemailexception;import Com.juvenxu.mvnbook.account.email.accountemailservice;import Com.juvenxu.mvnbook.account.persist.account;import Com.juvenxu.mvnbook.account.persist.accountpersistexception;import Com.juvenxu.mvnbook.account.persist.accountpersistservice;public class Accountserviceimpl Implements Accountservice {private map<string, string> activationmap = new hashmap<string, string> ();p rivate Accountpersistservice Accountpersistservice; Account Persistence Layer Service private accountemailservice accountemailservice; Account Email service private Accountcaptchaservice accountcaptchaservice; Account Verification Code Service public Accountpersistservice Getaccountpersistservice () {return AccountpersistserviCE;} public void Setaccountpersistservice (Accountpersistservice accountpersistservice) {This.accountpersistservice = Accountpersistservice;} Public Accountemailservice Getaccountemailservice () {return accountemailservice;} public void Setaccountemailservice (Accountemailservice accountemailservice) {This.accountemailservice = Accountemailservice;} Public Accountcaptchaservice Getaccountcaptchaservice () {return accountcaptchaservice;} public void Setaccountcaptchaservice (Accountcaptchaservice accountcaptchaservice) {This.accountcaptchaservice = Accountcaptchaservice;} Public byte[] Generatecaptchaimage (String captchakey) throws Accountserviceexception {try {return Accountcaptchaservice.generatecaptchaimage (Captchakey);} catch (Accountcaptchaexception e) {throw new Accountserviceexception ("Unable to generate Captcha Image.", E);}} Public String Generatecaptchakey () throws Accountserviceexception {try {return Accountcaptchaservice.generatecaptchakey ();} catch (Accountcaptchaexception e) {throw new Accountserviceexception ("Unable to generate Captcha key.", E);}} public void SignUp (Signuprequest signuprequest) throws Accountserviceexception {try {//Check password consistency if (! Signuprequest.getpassword (). Equals (Signuprequest.getconfirmpassword ())) {throw new Accountserviceexception ("2 Passwords does not match. ");} Use Accountcaptchaservice to check the captcha if (!accountcaptchaservice.validatecaptcha (Signuprequest.getcaptchakey (), Signuprequest.getcaptchavalue ())) {throw new Accountserviceexception ("Incorrect Captcha.");} Instantiate an account object with the user information in the request account = new (); Account.setid (Signuprequest.getid ()); Account.setemail ( Signuprequest.getemail ()); Account.setname (Signuprequest.getname ()); Account.setpassword ( Signuprequest.getpassword ()); account.setactivated (false);//Save User Information Accountpersistservice.createaccount (account) ;//Generate random Activation code and save in temporary activatemap string activationid = Randomgenerator.getrandomstring (); Activationmap.put ( Activationid, Account.getid ());//Create an activation link based on the activation code and the server URL in the request string link = signuprequest.Getactivateserviceurl (). EndsWith ("/")? Signuprequest.getactivateserviceurl () + ACTIVATIONID:SIGNUPREQUEST.GETACTIVATESERVICEURL () + "? key=" + Activationid ///Use the Mail service to send the link to the user accountemailservice.sendmail (Account.getemail (), "Activate Your account", link); catch (Accountcaptchaexception e) {throw new Accountserviceexception ("Unable to validate Captcha.", e);} catch (Accountpe Rsistexception e) {throw new Accountserviceexception ("Unable to create account.", e);} catch (Accountemailexception e) {th Row New Accountserviceexception ("Unable to send actiavtion mail.", e);}} public void Activate (String Activationid) throws Accountserviceexception {//based on the activation code from the temporary Activationmap to find the appropriate user idstring AccountId = Activationmap.get (Activationid);//cannot find the user ID throws an exception if (accountId = = null) {throw new Accountserviceexception (" Invalid account activation ID. "); The user ID is found to update the account status to activate try {accounts = Accountpersistservice.readaccount (accountId); account.setactivated (true); Accountpersistservice.updateaccount (Account);} catch (Accountpersistexception e) {throw new Accountserviceexception ("Unable to activate account.");} public void login (string id, string password) throws Accountserviceexception {try {///read user information based on id account = ACCOUNTPE Rsistservice.readaccount (ID), if (account = = null) {throw new Accountserviceexception ("account does not exist."); if (!account.isactivated ()) {throw new Accountserviceexception ("account is disabled."); if (!account.getpassword (). Equals (password)) {throw new accountserviceexception ("Incorrect password.");}} catch (Accountpersistexception e) {throw new Accountserviceexception ("Unable to log in.", E);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A maven-built Web application (top)