Skip to main content

Part 3 - Introducing the domain objects and first lead create view

As I decided that I will go with static forms instead of dynamic ones, I set sail creating the createlead.html page with Thymeleaf support and Spring MVC.The listing below shows the form:

Listing 1 - createlead.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">

<head th:include="common :: headerFragment">

<title>effectiv:Home</title>

</head>

<body>

<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">

<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse"
data-target=".nav-collapse"> <span class="icon-bar"></span> <span
class="icon-bar"></span> <span class="icon-bar"></span>
</a> <a class="brand" href="#">Project name</a>
<div class="nav-collapse collapse">

<p class="navbar-text pull-right">
Logged in as <a href="#" class="navbar-link">Username</a>
</p>
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>

</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
</div>

<div class="container-fluid">
<div class="row-fluid">
<div class="span3">

<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Sidebar</li>
<li class="active"><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>

<li class="nav-header">Sidebar</li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>

<li><a href="#">Link</a></li>
<li class="nav-header">Sidebar</li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>

</div>
<!--/.well -->
</div>
<!--/span-->

<div class="span9">

<div class="well">

<form class="form-horizontal" action="#" th:action="@{/savelead}" th:object="${lead}" method="post">

<h4>New Lead</h4>

<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
<button type="button" class="btn">Cancel</button>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="firstName">First Name</label>
<div class="controls">
<input type="text" th:field="*{firstName}" placeholder="First Name"></input>
</div>
</div>

<div class="span4">
<label class="control-label" for="lastName">Last Name</label>
<div class="controls">
<input type="text" placeholder="Last Name" th:field="*{lastName}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="opportunityAmount">Opportunity Amount</label>
<div class="controls">
<input type="text" placeholder="Opportunity Amount" th:field="*{opportunityAmount}"></input>
</div>
</div>

<div class="span4">
<label class="control-label" for="converted">Converted</label>
<div class="controls">
<input type="checkbox" disabled="true" th:field="*{converted}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="email">Email</label>
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span>
<input type="text" placeholder="Email" th:field="*{email}"></input>
</div>
</div>
</div>

<div class="span4">
<label class="control-label" for="mobile">Mobile</label>
<div class="controls">
<input type="text" placeholder="Mobile" th:field="*{mobile}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="workPhone">Work Phone</label>
<div class="controls">
<input type="text" placeholder="Work Phone" th:field="*{workPhone}"></input>
</div>
</div>

<div class="span4">
<label class="control-label" for="fax">Fax</label>
<div class="controls">
<input type="text" placeholder="Fax" th:field="*{fax}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span8">
<label class="control-label" for="description">Description</label>
<div class="controls">

<textarea rows="4" cols="20" placeholder="Description" th:field="*{description}"></textarea>
</div>
</div>

</div>
</div>

<hr></hr>
<h5>Address</h5>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="city">City</label>
<div class="controls">

<input type="text" id="city" placeholder="City" th:field="*{address.city}"></input>
</div>
</div>

<div class="span4">
<label class="control-label" for="country">Country</label>
<div class="controls">
<input type="text" id="country" placeholder="country" th:field="*{address.country}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span4">
<label class="control-label" for="postcode">Post Code</label>
<div class="controls">

<input type="text" id="postcode" placeholder="Post Code" th:field="*{address.postcode}"></input>
</div>
</div>

<div class="span4">
<label class="control-label" for="state">State</label>
<div class="controls">
<input type="text" id="state" placeholder="State" th:field="*{address.state}"></input>
</div>
</div>
</div>
</div>

<div class="control-group">

<div class="row-fluid">
<div class="span8">
<label class="control-label" for="street">Street</label>
<div class="controls">

<textarea rows="4" cols="60" placeholder="Street" th:field="*{address.street}"></textarea>
</div>
</div>

</div>
</div>

<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
<button type="button" class="btn">Cancel</button>
</div>

</form>
</div>

</div>
<!--/span-->

</div>
<!--/row-->

<hr></hr>

<footer>
<p>&copy; Company 2012</p>
</footer>

</div>
<!--/.fluid-container-->

<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../assets/js/jquery.js"></script>
<script src="../assets/js/bootstrap-transition.js"></script>
<script src="../assets/js/bootstrap-alert.js"></script>
<script src="../assets/js/bootstrap-modal.js"></script>

<script src="../assets/js/bootstrap-dropdown.js"></script>
<script src="../assets/js/bootstrap-scrollspy.js"></script>
<script src="../assets/js/bootstrap-tab.js"></script>
<script src="../assets/js/bootstrap-tooltip.js"></script>
<script src="../assets/js/bootstrap-popover.js"></script>
<script src="../assets/js/bootstrap-button.js"></script>

<script src="../assets/js/bootstrap-collapse.js"></script>
<script src="../assets/js/bootstrap-carousel.js"></script>
<script src="../assets/js/bootstrap-typeahead.js"></script>

</body>
</html>

 

Next step is to modify the guest controller and introduce a new savelead controller as shown in Listing 2

Listing 2 - SaveLeadController.java

package com.effectivcrm.controller;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.effectivcrm.domain.Address;
import com.effectivcrm.domain.Lead;

@Controller
@Slf4j
public class SaveLeadController {

@ModelAttribute
public Lead initLead(){
Lead lead = new Lead();
lead.setAddress(new Address());

log.info("Lead = {}", lead);
log.info("Address = {}", lead.getAddress());

return new Lead();
}

@RequestMapping("/savelead")
public String saveLead(Lead lead){
log.info("Lead for saving = {}", lead);
return "createlead";
}

@RequestMapping(value="/createlead")
public String createLead(){
return "createlead";
}

}

 

Now the most important thing we introduce the domain objects. For this we need the Hibernate dependencies. These are added in the parent pom.xml. The domain object project is created by Maven quickstart archetype. The domain object Lead extends from PersistentObject class and also embeds an Address object. Note the use of nested object with Spring EL and Thmyeleaf in Listing 1. All source code is available in SVN.

Listing 3 - PeristentObject.java

/**
*
*/
package com.effectivcrm.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.envers.Audited;
import org.joda.time.DateTime;

/**
* @author Dhrubo
* @param
* @param
*
*/
@Audited
@MappedSuperclass
@EqualsAndHashCode(exclude={"createdBy", "lastModifiedBy", "createdDate", "lastModifiedDate", "deleted", "version"})
@ToString(exclude={"createdBy", "lastModifiedBy", "createdDate", "lastModifiedDate", "deleted", "version"})
public abstract class PersistentObject implements Serializable{

private static final long serialVersionUID = 1L;

@Getter @Setter
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "id", updatable = false, nullable = false, unique = true)
private ID id;

@Getter @Setter
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "created_by", referencedColumnName = "user_id", nullable = false, updatable = false)
private U createdBy;

@Getter @Setter
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "last_modified_by", referencedColumnName = "user_id", nullable = false, updatable = true)
private U lastModifiedBy;

@Getter @Setter
@Temporal(TemporalType.TIMESTAMP)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "created_date", nullable = false, updatable = false)
private DateTime createdDate;

@Getter @Setter
@Temporal(TemporalType.TIMESTAMP)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "last_modified_date", nullable = false, updatable = true)
private DateTime lastModifiedDate;

@Getter @Setter
@Column(name = "deleted")
private boolean deleted;

@Getter @Setter
@Version
@Column(name = "version")
private int version;

}

 

Listing 4 - User.java

package com.effectivcrm.domain;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.envers.Audited;
import org.joda.time.DateTime;

/**
* @author Dhrubo
*
*/
@Audited
@Entity
@Table(name = "t_user")
public class User implements Serializable{
private static final long serialVersionUID = 1L;

@Getter @Setter
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "user_id", updatable = false, nullable = false, unique = true)
private String id;

@Getter @Setter
@Column(name="first_name" ,nullable=false , length=50)
private String firstName;

@Getter @Setter
@Column(name="last_name" ,nullable=false , length=50)
private String lastName;

@Getter @Setter
@Column(name="email",unique=true ,nullable=false, length=100)
private String email;

@Getter @Setter
@Column(name="password",unique=true ,nullable=false, length=100)
private String password;

@Getter @Setter
@Temporal(TemporalType.TIMESTAMP)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "created_date", nullable = false, updatable = false)
private DateTime createdDate;

@Getter @Setter
@Temporal(TemporalType.TIMESTAMP)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "last_modified_date", nullable = false, updatable = false)
private DateTime lastModifiedDate;

@Getter @Setter
@Deprecated
@Temporal(TemporalType.TIMESTAMP)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "activation_date", nullable = true, updatable = false)
private DateTime activationDate;

@Getter @Setter
@Column(name="deleted" ,nullable=false)
private boolean deleted;

@Getter @Setter
@Column(name="locked" ,nullable=false)
private boolean locked;

}

 

Listing 5 - Address.java

package com.effectivcrm.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import lombok.Getter;
import lombok.Setter;

/**
* @author Dhrubo
*
*/
@Embeddable
public class Address implements Serializable {
private static final long serialVersionUID = 1L;

@Getter @Setter
@Column(name="city", insertable=true , updatable=true, length=50)
private String city;

@Getter @Setter
@Column(name="country", insertable=true , updatable=true, length=50)
private String country;

@Getter @Setter
@Column(name="postcode", insertable=true , updatable=true, length=50)
private String postcode;

@Getter @Setter
@Column(name="state", insertable=true , updatable=true, length=50)
private String state;

@Getter @Setter
@Column(name="street", insertable=true , updatable=true, length=50)
private String street;

}

 

Listing 6 - Lead.java

package com.effectivcrm.domain;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;

import org.hibernate.envers.Audited;

/**
* The persistent class for the leads database table.
*
*/
@Entity
@Table(name = "t_lead")
@Audited
public class Lead extends PersistentObject {
private static final long serialVersionUID = 1L;

@Getter @Setter
@Column(name = "first_name", length = 100)
private String firstName;

@Getter @Setter
@Column(name = "last_name", length = 100)
private String lastName;

@Getter @Setter
@Column(name = "opportunity_amount")
private double opportunityAmount;

@Getter @Setter
@Column(name = "converted")
private boolean converted;

@Getter @Setter
@Column(name = "description", length = 250)
private String description;

@Getter @Setter
@Column(name = "email", length = 100)
private String email;

@Getter @Setter
@Column(name = "mobile", length = 30)
private String mobile;

@Getter @Setter
@Column(name = "work_phone", length = 30)
private String workPhone;

@Getter @Setter
@Column(name="fax")
private String fax;

@Getter @Setter
@Embedded
private Address address;

}

So whats next in part 3

  • Tiles integration
  • Internationalized messages
  • Validation
  • Spring Security integration
  • Introduction of service layer
  • Introduction of repository layer

    Comments

    Popular posts from this blog

    CKEDITOR 3.x - Simplest Ajax Submit Plugin

      I have assumed that you have downloaded and got started with CKEDITOR. Step 1 – The html file is shown below: <html> <head> <title>Writer</title> <meta content="text/html; charset=utf-8" http-equiv="content-type" /> <script type="text/javascript" src="ckeditor/ckeditor.js"></script> <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <style> .cke_contents { height: 400px !important; } </style> </head> <body> <form action="sample_posteddata.php" method="post"> <textarea id="editor" > </textarea> <script type="text/javascript"> //<![CDATA[ CKEDITOR.replace( 'editor', { fullPage : true, uiColor : '#9AB8F3', toolbar : 'MyToolbar' }); //]]> </script> </form> </body> </html> Note that the jquery js

    Part 3 - Integrating Tiles, Thymeleaf and Spring MVC 3

    In this post I will demonstrate how to integrate Apache Tiles with Thymeleaf. This is very simple. The first step is to include the tiles and thymeleaf-tiles extension dependencies. I will include them in the pom.xml. Note we wil lbe using Tiles 2.2.2 Listing 1 - parent/pom.xml --- thymeleaf-tiles and tiles dependencies <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- Tiles --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>${tiles.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-template</artifactId> <version>${tiles.version}</version> <scope>compile</s

    How to Stand up a Spring Cloud Config Server?

    Setup and Configure Spring Cloud Config Server Project Spring Cloud Config Server is just another Spring Boot application. It provides several infrastructure micro services to centralize access to configuration information backed by a version controlled (well at least in the case of default GIT storage) repository. Step 1 - Create a Spring Boot project in STS with the dependencies shown in Figure 2. Figure 1 - Creating Spring Boot project to setup Spring Cloud Config Server Figure 2 - Spring Cloud Config Server dependencies Click on 'Finish' to complete the creation of the Spring Boot project in STS. The build.gradle file is shown in listing below. There is only one dependency to the Spring Cloud Config Server. Also Spring Cloud release train 'Dalston.SR1'. Step 2 - Annotate the class containing main method The next step is to annotate the ConfigServerInfraApplication class with  @EnableConfigServer That's all is needed on the Java si