## What we will do: - Display Todos in a table using JSTL Tags - <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> - Add Dependency for jstl ``` javax.servlet jstl 1.2 ``` ## Files List ### /pom.xml ``` 4.0.0 com.in28minutes in28Minutes-springmvc 0.0.1-SNAPSHOT war javax javaee-web-api 6.0 provided org.springframework spring-webmvc 4.2.2.RELEASE javax.servlet jstl 1.2 log4j log4j 1.2.17 org.apache.maven.plugins maven-compiler-plugin 3.2 true 1.8 1.8 true org.apache.tomcat.maven tomcat7-maven-plugin 2.2 / true ``` ### /src/main/java/com/in28minutes/login/LoginController.java ``` package com.in28minutes.login; 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.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.SessionAttributes; @Controller @SessionAttributes("name") public class LoginController { @Autowired private LoginService loginService; @RequestMapping(value = "/login", method = RequestMethod.GET) public String showLoginPage() { return "login"; } @RequestMapping(value = "/login", method = RequestMethod.POST) public String handleUserLogin(ModelMap model, @RequestParam String name, @RequestParam String password) { if (!loginService.validateUser(name, password)) { model.put("errorMessage", "Invalid Credentials"); return "login"; } model.put("name", name); return "welcome"; } } ``` ### /src/main/java/com/in28minutes/login/LoginService.java ``` package com.in28minutes.login; import org.springframework.stereotype.Service; @Service public class LoginService { public boolean validateUser(String user, String password) { return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy"); } } ``` ### /src/main/java/com/in28minutes/model/Todo.java ``` package com.in28minutes.model; import java.util.Date; public class Todo { private int id; private String user; private String desc; private Date targetDate; private boolean isDone; public Todo(int id, String user, String desc, Date targetDate, boolean isDone) { super(); this.id = id; this.user = user; this.desc = desc; this.targetDate = targetDate; this.isDone = isDone; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public Date getTargetDate() { return targetDate; } public void setTargetDate(Date targetDate) { this.targetDate = targetDate; } public boolean isDone() { return isDone; } public void setDone(boolean isDone) { this.isDone = isDone; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Todo other = (Todo) obj; if (id != other.id) return false; return true; } @Override public String toString() { return String.format( "Todo [id=%s, user=%s, desc=%s, targetDate=%s, isDone=%s]", id, user, desc, targetDate, isDone); } } ``` ### /src/main/java/com/in28minutes/todo/service/TodoService.java ``` package com.in28minutes.todo.service; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import org.springframework.stereotype.Service; import com.in28minutes.model.Todo; @Service public class TodoService { private static List todos = new ArrayList(); private static int todoCount = 3; static { todos.add(new Todo(1, "in28Minutes", "Learn Spring MVC", new Date(), false)); todos.add(new Todo(2, "in28Minutes", "Learn Struts", new Date(), false)); todos.add(new Todo(3, "in28Minutes", "Learn Hibernate", new Date(), false)); } public List retrieveTodos(String user) { List filteredTodos = new ArrayList(); for (Todo todo : todos) { if (todo.getUser().equals(user)) filteredTodos.add(todo); } return filteredTodos; } public void addTodo(String name, String desc, Date targetDate, boolean isDone) { todos.add(new Todo(++todoCount, name, desc, targetDate, isDone)); } public void deleteTodo(int id) { Iterator iterator = todos.iterator(); while (iterator.hasNext()) { Todo todo = iterator.next(); if (todo.getId() == id) { iterator.remove(); } } } } ``` ### /src/main/java/com/in28minutes/todo/TodoController.java ``` package com.in28minutes.todo; import java.util.Date; 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.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.SessionAttributes; import com.in28minutes.todo.service.TodoService; @Controller @SessionAttributes("name") public class TodoController { @Autowired private TodoService service; @RequestMapping(value = "/list-todos", method = RequestMethod.GET) public String showTodosList(ModelMap model) { String user = (String) model.get("name"); model.addAttribute("todos", service.retrieveTodos(user)); return "list-todos"; } @RequestMapping(value = "/add-todo", method = RequestMethod.GET) public String showTodoPage() { return "todo"; } @RequestMapping(value = "/add-todo", method = RequestMethod.POST) public String addTodo(ModelMap model, @RequestParam String desc) { service.addTodo((String) model.get("name"), desc, new Date(), false); model.clear();// to prevent request parameter "name" to be passed return "redirect:/list-todos"; } } ``` ### /src/main/resources/log4j.properties ``` log4j.rootLogger=TRACE, Appender1, Appender2 log4j.appender.Appender1=org.apache.log4j.ConsoleAppender log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n ``` ### /src/main/webapp/WEB-INF/todo-servlet.xml ``` /WEB-INF/views/ .jsp ``` ### /src/main/webapp/WEB-INF/views/list-todos.jsp ``` <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> Todos for ${name}

Your Todos

Your Todos are
Description Date Completed
${todo.desc} ${todo.targetDate} ${todo.done}
Add ``` ### /src/main/webapp/WEB-INF/views/login.jsp ``` Login Page

${errorMessage}

Name : Password :
``` ### /src/main/webapp/WEB-INF/views/todo.jsp ``` Login Page
Description :
``` ### /src/main/webapp/WEB-INF/views/welcome.jsp ``` Yahoo!! Welcome ${name}. You are now authenticated. Click here to start maintaining your todo's. ``` ### /src/main/webapp/WEB-INF/web.xml ``` To do List dispatcher org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/todo-servlet.xml 1 dispatcher / ```