Archive | February 2017

Add Maven Dependency – web project

Add maven dependencies to deployment assembly,
1) right click project
2) navigate to deployment assembly
3) add the maven dependencies library.

Advertisements

Maven – web.xml (Web Project)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
</web-app>

ConnectionUtil – Spring JdbcTemplate

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class ConnectionUtil {
	public static BasicDataSource dataSource() {

		BasicDataSource ds = new BasicDataSource();
		ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
		ds.setUrl("jdbc:mysql://localhost:3306/courses_db");
		ds.setUsername("root");
		ds.setPassword("password");
		return ds;

	}

	public static JdbcTemplate getJdbcTemplate() {
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
		jdbcTemplate.setDataSource(dataSource());
		return jdbcTemplate;
	}
}

Maven (web app) Properties

	<properties>
		<failOnMissingWebXml>false</failOnMissingWebXml>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

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

Spring – Maven Web Project

Step 1: Create a dynamic web project

Step 2: Copy the web.xml

Step 3: Create a maven web project and replace the web.xml with the copied web.xml from dynamic web project

Step 4: Add required dependencies in pom.xml