Skip to content

Instantly share code, notes, and snippets.

@dimMaryanto93
Last active October 11, 2016 23:22
Show Gist options
  • Save dimMaryanto93/546c0ed6a01e0c986bda2a4610ae23cd to your computer and use it in GitHub Desktop.
Save dimMaryanto93/546c0ed6a01e0c986bda2a4610ae23cd to your computer and use it in GitHub Desktop.
Spring Web MVC
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="f" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tambah Mahasiwa</title>
</head>
<body>
<h3>Tambah data Mahasiswa</h3>
<f:form commandName="m" action="proses" method="post">
<div>
<f:label path="nim">NIM: </f:label>
<f:input path="nim" maxlength="8" size="10" pattern="\d+"/>
</div>
<div>
<f:label path="nama">Nama: </f:label>
<f:input path="nama" maxlength="25" size="30"/>
</div>
<div>
<f:label path="noTlp">No Telp: </f:label>
<f:input path="noTlp" maxlength="15" size="20" />
</div>
<div>
<f:label path="alamat">Alamat: </f:label>
<f:textarea path="alamat" cols="35" rows="5"/>
</div>
<div>
<f:label path="kelamin">Jenis Kelamin: </f:label>
<f:radiobutton path="kelamin" value="L" label="Laki - Laki"/>
<f:radiobutton path="kelamin" value="P" label="Perempuan"/>
</div>
<div>
<input type="submit" value="kirim"/>
</div>
</f:form>
</body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="f" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tambah Mahasiwa</title>
</head>
<body>
<h3>Tambah data Mahasiswa</h3>
<f:form commandName="m" action="proses" method="post">
<div>
<f:label path="nim">NIM: </f:label>
<f:input path="nim" maxlength="8" size="10" pattern="\d+"/>
</div>
<div>
<f:label path="nama">Nama: </f:label>
<f:input path="nama" maxlength="25" size="30"/>
</div>
<div>
<f:label path="noTlp">No Telp: </f:label>
<f:input path="noTlp" maxlength="15" size="20" />
</div>
<div>
<f:label path="alamat">Alamat: </f:label>
<f:textarea path="alamat" cols="35" rows="5"/>
</div>
<div>
<f:label path="kelamin">Jenis Kelamin: </f:label>
<f:radiobutton path="kelamin" value="L" label="Laki - Laki"/>
<f:radiobutton path="kelamin" value="P" label="Perempuan"/>
</div>
<div>
<f:select path="daftarMatakuliah" multiple="true">
<f:option value="Matematika" label="Matematika"/>
<f:option value="Komputer" label="Komputer"/>
<f:option value="Fisika" label="Fisika"/>
<f:option value="Kimia" label="Kimia"/>
</f:select>
</div>
<div>
<input type="submit" value="kirim"/>
</div>
</f:form>
</body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h3>${pesan}</h3>
</body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="st" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<link rel="stylesheet" href="<st:url value="/style/halocss.css"/>" />
<link rel="stylesheet" href="<st:url value="/webjars/bootstrap/3.3.7/css/bootstrap.min.css"/>">
</head>
<body class="container">
<h3 class="halo">${pesan}</h3>
<script type="text/javascript" src="<st:url value="/webjars/jquery/1.11.1/jquery.min.js"/>"></script>
<script type="text/javascript" src="<st:url value="/webjars/bootstrap/3.3.7/js/bootstrap.min.js"/>"></script>
</body>
</html>
package com.hotmail.dimmaryanto.software.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HaloController {
@RequestMapping("/halo")
public void halo(Model m){
m.addAttribute("pesan", "Halo ini dari Spring Web MVC");
}
}
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Info Mahasiswa</title>
</head>
<body>
<h1>Informasi Mahasiswa</h1>
<table>
<tbody>
<tr>
<td>NIM</td>
<td>${m.nim}</td>
</tr>
<tr>
<td>Nama</td>
<td>${m.nama}</td>
</tr>
<tr>
<td>Alamat</td>
<td>${m.alamat}</td>
</tr>
<tr>
<td>No Telp</td>
<td>${m.noTlp}</td>
</tr>
<tr>
<td>Jenis Kelamin</td>
<td>${m.kelamin}</td>
</tr>
</tbody>
</table>
</body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Info Mahasiswa</title>
</head>
<body>
<h1>Informasi Mahasiswa</h1>
<table>
<tbody>
<tr>
<td>NIM</td>
<td>${m.nim}</td>
</tr>
<tr>
<td>Nama</td>
<td>${m.nama}</td>
</tr>
<tr>
<td>Alamat</td>
<td>${m.alamat}</td>
</tr>
<tr>
<td>No Telp</td>
<td>${m.noTlp}</td>
</tr>
<tr>
<td>Jenis Kelamin</td>
<td>${m.kelamin}</td>
</tr>
<tr>
<td>Matakuliah</td>
<td>
<ul>
<c:forEach items="${m.daftarMatakuliah}" var="k">
<li>${k}</li>
</c:forEach>
</ul>
</td>
</tr>
</tbody>
</table>
</body>
</html>
package com.hotmail.dimmaryanto.software;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.hotmail.dimmaryanto.software")
public class KonfigurasiWeb extends WebMvcConfigurerAdapter {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/pages/", ".jsp");
}
}
package com.hotmail.dimmaryanto.software;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.hotmail.dimmaryanto.software")
public class KonfigurasiWeb extends WebMvcConfigurerAdapter {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/pages/", ".jsp");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/style/**").addResourceLocations("/WEB-INF/style/");
}
}
package com.hotmail.dimmaryanto.software;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.hotmail.dimmaryanto.software")
public class KonfigurasiWeb extends WebMvcConfigurerAdapter {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/pages/", ".jsp");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/style/**").addResourceLocations("/WEB-INF/style/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>List of Product</title>
</head>
<body>
<h3>Daftar Product</h3>
<br>
<table border="1">
<thead>
<tr>
<th>No</th>
<th>Nama Produk</th>
<th>Harga</th>
<th>Stok</th>
</tr>
</thead>
<tbody>
<c:forEach items="${listProducts}" var="p" varStatus="i">
<tr>
<td>${i.count}</td>
<td>${p.nama}</td>
<td>${p.hargaJual}</td>
<td>${p.stok}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<h3>Daftar Product</h3>
<br>
<table class="table">
<thead>
<tr>
<th>No</th>
<th>Nama Produk</th>
<th>Harga</th>
<th>Stok</th>
</tr>
</thead>
<tbody>
<c:forEach items="${listProducts}" var="p" varStatus="i">
<tr>
<td>${i.count}</td>
<td>${p.nama}</td>
<td>${p.hargaJual}</td>
<td>${p.stok}</td>
</tr>
</c:forEach>
</tbody>
</table>
package com.hotmail.dimmaryanto.software.entity;
public class Mahasiswa {
private String nim;
private String nama;
private String alamat;
private String noTlp;
private String kelamin;
public String getKelamin() {
return kelamin;
}
public void setKelamin(String kelamin) {
this.kelamin = kelamin;
}
public String getNim() {
return nim;
}
public void setNim(String nim) {
this.nim = nim;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getAlamat() {
return alamat;
}
public void setAlamat(String alamat) {
this.alamat = alamat;
}
public String getNoTlp() {
return noTlp;
}
public void setNoTlp(String noTlp) {
this.noTlp = noTlp;
}
}
package com.hotmail.dimmaryanto.software.controller;
import com.hotmail.dimmaryanto.software.entity.Mahasiswa;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/student")
public class MahasiswaController {
@GetMapping("/add")
public void tampilForm(Model m) {
m.addAttribute("m", new Mahasiswa());
}
@PostMapping("/proses")
public String prosesForm(@ModelAttribute Mahasiswa mhs, Model m) {
m.addAttribute("m", mhs);
return "student/info";
}
}
package com.hotmail.dimmaryanto.software.entity;
import java.util.ArrayList;
import java.util.List;
public class Mahasiswa {
private String nim;
private String nama;
private String alamat;
private String noTlp;
private String kelamin;
private List<String> daftarMatakuliah = new ArrayList<>();
public List<String> getDaftarMatakuliah() {
return daftarMatakuliah;
}
public void setDaftarMatakuliah(List<String> daftarMatakuliah) {
this.daftarMatakuliah = daftarMatakuliah;
}
public String getKelamin() {
return kelamin;
}
public void setKelamin(String kelamin) {
this.kelamin = kelamin;
}
public String getNim() {
return nim;
}
public void setNim(String nim) {
this.nim = nim;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getAlamat() {
return alamat;
}
public void setAlamat(String alamat) {
this.alamat = alamat;
}
public String getNoTlp() {
return noTlp;
}
public void setNoTlp(String noTlp) {
this.noTlp = noTlp;
}
}
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="st" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tutorial Spring WEB MVC</title>
<link rel="stylesheet" href="<st:url value="/style/halocss.css"/>" />
<link rel="stylesheet" href="<st:url value="/webjars/bootstrap/3.3.7/css/bootstrap.min.css"/>">
</head>
<body class="container">
<%-- nantinya akan di isi oleh content sesuai dengan request mapping --%>
<sitemesh:write property="body"></sitemesh:write>
<script type="text/javascript" src="<st:url value="/webjars/jquery/1.11.1/jquery.min.js"/>"></script>
<script type="text/javascript" src="<st:url value="/webjars/bootstrap/3.3.7/js/bootstrap.min.js"/>"></script>
</body>
</html>
<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.hotmail.dimmaryanto.software</groupId>
<artifactId>tutorial.spring-webmvc</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>Tutorial Spring Framework WEB MVC</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>spring-webmvc</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<target>1.7</target>
<source>1.7</source>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<!-- web.xml is not mandatory since JavaEE 5 -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.hotmail.dimmaryanto.software.entity;
import java.math.BigDecimal;
public class Product {
private String nama;
private BigDecimal hargaJual;
private Integer stok;
public Product(String nama, BigDecimal hargaJual, Integer stok) {
this.nama = nama;
this.hargaJual = hargaJual;
this.stok = stok;
}
public Product() {
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public BigDecimal getHargaJual() {
return hargaJual;
}
public void setHargaJual(BigDecimal hargaJual) {
this.hargaJual = hargaJual;
}
public Integer getStok() {
return stok;
}
public void setStok(Integer stok) {
this.stok = stok;
}
}
package com.hotmail.dimmaryanto.software.controller;
import com.hotmail.dimmaryanto.software.entity.Product;
import java.math.BigDecimal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/api/product")
public class ProductAPI {
@GetMapping("/indomie")
@ResponseBody
public Product indomie() {
return new Product("Indomie ayam bawang", new BigDecimal(5000), 3);
}
}
package com.hotmail.dimmaryanto.software.controller;
import com.hotmail.dimmaryanto.software.entity.Product;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/product")
public class ProductController {
private static List<Product> daftarProduct = new ArrayList<>();
static {
daftarProduct.add(new Product("Sabun Cuci", new BigDecimal(10000), 10));
daftarProduct.add(new Product("Lemari", new BigDecimal(1200000), 5));
daftarProduct.add(new Product("Ember", new BigDecimal(5000), 3));
}
@GetMapping("/list")
public void listProduct(Model m) {
m.addAttribute("listProducts", daftarProduct);
}
}
package com.hotmail.dimmaryanto.software;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ServletInit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{KonfigurasiWeb.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
package com.hotmail.dimmaryanto.software;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ServletInit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{KonfigurasiWeb.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
package com.hotmail.dimmaryanto.software;
import javax.servlet.annotation.WebFilter;
import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;
@WebFilter(servletNames = "SitemeshFilterDecorator", urlPatterns = {"/*"})
public class SitemeshFilterDecorator extends ConfigurableSiteMeshFilter {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.addDecoratorPath("/", "/WEB-INF/template/main.jsp");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment