Spring Boot – Thymeleaf

Spring Boot – Thymeleaf ”; Previous Next Thymeleaf is a Java-based library used to create a web application. It provides a good support for serving a XHTML/HTML5 in web applications. In this chapter, you will learn in detail about Thymeleaf. Thymeleaf Templates Thymeleaf converts your files into well-formed XML files. It contains 6 types of templates as given below − XML Valid XML XHTML Valid XHTML HTML5 Legacy HTML5 All templates, except Legacy HTML5, are referring to well-formed valid XML files. Legacy HTML5 allows us to render the HTML5 tags in web page including not closed tags. Web Application You can use Thymeleaf templates to create a web application in Spring Boot. You will have to follow the below steps to create a web application in Spring Boot by using Thymeleaf. Use the following code to create a @Controller class file to redirect the Request URI to HTML file − package com.tutorialspoint.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class WebController { @RequestMapping(value = “/index”) public String index() { return “index”; } } In the above example, the request URI is /index, and the control is redirected into the index.html file. Note that the index.html file should be placed under the templates directory and all JS and CSS files should be placed under the static directory in classpath. In the example shown, we used CSS file to change the color of the text. You can use the following code and created a CSS file in separate folder css and name the file as styles.css − h4 { color: red; } The code for index.html file is given below − <!DOCTYPE html> <html> <head> <meta charset = “ISO-8859-1” /> <link href = “css/styles.css” rel = “stylesheet”/> <title>Spring Boot Application</title> </head> <body> <h4>Welcome to Thymeleaf Spring Boot web application</h4> </body> </html> The project explorer is shown in the screenshot given below − Now, we need to add the Spring Boot Starter Thymeleaf dependency in our build configuration file. Maven users can add the following dependency into the pom.xml file − <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> Gradle users can add the following dependency in the build.gradle file − compile group: ”org.springframework.boot”, name: ”spring-boot-starter-thymeleaf” The code for main Spring Boot application class file is given below − package com.tutorialspoint.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } The code for Maven – pom.xml is given below − <?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>com.tutorialspoint</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> The code for Gradle – build.gradle is given below − buildscript { ext { springBootVersion = ”1.5.8.RELEASE” } repositories { mavenCentral() } dependencies { classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}”) } } apply plugin: ”java” apply plugin: ”eclipse” apply plugin: ”org.springframework.boot” group = ”com.tutorialspoint” version = ”0.0.1-SNAPSHOT” sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile(”org.springframework.boot:spring-boot-starter-web”) compile group: ”org.springframework.boot”, name: ”spring-boot-starter-thymeleaf” testCompile(”org.springframework.boot:spring-boot-starter-test”) } You can create an executable JAR file, and run the spring boot application by using the following Maven or Gradle commands − For Maven, use the command as shown below − mvn clean install After “BUILD SUCCESS”, you can find the JAR file under the target directory. For Gradle, use the command as shown below − gradle clean build After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory. Run the JAR file by using the command given here − java –jar <JARFILE> Now, the application has started on the Tomcat port 8080 as shown below − Now hit the URL in your web browser and you can see the output as shown − http://localhost:8080/index Print Page Previous Next Advertisements ”;

Tracing Micro Service Logs

Spring Boot – Tracing Micro Service Logs ”; Previous Next Most developers face difficulty of tracing logs if any issue occurred. This can be solved by Spring Cloud Sleuth and ZipKin server for Spring Boot application. Spring Cloud Sleuth Spring cloud Sleuth logs are printed in the following format − [application-name,traceid,spanid,zipkin-export] Where, Application-name = Name of the application Traceid = each request and response traceid is same when calling same service or one service to another service. Spanid = Span Id is printed along with Trace Id. Span Id is different every request and response calling one service to another service. Zipkin-export = By default it is false. If it is true, logs will be exported to the Zipkin server. Now, add the Spring Cloud Starter Sleuth dependency in your build configuration file as follows − Maven users can add the following dependency in your pom.xml file − <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> Gradle users can add the following dependency in your build.gradle file − compile(”org.springframework.cloud:spring-cloud-starter-sleuth”) Now, add the Logs into your Spring Boot application Rest Controller class file as shown here − package com.tutorialspoint.sleuthapp; import java.util.logging.Level; import java.util.logging.Logger; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class SleuthappApplication { private static final Logger LOG = Logger.getLogger(SleuthappApplication.class.getName()); public static void main(String[] args) { SpringApplication.run(SleuthappApplication.class, args); } @RequestMapping(“/”) public String index() { LOG.log(Level.INFO, “Index API is calling”); return “Welcome Sleuth!”; } } Now, add the application name in application.properties file as shown − spring.application.name = tracinglogs The complete code for build configuration file is given below − Maven – 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>com.tutorialspoint</groupId> <artifactId>sleuthapp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sleuthapp</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!– lookup parent from repository –> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> Gradle – build.gradle buildscript { ext { springBootVersion = ”1.5.9.RELEASE” } repositories { mavenCentral() } dependencies { classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}”) } } apply plugin: ”java” apply plugin: ”eclipse” apply plugin: ”org.springframework.boot” group = ”com.tutorialspoint” version = ”0.0.1-SNAPSHOT” sourceCompatibility = 1.8 repositories { mavenCentral() } ext { springCloudVersion = ”Edgware.RELEASE” } dependencies { compile(”org.springframework.cloud:spring-cloud-starter-sleuth”) compile(”org.springframework.boot:spring-boot-starter-web”) testCompile(”org.springframework.boot:spring-boot-starter-test”) } dependencyManagement { imports { mavenBom “org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}” } } You can create an executable JAR file, and run the Spring Boot application by using the following Maven or Gradle commands. For Maven, you can use the following command − mvn clean install After “BUILD SUCCESS”, you can find the JAR file under the target directory. For Gradle, you can use the following command − gradle clean build After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory. Now, run the JAR file by using the command shown here − java –jar <JARFILE> Now, the application has started on the Tomcat port 8080. Now, hit the URL in your web browser and see the output in console log. http://localhost:8080/ You can see the following logs in the console window. Observe that log is printed in the following format [application-name, traceid, spanid, zipkin-export] Zipkin Server Zipkin is an application that monitors and manages the Spring Cloud Sleuth logs of your Spring Boot application. To build a Zipkin server, we need to add the Zipkin UI and Zipkin Server dependencies in our build configuration file. Maven users can add the following dependency in your pom.xml file − <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency> Gradle users can add the below dependency in your build.gradle file − compile(”io.zipkin.java:zipkin-autoconfigure-ui”) compile(”io.zipkin.java:zipkin-server”) Now, configure the server.port = 9411 in application properties file. For properties file users, add the below property in application.properties file. server.port = 9411 For YAML users, add the below property in application.yml file. server: port: 9411 Add the @EnableZipkinServer annotation in your main Spring Boot application class fie. The @EnableZipkinServer annotation is used to enable your application act as a Zipkin server. package com.tutorialspoint.zipkinapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import zipkin.server.EnableZipkinServer; @SpringBootApplication @EnableZipkinServer public class ZipkinappApplication { public static void main(String[] args) { SpringApplication.run(ZipkinappApplication.class, args); } } The code for complete build configuration file is given below. Maven – 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>com.tutorialspoint</groupId> <artifactId>zipkinapp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>zipkinapp</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> <!– lookup parent from repository –> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> Gradle – build.gradle buildscript { ext { springBootVersion = ”1.5.9.RELEASE” } repositories { mavenCentral() } dependencies { classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}”) } } apply plugin: ”java” apply plugin: ”eclipse” apply plugin: ”org.springframework.boot” group = ”com.tutorialspoint” version = ”0.0.1-SNAPSHOT” sourceCompatibility = 1.8 repositories { mavenCentral() } ext { springCloudVersion = ”Edgware.RELEASE” } dependencies { compile(”io.zipkin.java:zipkin-autoconfigure-ui”) compile(”io.zipkin.java:zipkin-server”) testCompile(”org.springframework.boot:spring-boot-starter-test”) } dependencyManagement { imports { mavenBom “org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}” } } You can create an executable JAR file, and run the Spring Boot application by using the below Maven or Gradle commands − For Maven, use the command given below − mvn clean install After “BUILD SUCCESS”, you can find the JAR file under the target directory. For Gradle, use the command given below − gradle clean build After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory. Run the JAR file by using the command shown − java –jar <JARFILE> Now, the application has started on the Tomcat port 9411 as shown below − Now, hit the below URL and see the Zipkin server UI. http://localhost:9411/zipkin/ Then, add the following dependency in your client service application and point out the Zipkin Server URL to trace the microservice logs via Zipkin UI. Now, add

Spring Boot – Code Structure

Spring Boot – Code Structure ”; Previous Next Spring Boot does not have any code layout to work with. However, there are some best practices that will help us. This chapter talks about them in detail. Default package A class that does not have any package declaration is considered as a default package. Note that generally a default package declaration is not recommended. Spring Boot will cause issues such as malfunctioning of Auto Configuration or Component Scan, when you use default package. Note − Java”s recommended naming convention for package declaration is reversed domain name. For example − com.tutorialspoint.myproject Typical Layout The typical layout of Spring Boot application is shown in the image given below − The Application.java file should declare the main method along with @SpringBootApplication. Observe the code given below for a better understanding − package com.tutorialspoint.myproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) {SpringApplication.run(Application.class, args);} } Print Page Previous Next Advertisements ”;

Spring Boot – Discussion

Discuss Spring Boot ”; Previous Next Spring Boot is an open source Java-based framework used to create a Micro Service. It is developed by Pivotal Team. It is easy to create a stand-alone and production ready spring applications using Spring Boot. Spring Boot contains a comprehensive infrastructure support for developing a microservice and enables you to develop enterprise-ready applications that you can “just run”. Print Page Previous Next Advertisements ”;

Spring Boot – Useful Resources

Spring Boot – Useful Resources ”; Previous Next The following resources contain additional information on Spring Boot. Please use them to get more in-depth knowledge on this. Useful Links on Spring Boot Spring Framework − Wikipedia Reference for Spring Boot. Official Website − Official Website of Spring Boot. Useful Books on Spring Boot To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;

Securing Web Applications

Spring Boot – Securing Web Applications ”; Previous Next If a Spring Boot Security dependency is added on the classpath, Spring Boot application automatically requires the Basic Authentication for all HTTP Endpoints. The Endpoint “/” and “/home” does not require any authentication. All other Endpoints require authentication. For adding a Spring Boot Security to your Spring Boot application, we need to add the Spring Boot Starter Security dependency in our build configuration file. Maven users can add the following dependency in the pom.xml file. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> Gradle users can add the following dependency in the build.gradle file. compile(“org.springframework.boot:spring-boot-starter-security”) Securing a Web application First, create an unsecure web application by using Thymeleaf templates. Then, create a home.html file under src/main/resources/templates directory. <!DOCTYPE html> <html xmlns = “http://www.w3.org/1999/xhtml” xmlns:th = “http://www.thymeleaf.org” xmlns:sec = “http://www.thymeleaf.org/thymeleaf-extras-springsecurity3”> <head> <title>Spring Security Example</title> </head> <body> <h1>Welcome!</h1> <p>Click <a th:href = “@{/hello}”>here</a> to see a greeting.</p> </body> </html> The simple view /hello defined in the HTML file by using Thymeleaf templates. Now, create a hello.html under src/main/resources/templates directory. <!DOCTYPE html> <html xmlns = “http://www.w3.org/1999/xhtml” xmlns:th = “http://www.thymeleaf.org” xmlns:sec = “http://www.thymeleaf.org/thymeleaf-extras-springsecurity3”> <head> <title>Hello World!</title> </head> <body> <h1>Hello world!</h1> </body> </html> Now, we need to setup the Spring MVC – View controller for home and hello views. For this, create a MVC configuration file that extends WebMvcConfigurerAdapter. package com.tutorialspoint.websecuritydemo; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController(“/home”).setViewName(“home”); registry.addViewController(“/”).setViewName(“home”); registry.addViewController(“/hello”).setViewName(“hello”); registry.addViewController(“/login”).setViewName(“login”); } } Now, add the Spring Boot Starter security dependency to your build configuration file. Maven users can add the following dependency in your pom.xml file. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> Gradle users can add the following dependency in the build.gradle file. compile(“org.springframework.boot:spring-boot-starter-security”) Now, create a Web Security Configuration file, that is used to secure your application to access the HTTP Endpoints by using basic authentication. package com.tutorialspoint.websecuritydemo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers(“/”, “/home”).permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage(“/login”) .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser(“user”).password(“password”).roles(“USER”); } } Now, create a login.html file under the src/main/resources directory to allow the user to access the HTTP Endpoint via login screen. <!DOCTYPE html> <html xmlns = “http://www.w3.org/1999/xhtml” xmlns:th = “http://www.thymeleaf.org” xmlns:sec = “http://www.thymeleaf.org/thymeleaf-extras-springsecurity3”> <head> <title>Spring Security Example </title> </head> <body> <div th:if = “${param.error}”> Invalid username and password. </div> <div th:if = “${param.logout}”> You have been logged out. </div> <form th:action = “@{/login}” method = “post”> <div> <label> User Name : <input type = “text” name = “username”/> </label> </div> <div> <label> Password: <input type = “password” name = “password”/> </label> </div> <div> <input type = “submit” value = “Sign In”/> </div> </form> </body> </html> Finally, update the hello.html file – to allow the user to Sign-out from the application and display the current username as shown below − <!DOCTYPE html> <html xmlns = “http://www.w3.org/1999/xhtml” xmlns:th = “http://www.thymeleaf.org” xmlns:sec = “http://www.thymeleaf.org/thymeleaf-extras-springsecurity3”> <head> <title>Hello World!</title> </head> <body> <h1 th:inline = “text”>Hello [[${#httpServletRequest.remoteUser}]]!</h1> <form th:action = “@{/logout}” method = “post”> <input type = “submit” value = “Sign Out”/> </form> </body> </html> The code for main Spring Boot application is given below − package com.tutorialspoint.websecuritydemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class WebsecurityDemoApplication { public static void main(String[] args) { SpringApplication.run(WebsecurityDemoApplication.class, args); } } The complete code for build configuration file is given below. Maven – 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>com.tutorialspoint</groupId> <artifactId>websecurity-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>websecurity-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!– lookup parent from repository –> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> Gradle – build.gradle buildscript { ext { springBootVersion = ”1.5.9.RELEASE” } repositories { mavenCentral() } dependencies { classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}”) } } apply plugin: ”java” apply plugin: ”eclipse” apply plugin: ”org.springframework.boot” group = ”com.tutorialspoint” version = ”0.0.1-SNAPSHOT” sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile(”org.springframework.boot:spring-boot-starter-security”) compile(”org.springframework.boot:spring-boot-starter-thymeleaf”) compile(”org.springframework.boot:spring-boot-starter-web”) testCompile(”org.springframework.boot:spring-boot-starter-test”) testCompile(”org.springframework.security:spring-security-test”) } Now, create an executable JAR file, and run the Spring Boot application by using the following Maven or Gradle commands. Maven users can use the command as given below − mvn clean install After “BUILD SUCCESS”, you can find the JAR file under target directory. Gradle users can use the command as shown − gradle clean build After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory. Now, run the JAR file by using the command shown below − java –jar <JARFILE> Hit the URL http://localhost:8080/ in your web browser. You can see the output as shown. Print Page Previous Next Advertisements ”;

SWING – Controls

SWING – Controls ”; Previous Next Every user interface considers the following three main aspects − UI Elements − These are the core visual elements the user eventually sees and interacts with. GWT provides a huge list of widely used and common elements varying from basic to complex, which we will cover in this tutorial. Layouts − They define how UI elements should be organized on the screen and provide a final look and feel to the GUI (Graphical User Interface). This part will be covered in the Layout chapter. Behavior − These are the events which occur when the user interacts with UI elements. This part will be covered in the Event Handling chapter. Every SWING controls inherits properties from the following Component class hiearchy. S.No. Class & Description 1 Component A Component is the abstract base class for the non menu user-interface controls of SWING. Component represents an object with graphical representation 2 Container A Container is a component that can contain other SWING components 3 JComponent A JComponent is a base class for all SWING UI components. In order to use a SWING component that inherits from JComponent, the component must be in a containment hierarchy whose root is a top-level SWING container SWING UI Elements Following is the list of commonly used controls while designing GUI using SWING. S.No. Class & Description 1 JLabel A JLabel object is a component for placing text in a container. 2 JButton This class creates a labeled button. 3 JColorChooser A JColorChooser provides a pane of controls designed to allow a user to manipulate and select a color. 4 JCheck Box A JCheckBox is a graphical component that can be in either an on (true) or off (false) state. 5 JRadioButton The JRadioButton class is a graphical component that can be in either an on (true) or off (false) state. in a group. 6 JList A JList component presents the user with a scrolling list of text items. 7 JComboBox A JComboBox component presents the user with a to show up menu of choices. 8 JTextField A JTextField object is a text component that allows for the editing of a single line of text. 9 JPasswordField A JPasswordField object is a text component specialized for password entry. 10 JTextArea A JTextArea object is a text component that allows editing of a multiple lines of text. 11 ImageIcon A ImageIcon control is an implementation of the Icon interface that paints Icons from Images 12 JScrollbar A Scrollbar control represents a scroll bar component in order to enable the user to select from range of values. 13 JOptionPane JOptionPane provides set of standard dialog boxes that prompt users for a value or informs them of something. 14 JFileChooser A JFileChooser control represents a dialog window from which the user can select a file. 15 JProgressBar As the task progresses towards completion, the progress bar displays the task”s percentage of completion. 16 JSlider A JSlider lets the user graphically select a value by sliding a knob within a bounded interval. 17 JSpinner A JSpinner is a single line input field that lets the user select a number or an object value from an ordered sequence. Print Page Previous Next Advertisements ”;

SWING – Environment

SWING – Environment Setup ”; Previous Next This section guides you on how to download and set up Java on your machine. Please use the following steps to set up the environment. Java SE is freely available from the link Download Java. Hence, you can download a version based on your operating system. Follow the instructions to download Java and run the .exe to install Java on your machine. Once you have installed Java on your machine, you would need to set the environment variables to point to the correct installation directories. Setting Up the Path for Windows 2000/XP Assuming you have installed Java in c:Program Filesjavajdk directory − Step 1 − Right-click on ”My Computer” and select ”Properties”. Step 2 − Click the ”Environment variables” button under the ”Advanced” tab. Step 3 − Alter the ”Path” variable so that it also contains the path to the Java executable. Example, if the path is currently set to ”C:WINDOWSSYSTEM32”, then change your path to read ”C:WINDOWSSYSTEM32;c:Program Filesjavajdkbin”. Setting Up the Path for Windows 95/98/ME Assuming you have installed Java in c:Program Filesjavajdk directory − Step 1 − Edit the ”C:autoexec.bat” file and add the following line at the end: ”SET PATH=%PATH%;C:Program Filesjavajdkbin”. Setting Up the Path for Linux, UNIX, Solaris, FreeBSD Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your Shell documentation if you have trouble doing this. Example, if you use bash as your shell, then you would add the following line to the end ”.bashrc: export PATH=/path/to/java:$PATH”. Popular Java Editors To write your Java programs, you will need a text editor. There are even more sophisticated IDE available in the market. But for now, you can consider one of the following − Notepad − On Windows machine, you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad. Netbeans − Netbeans is a Java IDE that is open source and free, which can be downloaded from https://www.netbeans.org/index.html. Eclipse − Eclipse is also a Java IDE developed by the Eclipse open source community and can be downloaded from https://www.eclipse.org/. Print Page Previous Next Advertisements ”;

TestNG – Useful Resources

TestNG – Useful Resources ”; Previous Next The following resources contain additional information on TestNG. Please use them to get more in-depth knowledge on this topic. Useful Video Courses Selenium And Java Advanced Course with Live Project Best Seller 296 Lectures 146 hours Arun Motoori More Detail TestNG Complete Bootcamp For Beginners 39 Lectures 4.5 hours Lets Kode It More Detail Selenium WebDriver 4 with Java – Zero To Hero Most Popular 294 Lectures 41.5 hours Lets Kode It More Detail தமிழ் மொழியில் Selenium கற்றுக்கொள்ளுங்கள் | Learn Selenium 35 Lectures 8.5 hours Programming Line More Detail Learn TestNG 15 Lectures 1.5 hours Quaatso Learning More Detail The Complete Selenium WebDriver with Java Course Most Popular 202 Lectures 21 hours Dezlearn Education More Detail Print Page Previous Next Advertisements ”;

TestNG – Overview

TestNG – Overview ”; Previous Next Testing is the process of checking the functionality of an application to ensure it works as per requirements. Unit testing comes into picture at the developer level where adequate measures are taken to test every single entity (class or method) to ensure the final product meets the requirements. JUnit has driven developers to understand the usefulness of tests, especially of unit tests, when compared to any other testing framework. Leveraging a rather simple, pragmatic, and strict architecture, JUnit has been able to “infect” great number of developers. Do take a look at our tutorial on JUnit to have a good understanding of its features. JUnit, at the same time, has some shortcomings as well, which are listed below − Initially designed to enable unit testing only, now used for all kinds of testing. Cannot do dependency testing. Poor configuration control (setUp/tearDown). Intrusive (forces you to extend classes and name your methods a certain way). Static programming model (forces you to recompile unnecessarily). The management of different suites of tests in complex projects can be very tricky. What is TestNG? Definition of TestNG as per its documentation is as follows − TestNG is a testing framework inspired from JUnit and NUnit, but introducing some new functionalities that make it more powerful and easier to use. TestNG is an open source automated testing framework; where NG means NextGeneration. TestNG is similar to JUnit (especially JUnit 4), but it is not a JUnit extension. It is inspired by JUnit. It is designed to be better than JUnit, especially when testing integrated classes. The creator of TestNG is Cedric Beust. Eliminating most of the limitations of the older framework, TestNG gives the developer the ability to write more flexible and powerful tests. As it heavily borrows from Java Annotations (introduced with JDK 5.0) to define tests, it can also show you how to use this new feature of the Java language in a real production environment. TestNG Features Supports annotations. TestNG uses more Java and OO features. Supports testing integrated classes (e.g., by default, no need to create a new test class instance for every test method). Separates compile-time test code from run-time configuration/data info. Flexible runtime configuration. Introduces ‘test groups’. Once you have compiled your tests, you can just ask TestNG to run all the “front-end” tests, or “fast”, “slow”, “database” tests, etc. Supports Dependent test methods, parallel testing, load testing, and partial failure. Flexible plug-in API. Support for multi threaded testing. Print Page Previous Next Advertisements ”;