= Spring =
Spring helps development teams everywhere build simple, portable, fast and flexible JVM-based systems and applications.

http://spring.io/guides

http://spring.io/guides/gs/serving-web-content/

http://spring.io/guides/gs/convert-jar-to-war-maven/

== REST service ==
spring.io/guides/gs/rest-service/

=== Structure ===
{{{
.
|-- pom.xml
|-- src
|   `-- main
|       `-- java
|           `-- hello
|               |-- Application.java
|               |-- Greeting.java
|               `-- GreetingController.java
`-- target
    |-- classes
    |   `-- hello
    |       |-- Application.class
    |       |-- Greeting.class
    |       `-- GreetingController.class
    |-- generated-sources
    |   `-- annotations
    |-- gs-rest-service-0.1.0.jar
    |-- gs-rest-service-0.1.0.jar.original
    `-- maven-archiver
        `-- pom.properties

}}}

=== Steps ===

 * mkdir -p /tmp/greetingSpring
 * mkdir -p /tmp/greetingSpring/src/main/java/hello
 * mkdir -p /tmp/greetingSpring/target
 * cd /tmp/greetingSpring

pom.xml 
{{{#!highlihht xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
  <groupId>org.springframework</groupId>
  <artifactId>gs-rest-service</artifactId>
  <version>0.1.0</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.5.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
<properties>
<start-class>hello.Application</start-class>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>http://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
}}}

src/main/java/hello/Application.java 
{{{#!highlight java
package hello;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
}}}

src/main/java/hello/Greeting.java 
{{{#!highlight java
package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}
}}}

src/main/java/hello/GreetingController.java 
{{{#!highlight java
package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

}}}

Build with 
 * mvn clean compile package

Run with:
 * jar -tf target/gs-rest-service-0.1.0.jar # check JAR contents
 * java -jar target/gs-rest-service-0.1.0.jar
 * open URL http://localhost:8080/greeting


== Tomcat REST service ==
Structure
{{{
.
|-- pom.xml
|-- src
    `-- main
        `-- java
            `-- hello
                |-- Application.java
                |-- Greeting.java
                `-- GreetingController.java
}}}

'''pom.xml'''
{{{#!highlight 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>
    <groupId>org.springframework</groupId>
    <artifactId>gs-rest-service-tomcat</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>compile</scope> 
        </dependency>
    </dependencies>

    <properties>
        <start-class>hello.Application</start-class>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>http://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>http://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>
}}}

'''src/main/java/hello/Application.java'''
{{{#!highlight java
package hello;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Configuration;

@ComponentScan
@EnableAutoConfiguration
@Configuration
public class Application  extends SpringBootServletInitializer  {

    public static void main(String[] args) {
        SpringApplication.run(appClass, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(appClass);
    }

    private static Class<Application> appClass = Application.class;
}
}}}

'''src/main/java/hello/Greeting.java'''
{{{#!highlight java
package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}
}}}

'''src/main/java/hello/GreetingController.java'''
{{{#!highlight java
package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello Tomcat, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greetingTomcat")
    public Greeting greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}
}}}

=== Build and deploy ===
 * mvn clean compile package install
 * cp target/gs-rest-service-tomcat-0.1.0.war /usr/local/tomcat/webapps/ 
 * cp target/gs-rest-service-tomcat-0.1.0.war /opt/apache-tomcat-7.0.53/webapps/ # in alternative
 * Open http://localhost:8080/gs-rest-service-tomcat-0.1.0/greetingTomcat


== Spring MVC ==
'''Structure:'''
{{{
.
|-- pom.xml
|-- src
    `-- main
        |-- java
        |   `-- hello
        |       |-- Application.java
        |       `-- GreetingController.java
        `-- resources
            `-- templates
                `-- greeting.html
}}}

 * mkdir -p src/main/java/hello
 * mkdir -p src/main/resources/templates/
 * nano pom.xml
{{{#!highlight xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <groupId>org.springframework</groupId>
    <artifactId>gs-serving-web-content</artifactId>
    <version>0.1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.8.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <start-class>hello.Application</start-class>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestone</id>
            <url>http://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestone</id>
            <url>http://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>
}}}
 * cd src/main/java/hello/
 * nano GreetingController.java
{{{#!highlight java
package hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class GreetingController {

    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }

}
}}}
 * nano src/main/resources/templates/greeting.html
{{{#!highlight html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
}}}
 * nano src/main/java/hello/Application.java 
{{{#!highlight java
package hello;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
}}}
 * mvn clean compile package install
 * java -jar target/gs-serving-web-content-0.1.0.jar
 * Open http://localhost:8080/greeting?name=Yooo

== testSpringMVCJSP ==
Structure Maven:
{{{
.
|-- pom.xml
|-- src
|   `-- main
|       |-- java
|       |   `-- org
|       |       `-- allowed
|       |           `-- bitarus
|       |               `-- hello
|       |                   `-- HelloController.java
|       `-- webapp
|           `-- WEB-INF
|               |-- mvc-dispatcher-servlet.xml
|               |-- pages
|               |   `-- greeting.jsp
|               `-- web.xml
}}}

pom.xml
{{{
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <groupId>org.allowed.bitarus</groupId>
    <artifactId>testSpringMVCJSP</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <dependencies>
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                        <version>4.1.4.RELEASE</version>
                </dependency>
 
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-web</artifactId>
                        <version>4.1.4.RELEASE</version>
                </dependency>
 
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-webmvc</artifactId>
                        <version>4.1.4.RELEASE</version>
                </dependency>
    </dependencies>
</project>
}}}

HelloController.java
{{{
package org.allowed.bitarus.hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hellox(@RequestParam(value="name", required=false,  defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }

}

}}}

mvc-dispatcher-servlet.xml
{{{
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        <context:component-scan base-package="org.allowed.bitarus.hello" />
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix">
                        <value>/WEB-INF/pages/</value>
                </property>
                <property name="suffix">
                        <value>.jsp</value>
                </property>
        </bean>
</beans>
}}}

greeting.jsp
{{{
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p>Hello ${name}</p>
</body>
</html>
}}}

web.xml
{{{
<web-app id="WebApp_ID" version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
        <display-name>Spring MVC Application + JSP</display-name> 
        <servlet>
                <servlet-name>mvc-dispatcher</servlet-name>
                <servlet-class>
                       org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
                <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
                <servlet-name>mvc-dispatcher</servlet-name>
                <url-pattern>/</url-pattern>
        </servlet-mapping>
        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </context-param>
        <listener>
                <listener-class>
                      org.springframework.web.context.ContextLoaderListener
                </listener-class>
        </listener>
</web-app>
}}}

 * mvn clean compile package
 * Access in Tomcat http://localhost:8081/testSpringMVCJSP-0.1.0/hello?name=asd

== testSpringThymeleaf ==
Maven Structure
{{{
.
|-- pom.xml
|-- src
|   `-- main
|       |-- java
|       |   `-- org
|       |       `-- allowed
|       |           `-- bitarus
|       |               `-- hello
|       |                   |-- HelloController.java
|       `-- webapp
|           `-- WEB-INF
|               |-- mvc-dispatcher-servlet.xml
|               |-- pages
|               |   |-- greeting.html
|               |-- web.xml
}}}

pom.xml
{{{#!highlight 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>
    <groupId>org.allowed.bitarus</groupId>
    <artifactId>testSpringMVCThymeleaf</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <dependencies>
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                        <version>4.1.4.RELEASE</version>
                </dependency>
 
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-web</artifactId>
                        <version>4.1.4.RELEASE</version>
                </dependency>
 
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-webmvc</artifactId>
                        <version>4.1.4.RELEASE</version>
                </dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.10</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.10</version>
</dependency>
    </dependencies>
</project>
}}}

HelloController.java
{{{#!highlight java
package org.allowed.bitarus.hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hellox(@RequestParam(value="name", required=false,  defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }

}
}}}

mvc-dispatcher-servlet.xml
{{{#!highlight xml
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        <context:component-scan base-package="org.allowed.bitarus.hello" />
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
  <property name="prefix" value="/WEB-INF/pages/" />
  <property name="suffix" value=".html" />
  <property name="templateMode" value="HTML5" />
  <property name="cacheable" value="false" />
 </bean>
 <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
  <property name="templateResolver" ref="templateResolver" />
 </bean>
 <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
  <property name="templateEngine" ref="templateEngine" />
  <property name="order" value="1" />
 </bean>    
</beans>
}}}

greeting.html
{{{#!highlight html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
}}}

web.xml
{{{#!highlight xml
<web-app id="WebApp_ID" version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
        <display-name>Spring MVC Application + Thymeleaf</display-name> 
        <servlet>
                <servlet-name>mvc-dispatcher</servlet-name>
                <servlet-class>
                       org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
                <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
                <servlet-name>mvc-dispatcher</servlet-name>
                <url-pattern>/</url-pattern>
    </servlet-mapping>
        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </context-param>
        <listener>
                <listener-class>
                      org.springframework.web.context.ContextLoaderListener
                  </listener-class>
        </listener>
</web-app>
}}}

 * mvn clean compile package
 * Access in Tomcat http://localhost:8081/testSpringMVCThymeleaf-0.1.0/hello?name=agh