首页>代码>JPA使用视图进行查询、使用Adobe Acrobat准备表单生成PDF>/JpaFormPdf/src/main/java/com/ldg/test/controller/MainController.java
package com.ldg.test.controller; import com.ldg.test.modal.User; import com.ldg.test.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller public class MainController { @Autowired private IUserService service; @RequestMapping("/user") public String viewIndexPage(Model model) { String keyword = null; return listByPage(model, 1, "id", "asc", keyword); } @GetMapping("/page/{pageNumber}") public String listByPage(Model model, @PathVariable("pageNumber") int currentPage, @Param("sortField") String sortField, @Param("sortDir") String sortDir, @Param("keyword") String keyword) { Page<User> page = service.listAll(currentPage, sortField, sortDir, keyword); long totalItems = page.getTotalElements(); int totalPages = page.getTotalPages(); // int currentPage = page.previousPageable().getPageNumber(); List<User> listUsers = page.getContent(); model.addAttribute("totalItems", totalItems); model.addAttribute("totalPages", totalPages); model.addAttribute("currentPage", currentPage); model.addAttribute("listUsers", listUsers); // next bc of thymeleaf we make the user.html model.addAttribute("sortField", sortField); model.addAttribute("sortDir", sortDir); model.addAttribute("keyword", keyword); String reverseSortDir = sortDir.equals("asc") ? "desc" : "asc"; model.addAttribute("reverseSortDir", reverseSortDir); return "user"; } @RequestMapping("/new") public String showNewUserForm(Model model) { User user = new User(); model.addAttribute("user", user); return "new_user"; } @RequestMapping(value = "/save", method = RequestMethod.POST) public String saveUser(@ModelAttribute("user") User user) { service.save(user); return "redirect:/"; } @RequestMapping("/edit/{id}") public ModelAndView showEditUserForm(@PathVariable(name = "id") Long id) { ModelAndView modelAndView = new ModelAndView("edit_user"); User user = service.get(id); modelAndView.addObject("user", user); return modelAndView; } @RequestMapping("/delete/{id}") public String deleteUser(@PathVariable(name = "id") Long id) { service.delete(id); return "redirect:/"; } }
最近浏览更多
3993zby LV2
11月11日
Peny_ZH LV5
9月21日
liiiyou LV1
6月2日
kenhomeliu LV29
4月30日
youwuzuichen LV10
4月17日
woldxy LV12
4月1日
wanglinddad LV55
3月13日
田DJ sozzled
3月2日
暂无贡献等级
1134116035
2月28日
暂无贡献等级
houxianzheng
2月19日
暂无贡献等级