spring mvc使用更新,url更有restful风格
This commit is contained in:
+2
-6
@@ -34,17 +34,13 @@ public class ProfileController {
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST)
|
||||
public String update(@Valid @ModelAttribute("user") User user) {
|
||||
public String update(@Valid @ModelAttribute("preloadUser") User user) {
|
||||
accountService.updateUser(user);
|
||||
updateCurrentUserName(user.getName());
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
|
||||
* 因为仅update()方法的form中有id属性,因此本方法在该方法中执行.
|
||||
*/
|
||||
@ModelAttribute("user")
|
||||
@ModelAttribute("preloadUser")
|
||||
public User getUser(@RequestParam(value = "id", required = false) Long id) {
|
||||
if (id != null) {
|
||||
return accountService.getUser(id);
|
||||
|
||||
+8
-7
@@ -28,7 +28,7 @@ public class UserAdminController {
|
||||
@Autowired
|
||||
private AccountService accountService;
|
||||
|
||||
@RequestMapping(value = "")
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public String list(Model model) {
|
||||
List<User> users = accountService.getAllUser();
|
||||
model.addAttribute("users", users);
|
||||
@@ -42,25 +42,26 @@ public class UserAdminController {
|
||||
return "account/adminUserForm";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "update/{userId}", method = RequestMethod.POST)
|
||||
public String update(@Valid @ModelAttribute("user") User user, RedirectAttributes redirectAttributes) {
|
||||
@RequestMapping(value = "update", method = RequestMethod.POST)
|
||||
public String update(@Valid @ModelAttribute("preloadUser") User user, RedirectAttributes redirectAttributes) {
|
||||
accountService.updateUser(user);
|
||||
redirectAttributes.addFlashAttribute("message", "更新用户成功");
|
||||
redirectAttributes.addFlashAttribute("message", "更新用户" + user.getLoginName() + "成功");
|
||||
return "redirect:/admin/user";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "delete/{id}")
|
||||
public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
|
||||
User user = accountService.getUser(id);
|
||||
accountService.deleteUser(id);
|
||||
redirectAttributes.addFlashAttribute("message", "删除用户成功");
|
||||
redirectAttributes.addFlashAttribute("message", "删除用户" + user.getLoginName() + "成功");
|
||||
return "redirect:/admin/user";
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
|
||||
* 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
|
||||
* 因为仅update()方法的form中有id属性,因此本方法在该方法中执行.
|
||||
*/
|
||||
@ModelAttribute("user")
|
||||
@ModelAttribute("preloadUser")
|
||||
public User getUser(@RequestParam(value = "id", required = false) Long id) {
|
||||
if (id != null) {
|
||||
return accountService.getUser(id);
|
||||
|
||||
+28
-12
@@ -9,8 +9,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
import org.springside.examples.quickstart.entity.Task;
|
||||
@@ -23,12 +25,12 @@ import com.google.common.collect.Maps;
|
||||
/**
|
||||
* Task管理的Controller, 使用Restful风格的Urls:
|
||||
*
|
||||
* List page : GET /task/
|
||||
* Create page : GET /task/create
|
||||
* Create action : POST /task/save
|
||||
* Update page : GET /task/update/{id}
|
||||
* Update action : POST /task/save/{id}
|
||||
* Delete action : POST /task/delete/{id}
|
||||
* List page : GET /task/
|
||||
* Create page : GET /task/create
|
||||
* Create action : POST /task/create
|
||||
* Update page : GET /task/update/{id}
|
||||
* Update action : POST /task/update
|
||||
* Delete action : GET /task/delete/{id}
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
@@ -47,7 +49,7 @@ public class TaskController {
|
||||
sortTypes.put("title", "标题");
|
||||
}
|
||||
|
||||
@RequestMapping(value = "")
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public String list(@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
|
||||
@RequestParam(value = "page", defaultValue = "1") int pageNumber, Model model) {
|
||||
Long userId = getCurrentUserId();
|
||||
@@ -60,13 +62,14 @@ public class TaskController {
|
||||
return "task/taskList";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "create")
|
||||
@RequestMapping(value = "create", method = RequestMethod.GET)
|
||||
public String createForm(Model model) {
|
||||
model.addAttribute("task", new Task());
|
||||
model.addAttribute("action", "create");
|
||||
return "task/taskForm";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "save")
|
||||
@RequestMapping(value = "create", method = RequestMethod.POST)
|
||||
public String create(@Valid Task newTask, RedirectAttributes redirectAttributes) {
|
||||
User user = new User(getCurrentUserId());
|
||||
newTask.setUser(user);
|
||||
@@ -76,14 +79,15 @@ public class TaskController {
|
||||
return "redirect:/task/";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "update/{id}")
|
||||
@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
|
||||
public String updateForm(@PathVariable("id") Long id, Model model) {
|
||||
model.addAttribute("task", taskService.getTask(id));
|
||||
model.addAttribute("action", "update");
|
||||
return "task/taskForm";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "save/{id}")
|
||||
public String update(@Valid Task task, RedirectAttributes redirectAttributes) {
|
||||
@RequestMapping(value = "update", method = RequestMethod.POST)
|
||||
public String update(@Valid @ModelAttribute("preloadTask") Task task, RedirectAttributes redirectAttributes) {
|
||||
taskService.saveTask(task);
|
||||
redirectAttributes.addFlashAttribute("message", "更新任务成功");
|
||||
return "redirect:/task/";
|
||||
@@ -96,6 +100,18 @@ public class TaskController {
|
||||
return "redirect:/task/";
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
|
||||
* 因为仅update()方法的form中有id属性,因此本方法在该方法中执行.
|
||||
*/
|
||||
@ModelAttribute("preloadTask")
|
||||
public Task getTask(@RequestParam(value = "id", required = false) Long id) {
|
||||
if (id != null) {
|
||||
return taskService.getTask(id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取出Shiro中的当前用户Id.
|
||||
*/
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<form id="inputForm" action="${ctx}/admin/user/update/${user.id}" method="post" class="form-horizontal">
|
||||
<form id="inputForm" action="${ctx}/admin/user/update" method="post" class="form-horizontal">
|
||||
<input type="hidden" name="id" value="${user.id}"/>
|
||||
<fieldset>
|
||||
<legend><small>用户管理</small></legend>
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<form id="inputForm" action="${ctx}/task/save/${task.id}" method="post" class="form-horizontal">
|
||||
<form id="inputForm" action="${ctx}/task/${action}" method="post" class="form-horizontal">
|
||||
<input type="hidden" name="id" value="${task.id}"/>
|
||||
<fieldset>
|
||||
<legend><small>管理任务</small></legend>
|
||||
|
||||
+5
-4
@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.InitBinder;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
@@ -50,7 +51,7 @@ public class UserController {
|
||||
}
|
||||
|
||||
@RequiresRoles("Admin")
|
||||
@RequestMapping(value = "update/{id}")
|
||||
@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
|
||||
public String updateForm(@PathVariable("id") Long id, Model model) {
|
||||
model.addAttribute("user", accountService.getUser(id));
|
||||
model.addAttribute("allStatus", allStatus);
|
||||
@@ -62,8 +63,8 @@ public class UserController {
|
||||
* 演示自行绑定表单中的checkBox roleList到对象中.
|
||||
*/
|
||||
@RequiresPermissions("user:edit")
|
||||
@RequestMapping(value = "save/{userId}")
|
||||
public String update(@Valid @ModelAttribute("user") User user,
|
||||
@RequestMapping(value = "update", method = RequestMethod.POST)
|
||||
public String update(@Valid @ModelAttribute("preloadUser") User user,
|
||||
@RequestParam(value = "roleList") List<Long> checkedRoleList, RedirectAttributes redirectAttributes) {
|
||||
|
||||
//bind roleList
|
||||
@@ -96,7 +97,7 @@ public class UserController {
|
||||
* 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
|
||||
* 因为仅update()方法的form中有id属性,因此本方法在该方法中执行.
|
||||
*/
|
||||
@ModelAttribute("user")
|
||||
@ModelAttribute("preloadUser")
|
||||
public User getUser(@RequestParam(value = "id", required = false) Long id) {
|
||||
if (id != null) {
|
||||
return accountService.getUser(id);
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||
<body>
|
||||
<h1>综合演示用例</h1>
|
||||
<form:form id="inputForm" modelAttribute="user" action="${ctx}/account/user/save/${user.id}" method="post" class="form-horizontal">
|
||||
<form:form id="inputForm" modelAttribute="user" action="${ctx}/account/user/update" method="post" class="form-horizontal">
|
||||
<input type="hidden" name="id" value="${user.id}"/>
|
||||
<fieldset>
|
||||
<legend><small>管理用户</small></legend>
|
||||
|
||||
Reference in New Issue
Block a user