Simple CRUD Spring MVC and Thymeleaf

In the previous post, I showed you how to integrating Spring MVC and Thymeleaf. In this chapter I will show you step by step to create Simple CRUD using Spring MVC and Thymeleaf

Our Goal is :
thymeleaf_list
thymeleaf_add

Create Domain Class Faq.java

package com.hidupbersahaja.domain;

public class Faq {

	private Long id;
	private String name;
	private String description;
	private boolean publish;
	
	public Faq(){}

	public Long getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isPublish() {
		return publish;
	}

	public void setPublish(boolean publish) {
		this.publish = publish;
	}
	
	
	@Override
	public boolean equals(Object o) {
		if (null == o)
			return false;
		if (!(o instanceof Faq))
			return false;
		Faq oo = (Faq) o;
		if (this.getId() == null) {
			return oo.getId() == null;
		} else
			return this.getId().equals(oo.getId());
	}
}

Service Layer FaqService.java and FaqServiceImpl.java

FaqService.java

package com.hidupbersahaja.service;

import java.util.List;

import com.hidupbersahaja.domain.Faq;

public interface FaqService {
	
	public List<Faq> findAllFAQ();
	public void save(Faq faq);
	public Faq findByPrimaryKey(Long id);
}

FaqServiceImpl.java

package com.hidupbersahaja.service;

import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.hidupbersahaja.domain.Faq;

@Service
public class FaqServiceImpl implements FaqService{
	
	private Set<Faq> list = new LinkedHashSet<Faq>();
	
	public List<Faq> findAllFAQ(){
		List<Faq> faqs = new LinkedList<Faq>();
		for(Faq f : list){
			faqs.add(f);
		}
		return faqs;
	}
	
	public void save(Faq faq){
		Long id = 1L;
		if(faq.getId() == null && list.size() > 0) {
			id = list.size() + 1L;
			
		} 
		
		faq.setId(id);
		list.add(faq);
	}
	
	public Faq findByPrimaryKey(Long id){
		for(Faq faq : list){
			if(faq.getId().equals(id)) {
				return faq;
			}
		}
		return null;
	}
	

}

Controller side FAQController.java

package com.hidupbersahaja.controller;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hidupbersahaja.data.util.DataConstants;
import com.hidupbersahaja.domain.Faq;
import com.hidupbersahaja.service.FaqService;

@Controller
@RequestMapping("/faq")
public class FAQController {
	
	@Autowired
	private FaqService faqService;

	/**
	 * default handler showing faq widget page
	 * 
	 * @return faq.jsp
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String defaultHandler(Model model) {
		try {
			model.addAttribute("faqs", faqService.findAllFAQ());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "faq/faq_list";
	}
	
	/**
	 * Add FAQ
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String doAdd(Model model){
		try {
			model.addAttribute("faq", new Faq());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "faq/faq_add";
	}
	
	/**
	 * Edit FAQ
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String doEdit(@PathVariable Long id, Model model) {
		Faq faq = faqService.findByPrimaryKey(id);
		model.addAttribute("faq", faq);
		return "faq/faq_add";
	}
	
	/**
	 * Save FAQ
	 * @param faq
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String doSave(@ModelAttribute Faq faq, Model model) {
		faqService.save(faq);
		return "redirect:/faq";
	}

	
}

create faq_list.html and faq_add.html inside /WEB-INF/templates/faq directory

faq_list.html

<!doctype html>
<html lang="en">
<head>
</head>
<body>
	<h2>Frequently Asked Question</h2>
	<a href="/learn/faq/add">Add FAQ</a>
	<table>
		<thead>
			<tr>
				<th>Id</th>
				<th>Name</th>
				<th>Description</th>
				<th>Published</th>
				<th></th>
			</tr>
		</thead>
		<tbody>
			<tr th:each="faq : ${faqs}">
			    <td th:text="${faq.id}"></td>
			    <td th:text="${faq.name}"></td>
			    <td th:text="${faq.description}"></td>
			    <td th:text="${faq.publish}"></td>
			    <td>
			    	<a  th:href="@{'/faq/edit/'+${faq.id}}"><span>Edit</span></a>
			    </td>
			</tr>
		</tbody>
	</table>
</body>
</html>

faq_add.html

<!doctype html>
<html lang="en">
<head>
</head>
<body>
<h2>Add FAQ</h2>
<form action="#" th:action="@{/faq/save}" th:object="${faq}" method="post">
<input type="hidden" th:field="*{id}" />
<table>
	<tr>
		<td>Name</td>
		<td> : </td>
		<td><input type="text" th:field="*{name}" /></td>
	</tr>
	<tr>
		<td>Description</td>
		<td> : </td>
		<td><input type="text" th:field="*{description}" /></td>
	</tr>
	<tr>
		<td>is Publish ?</td>
		<td> : </td>
		<td><input type="checkbox" th:field="*{publish}"/></td>
	</tr>
	<tr>
		<td colspan="3" align="center">
			<button type="submit" value="Submit">Submit</button>	
		</td>
	</tr>
</table>
</form>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>