Last active
November 18, 2015 04:21
-
-
Save jumperchen/be9aa6d622d4072dc0bb to your computer and use it in GitHub Desktop.
ZK Spring MVC Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<zk xmlns:ca="client/attribute"> | |
<style> | |
input:checked + .z-checkbox-content { | |
text-decoration: line-through; | |
color: grey; | |
} | |
</style> | |
<vlayout> | |
<listbox id="list" model="${todoList}" ca:data-springmvc-action="edit" | |
ca:data-springmvc-trigger="onSelect"> | |
<listhead> | |
<listheader label="Message"/> | |
</listhead> | |
<template name="model"> | |
<listitem> | |
<listcell> | |
<checkbox label="${each.message}" checked="${each.done}" value="${each}" ca:data-springmvc-action="finish" ca:data-springmvc-trigger="onCheck"/> | |
</listcell> | |
</listitem> | |
</template> | |
</listbox> | |
<div> | |
<label id="status" value="Add:"/> | |
<grid id="form"> | |
<columns> | |
<column label="Message"/> | |
</columns> | |
<rows> | |
<row> | |
<textbox id="message" cols="40"/> | |
</row> | |
<row> | |
<button id="submit" label="Add new Todo" | |
ca:data-springmvc-action="add"/> | |
</row> | |
</rows> | |
</grid> | |
</div> | |
</vlayout> | |
</zk> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Todo { | |
private String message; | |
private boolean done; | |
public Todo() {} | |
public Todo(String message) { | |
this.message = message; | |
} | |
public boolean isDone() { | |
return done; | |
} | |
public void setDone(boolean done) { | |
this.done = done; | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@RequestMapping("/mvc/todos") | |
@SessionAttributes("todoList") | |
public class TodoController { | |
@RequestMapping(value = "/", method = RequestMethod.GET) | |
public String index(ModelMap model) { | |
ListModelList<Todo> todoList = new ListModelList<Todo>(); | |
todoList.add(new Todo("Wakeup early")); | |
todoList.add(new Todo("Booking a restaurant")); | |
todoList.add(new Todo("Visit Jobs' office")); | |
model.addAttribute(todoList); | |
return "forward:list"; | |
} | |
@RequestMapping(value = "/finish", method = RequestMethod.POST) | |
public String finish(@ZKVariable("self.value") Todo todo) { | |
todo.setDone(!todo.isDone()); | |
return ZKModelAndView.SELF; | |
} | |
@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST}) | |
public String list() { | |
return "mvc/list.zul"; | |
} | |
@RequestMapping(value = "/add", method = RequestMethod.POST) | |
public String add( | |
@ModelAttribute("todoList") ListModelList<Todo> todoList, | |
@ZKSelector("#message") String message) { | |
todoList.add(new Todo(message)); | |
return "forward:list"; | |
} | |
@RequestMapping(value = "/edit", method = RequestMethod.POST) | |
public String edit( | |
@ModelAttribute("todoList") ListModelList<Todo> todoList, | |
@ZKSelector("#status") Label status, | |
@ZKSelector("#message") Textbox message, | |
@ZKSelector("#submit") Button submit) { | |
Todo editTodo = todoList.getSelection().iterator().next(); | |
message.setValue(editTodo.getMessage()); | |
status.setValue("Edit:"); | |
submit.setLabel("Update"); | |
submit.setClientDataAttribute("springmvc-action", "update"); | |
return ZKModelAndView.SELF; | |
} | |
@RequestMapping(value = "/update", method = RequestMethod.POST) | |
public String update( | |
@ModelAttribute("todoList") ListModelList<Todo> todoList, | |
@ZKSelector("#status") Label status, | |
@ZKSelector("#message") String message, | |
@ZKSelector("#submit") Button submit) { | |
Todo editTodo = todoList.getSelection().iterator().next(); | |
editTodo.setMessage(message); | |
status.setValue("Add:"); | |
submit.setLabel("Add new Todo"); | |
submit.setClientDataAttribute("springmvc-action", "add"); | |
//save to model | |
todoList.update(editTodo); | |
return "forward:list"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment