Cara Buat Aplikasi Absensi Dengan FrameWork

 Untuk langka awal open link di samping https://start.spring.io/ di dalam ini kita akan membuat project spring kita jika sudah maka klik generate project maka otomatis kita akan akan mendownload project yang di buat tdi, lalu kita buka Netbeans dan open project tadi di dalam Netbeans


Begilah tampilanya di dalam NetBeans  dan buatlah package dan class java sesuai pada gambar di bawah


Di Bawah ini dalah kodingan lengkap tinggal di copi paste aja kalo mau :


AdminHtmlController.java
package com.jaya5.db_absensi.controller;

importjava.sql.Date;
importjava.text.SimpleDateFormat;
importjavax.validation.Valid;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.propertyeditors.CustomDateEditor;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.validation.BindingResult;
importorg.springframework.web.bind.WebDataBinder;
importorg.springframework.web.bind.annotation.InitBinder;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RequestParam;
import com.jaya5.db_absensi.dao.AdminDao;
import com.jaya5.db_absensi.entity.Admin;

/**
 *
 * @author Aras
 */
@Controller
@RequestMapping("/admin")
public class AdminHtmlController {

    @Autowired
privateAdminDaopd;

    @InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormatdateFormat
                = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class,
newCustomDateEditor(dateFormat, true));
    }

@RequestMapping("/list")
public void daftarAdmin(Model m) {
m.addAttribute("daftarAdmin", pd.findAll());
    }
@RequestMapping("/view")
public void lihatdaftar(Model m){
m.addAttribute("lihatDaftar", pd.findAll());
    }
@RequestMapping("/hapus")
public String hapus(@RequestParam("id") String id) {
pd.delete(id);
return "redirect:list";
    }

@RequestMapping(value = "/form", method = RequestMethod.POST)
public String form(@Valid Admin p,
BindingResult errors) {
if (errors.hasErrors()) {
return "/admin/form";
        }
pd.save(p);
return "redirect:list";
    }

@RequestMapping(value = "/form", method = RequestMethod.GET)
public String form(@RequestParam(value = "id",
required = false) String id, Model m) {
m.addAttribute("admin", new Admin());
if (id != null && !id.isEmpty()) {
            Admin p = pd.findOne(id);
if (p != null) {
m.addAttribute("admin", p);
            }
        }
return "/admin/form";
    }
}


SiswaHtmlController.java
package com.jaya5.db_absensi.controller;

importjava.sql.Date;
importjava.text.SimpleDateFormat;
importjavax.validation.Valid;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.propertyeditors.CustomDateEditor;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.validation.BindingResult;
importorg.springframework.web.bind.WebDataBinder;
importorg.springframework.web.bind.annotation.InitBinder;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RequestParam;
import com.jaya5.db_absensi.dao.SiswaDao;
import com.jaya5.db_absensi.entity.Siswa;

/**
 *
 * @author Aras
 */
@Controller
@RequestMapping("/siswa")
public class SiswaHtmlController {

    @Autowired
privateSiswaDaopd;

    @InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormatdateFormat
                = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class,
newCustomDateEditor(dateFormat, true));
    }

@RequestMapping("/list")
public void daftarSiswa(Model m) {
m.addAttribute("daftarSiswa", pd.findAll());
    }
@RequestMapping("/coba")
public void coba(Model m) {
m.addAttribute("daftarSiswa", pd.findAll());
    }
@RequestMapping("/view")
public void lihatdaftar(Model m){
m.addAttribute("lihatDaftar", pd.findAll());
    }
@RequestMapping("/hapus")
public String hapus(@RequestParam("id") String id) {
pd.delete(id);
return "redirect:list";
    }

@RequestMapping(value = "/form", method = RequestMethod.POST)
public String form(@Valid Siswa p,
BindingResult errors) {
if (errors.hasErrors()) {
return "/siswa/form";
        }
pd.save(p);
return "redirect:list";
    }

@RequestMapping(value = "/form", method = RequestMethod.GET)
public String form(@RequestParam(value = "id",
required = false) String id, Model m) {
m.addAttribute("siswa", new Siswa());
if (id != null && !id.isEmpty()) {
Siswa p = pd.findOne(id);
if (p != null) {
m.addAttribute("siswa", p);
            }
        }
return "/siswa/form";
    }
}


AdminDao.java
package com.jaya5.db_absensi.dao;

import com.jaya5.db_absensi.entity.Admin;
importjava.io.Serializable;
importorg.springframework.data.repository.PagingAndSortingRepository;

/**
 *
 * @author Lab-MM10
 */
public interface AdminDao extends PagingAndSortingRepository<Admin, String>{

}


SiswaDao.java
package com.jaya5.db_absensi.dao;

import com.jaya5.db_absensi.entity.Siswa;
importjava.io.Serializable;
importorg.springframework.data.repository.PagingAndSortingRepository;

/**
 *
 * @author Lab-MM10
 */
public interface SiswaDao extends PagingAndSortingRepository<Siswa, String>{

}


Admin.java
package com.jaya5.db_absensi.entity;

importjavax.persistence.*;
importorg.hibernate.annotations.CollectionId;
importorg.hibernate.annotations.GenericGenerator;

/**
 *
 * @author Lab-MM10
 */
@Entity
@Table (name = "tbl_Admin")
public class Admin {
    @Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid",strategy = "uuid2")
private String id;
@Column(nullable = false, length = 20)
private String username;
@Column(nullable = false, length = 20)
private String password;

public String getId() {
return id;
    }

public void setId(String id) {
        this.id = id;
    }

public String getUsername() {
return username;
    }

public void setUsername(String username) {
this.username = username;
    }

public String getPassword() {
return password;
    }

public void setPassword(String password) {
this.password = password;
    }


}


Siswa.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jaya5.db_absensi.entity;

importjavax.persistence.*;
importorg.hibernate.annotations.CollectionId;
importorg.hibernate.annotations.GenericGenerator;

/**
 *
 * @author Lab-MM10
 */
@Entity
@Table (name = "tbl_Siswa")
public class Siswa {
    @Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid",strategy = "uuid2")
private String id;
@Column(nullable = false, length = 50)
private String nama;
    @Column (nullable = false, length = 50)
private String kelas;
    @Column (nullable = false, length = 50)
private String jurusan;

public String getId() {
return id;
    }

public String getJurusan() {
returnjurusan;
    }

public void setJurusan(String jurusan) {
this.jurusan = jurusan;
    }

public void setId(String id) {
        this.id = id;
    }

public String getNama() {
returnnama;
    }

public void setNama(String nama) {
this.nama = nama;
    }

public String getKelas() {
returnkelas;
    }

public void setKelas(String kelas) {
this.kelas = kelas;
    }
}

DbAbsensiAplicationTests.java
package com.jaya5.db_absensi;

importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration (classes = DbAbsensiApplication.class)
public class DbAbsensiApplicationTests {

                @Test
                public void contextLoads() {
                }

}

DbAbsensiApplication.java
package com.jaya5.db_absensi;

importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DbAbsensiApplication {

                public static void main(String[] args) {
                                SpringApplication.run(DbAbsensiApplication.class, args);
                }
}

Application Propertis
spring.datasource.url=jdbc:mysql://localhost/db_absensi
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


spring.jpa.generate-ddl=true

Templates.admin
form.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Tambah Data Admin</title>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    <body>
        <h1>Tambah Data Admin</h1>
        <form action="#"
              th:action="@{/admin/form}"
              th:object="${admin}" method="post">
            <table border="1">
                
                <tr>
                    <td>Username</td>
                    <td><input type="text" th:field="*{username}"/></td>
                    <td th:if="${#fields.hasErrors('username')}"
                        th:errors="*{username}"> Pesan Error</td>
                </tr>
                
                <tr>
                    <td>Password</td>
                    <td><input type="text" th:field="*{password}"/></td>
                    <td th:if="${#fields.hasErrors('password')}"
                        th:errors="*{password}"> Pesan Error</td>
                </tr>
                <tr>
                    <td>Aksi</td>
                    <td colspan="2" align="center">
                        <input type="submit" value="simpan"/>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

list.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

    <head>
        <title>Daftar Admin</title>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    <body>
        <center>
            <h1>Daftar Admin</h1>
            <a herf="#" th:href="@{/siswa/coba}">
                Kembali</a>

            <table border="1">
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Password</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="p : ${daftarAdmin}">
                        <td th:text="${p.username}"></td>
                        <td th:text="${p.password}"></td> 

                        <td>
                          
                            <a href="#" th:href="@{/admin/hapus(id=${p.id})}">
                                Hapus</a>

                        </td>
                    </tr>
                </tbody>
            </table>
        </center>

    </body>
</html>

templates.siswa
form.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Tambah Data Siswa</title>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    <body>
        <h1>Tambah Data Siswa</h1>
        <form action="#"
              th:action="@{/siswa/form}"
              th:object="${siswa}" method="post">
            <table border="1">
                
                <tr>
                    <td>Nama</td>
                    <td><input type="text" th:field="*{nama}"/></td>
                    <td th:if="${#fields.hasErrors('nama')}"
                        th:errors="*{nama}"> Pesan Error</td>
                </tr>
                
                <tr>
                    <td>Kelas</td>
                    <td><input type="text" th:field="*{kelas}"/></td>
                    <td th:if="${#fields.hasErrors('kelas')}"
                        th:errors="*{kelas}"> Pesan Error</td>
                </tr>
                <tr>
                    <td>Jurusan</td>
                    <td><input type="text" th:field="*{jurusan}"/></td>
                    <td th:if="${#fields.hasErrors('jurusan')}"
                        th:errors="*{jurusan}"> Pesan Error</td>
                </tr>
                <tr>
                    <td>Aksi</td>
                    <td colspan="2" align="center">
                        <input type="submit" value="simpan"/>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

list.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

    <head>
        <title>Daftar Siswa</title>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    <body>
        <center>
            <h1>Daftar Siswa</h1>
            <a herf="#" th:href="@{/siswa/coba}">
                Kembali</a>

            <table border="1">
                <thead>
                    <tr>
                        <th>Nama</th>
                        <th>Kelas</th>
                        <th>Jurusan</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="p : ${daftarSiswa}">
                        <td th:text="${p.nama}"></td>
                        <td th:text="${p.kelas}"></td> 
                        <td th:text="${p.jurusan}"></td>

                        <td>
                          
                            <a href="#" th:href="@{/siswa/hapus(id=${p.id})}">
                                Hapus</a>

                        </td>
                    </tr>
                </tbody>
            </table>
        </center>

    </body>
</html>

coba.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

    <head>
        <title>Daftar Siswa</title>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    <body>
        <center>
            <h1>Daftar Siswa dan Admin</h1>
            <a herf="#" th:href="@{/siswa/form}">
                Tambah Data Siswa</a>
            <a herf="#" th:href="@{/admin/form}">
                Tambah Data Admin</a>
         
           
        </center>

    </body>
</html>
 

Selanjutnya kita akan demo aplikasi absensi maka ketikan di browser localhost:8080/siswa/coba maka akan menjadi seerti pada gambar di bawah

Ketika kita klik tambah data siswa maka dia akan berpindah tempat di form pengisian data siswa seperti pada fambar di bawah

Jika sudah selesai melakukan proses penginputan data maka klik button simpan dan data tersebut akan di parcing ke list.html seperti pada gambar di bawah

Jika data sudah ada kita kan coba lihat di console kita tapi kalau buka console maka jangan lupa aktifkan dulu xampp control panelnya, inilah data yang di input tadi



Pada gambar di atas datanya sudah ada di console dan saya sudah hapus dan lihatlah di browser sudah terhapus juga seperti pada gambar di bawah

Dan ketika di klik link kembali maka kita akan berpindah pada pada gambar sperti di bawah ini


Selanjutnya kita coba bagian admin apakah sudah berhasil lansung saja kita klik Tambah data admin maka tampilan akan berubah ke form penginputan admin seperti pada gambar di bawah

Jika sudah selesai menginput maka kita klik button simpan maka data akan di parcing di list admin seperti pada gambar di bawah


Jika data sudah ada kita kan coba lihat di console kita tapi kalau buka console dan inilah data yang di input tadi

Pada gambar di atas datanya sudah ada di console dan saya sudah hapus dan lihatlah di browser sudah terhapus juga seperti pada gambar di bawah


Dan ketika di klik link kembali maka kita akan berpindah pada pada gambar sperti di bawah ini

Dan selesai sudah tes demo aplikasi kita trimakasih semoga bermanfaat









Komentar

  1. Harrah's Hotel & Casino - MapyRO
    Find Harrah's Hotel & Casino, Atlantic City (New Jersey), 논산 출장마사지 United 서귀포 출장샵 States. Map, 구리 출장샵 reviews, 원주 출장샵 location, contact, driving 순천 출장안마 directions,

    BalasHapus

Posting Komentar

Postingan populer dari blog ini

Cara Buat Form Login & Register Database MYSQL Dengan Eclipse Adt

Cara Buat Aplikasi Android Biodata

Cara Buat Aplikasi Android From Login & Register