Spring Boot Environment Setup

#1 Open the url https://start.spring.io/

#2 In group, type com.<<your_name>>

#3 In artifact, type user-app

#4 In Dependencies, select web

#5 Click Generate Projectide

#6 Unzip user-app.zip and import into any java IDE like STS

Advertisements

Project Setup

#1 Create a new angular project

D:\BootcampProjects>ng new user-app

#2 Open the project in any editor like visual studio code

D:\BootcampProjects>cd user-app

D:\BootcampProjects\user-app>code .

#3 Understand the files generated,

Modules – Modules break up the application into logical pieces of code. Each piece of code or module is designed to perform a single task.All the modules are loaded by main.ts.

Component – Component is used to bring the modules together.

tsconfig.json – The presence of tsconfig.json file in a directory indicates that the directory is the root of a typescript project.This file specifies the root files and typescript compiler motions required to compile typescript.

package.json – It contains all the dependencies defined for the angular project.Once, we do nom install, these dependencies are automatically downloaded.

karma.conf.json – Config file for karma unit tests.

angular-cli.json –  Have all the application configurations such as info about the root and out directory.The welcome or main html file along with the main ts file. All the other environment related information will be present here.

#4 Run the application and view the output in the browser by hitting the following command in the Integrated terminal (open it by pressing ctrl+`) or in command prompt

D:\BootcampProjects\user-app>ng s

#5 Open the browser and hit the URL http://localhost:4200/

Environment Setup

#1 Install NodeJs

#2 Open the command prompt and check the version by typing node -v

node -v

#3 Execute following commands to install Angular CLI and to check the version

npm install -g @angular/cli

npm -v

 

Angular 5 Features

  1. Faster, lighter & easy to use
  2. Material design capabilities & improved compiler with build optimizer
  3. A complete rewrite of existing HttpModule is introduced
  4. Supports TypeScript 2.4
  5. Supports Reactive Forms

Spring Course – Hello World Spring Maven Web Project using Java configuration

Day #4 – Hello World Spring Maven Web Project using Java configuration

img1

#1 Add properties and dependencies in 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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.janani</groupId>
	<artifactId>bookapp-spring</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>bookapp-spring Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<failOnMissingWebXml>false</failOnMissingWebXml>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.11.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>6.0.6</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>bookapp-spring</finalName>
	</build>
</project>

#2 Create Java Configuration file SpringConfig.java and WebConfig.java under src=>main

  • Right click main=>New=>Folder=>java=>Finish
  • Right click java=>New=>Class
    • In package=>type com.janani.config and In Class=>SpringConfig=>Finish
    • In package=>type com.janani.config and In Class=>WebConfig=>Finish
package com.janani.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.janani")
public class SpringConfig extends WebMvcConfigurerAdapter {

	@Bean
	public ViewResolver viewResolver() {

		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setViewClass(JstlView.class);
		viewResolver.setPrefix("/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}

	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
}

 

package com.janani.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebConfig implements WebApplicationInitializer {
	public void onStartup(ServletContext ctx) throws ServletException {
		AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();
		webCtx.register(SpringConfig.class);
		webCtx.setServletContext(ctx);
		ServletRegistration.Dynamic servlet = ctx.addServlet("dispatcher", new DispatcherServlet(webCtx));
		servlet.setLoadOnStartup(1);
		servlet.addMapping("/");
	}
}

#3 Create a Controller

  • In bookapp-spring=>src=>main=>java=>com=>janani=>controller, create a class HomeController
package com.janani.controller;

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

@Controller
@RequestMapping("/home")
public class HomeController {
	@GetMapping
	public String home() {
		return "index";
	}
}

#3 Run the Project 

Hint:In menu bar=>Project=>Clean=>Clean all projects

  • Right click the project bookapp-spring=>Run As=>Run on Server=>Finish
  • Displays default jsp i.e. index.jsp

img3.png

  • Access http://localhost:8080/bookapp-spring/home, it also points to index.jsp(as specified in HomeController)

img2