Archives

Spring Course – Bookapp using Spring MVC Maven and Java Configuration

Day #5 – Bookapp using Spring MVC Maven and Java Configuration

Follow the instructions in Readme.md=>https://github.com/JananiVelmurugan/bookapp-springmvc

img4

img5

img6

img7.png

img8.png

 

img9img10

img11.png

img12

Advertisements

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

Spring Course – Hello World Maven Web Project

Day #3 – Hello World Maven Web Project

#1 Configure Apache Tomcat Web Server(https://www.youtube.com/watch?v=JRGZSV79Q74)

  • In Eclipse, Window=>Show View=>Servers
  • In the mid of Servers window=>right click=>New=>Server=>Apache=>select installed version(Tomcat v9.0 Server)=>Next
  • Click Browse=>C:\apache-tomcat-9.0.0.M21(Location of server in your machine)
  • Finish

#2 Create a maven project (Internet is mandatory to download maven dependencies)

  • In Eclipse, File=>New=>Maven Project=>Next=>Type web in Filter
  • Select maven-archetype-webapp=>Next
  • Enter Group Id=>com.janani and Artifact Id=>bookapp-spring=>Finish

#3 Remove web.xml(Since it points to older version-2.3 but recent version is 3.1)

  • In bookapp-spring=>src=>main=>webapp=>WEB-INF=>Right click web.xml and delete

#4 Run the application

Spring Course – Basic Git Commands

Day #2 – Basic Git Commands

  • Download and install git – https://git-scm.com/downloads
  • Create github repository in your github account
  • Go to the project location and type cmd in taskbar
  • Push the project to git using git commands as follows,
    git init
    git add --all
    git commit -m "first commit"
    git remote add origin https://github.com/JananiVelmurugan/bookapp-mvn.git
    git push -u origin master
  • Now reload the repository you should see the uploaded files
  • Go to the project add some code/files and then push the files as follows,
    git add --all
    git commit -m "second commit"
    git push
  • Again reload the repository you should see the recently uploaded files

Spring Course – Core Java & Web Applications

Day #1 – Core Java & Web Applications

Swagger – Spring Boot

Step #1: Add maven dependency

                <dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.6.1</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.6.1</version>
		</dependency>

Step #2: Create a SwaggerConfig.java in com.janani.config

package com.revature.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

	@Bean
	public Docket api() {
		// @formatter:off
		return new Docket(DocumentationType.SWAGGER_2)
				.groupName("lms-api")
				.apiInfo(apiInfo())
				.select()
				.paths(PathSelectors.any()).build();
	}

	// @formatter:on
	// private Predicate<String> fetchPaths() {
	// return or(regex("/holidays.*"), regex("/roles.*"),
	// regex("/employees.*"));
	// }

	private ApiInfo apiInfo() {
		// @formatter:off
		return new ApiInfoBuilder()
				.title("LMS API")
				.description("Leave Management System")
				.license("LMS License")
				.contact("jananise@gmail.com")
				.licenseUrl("https://github.com/JananiVelmurugan")
				.version("1.0").build();
	}

}

Step #3: http://localhost:5000/swagger-ui.html#/

Spring demo – Sequence

Step 1: create maven web-app
Step 2: delete web.xml
Step 3: add dependency in pom.xml
Step 4: Spring Boot -> Application.java ( Run as -> Java Application )
Expected: application started
Step 5: Home Controller(with print statements) + index.jsp
Expected: Print statement given inside HomeController
Step 7: Pass parameters in controller