Project Structure:
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.developer</groupId>
<artifactId>SpringAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringAPI</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.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-data-jpa</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>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
model/Users.java
package com.developer.SpringAPI.model;
/**
* Created by Pukar on 6/12/2017.
*/
public class Users {
public int id;
public String username;
public Users(int id, String username) {
this.id = id;
this.username = username;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
data/UsersData.java
package com.developer.SpringAPI.data;
import com.developer.SpringAPI.model.Users;
import java.util.Arrays;
import java.util.List;
import java.util.List.*;
import org.hibernate.criterion.ProjectionList;
import org.springframework.stereotype.Component;
/**
* Created by Pukar on 6/13/2017.
*/
@Component
public class UsersData {
private static final List<Users> usersList = Arrays.asList(
new Users(1,"Ram"),
new Users(2,"Pukar"),
new Users(3,"Chaudhary"),
new Users(4,"Suraj"),
new Users(5,"Rojish")
);
public Users findByUsername(String name){
for(Users users: usersList){
if(users.getUsername().equals(name)){
return users;
}
}
return null;
}
public List<Users> allList(){
return usersList;
}
}
controller/MainController.java
package com.developer.SpringAPI.controller;
import com.developer.SpringAPI.data.UsersData;
import com.developer.SpringAPI.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
/**
* Created by Pukar on 6/12/2017.
*/
@Controller
public class MainController {
@Autowired
public UsersData userData;
@RequestMapping(value = "/")
public String goHome(ModelMap modelMap){
List<Users> users = userData.allList();
modelMap.put("list",users);
return "index";
}
@RequestMapping(value = "/views")
public String goView(ModelMap modelMap){
Users users = userData.findByUsername("Ram");
modelMap.put("list",users);
return "views";
}
}
src\main\resources\templates\index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<div th:each="list : ${list}">
<p th:text="${list.username}"></p>
</div>
</body>
</html>
src\main\resources\templates\views.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<p th:text="${list.username}">Welcome to our grocery store!</p>
</body>
</html>
src\main\resources\application.properties
spring.datasource.url=jdbc:mysql://localhost/java_db
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver
Demo:
0 comments:
Post a Comment