Spring MVC中Bootstrap与Java分页实现详解

0 下载量 156 浏览量 更新于2024-08-28 收藏 104KB PDF 举报
本文是一篇关于在Spring MVC项目中集成Bootstrap进行分页实践的教程,作者在尝试应用前一篇文章介绍的分页方法时遇到了问题,因为网上的示例往往缺乏完整的细节。经过多次探索和调试,作者最终成功实现了分页功能,并决定分享这个经验,以便其他开发者参考。 在文章中,作者首先强调了理解分页的基本逻辑至关重要,但关键在于如何将理论应用到实际项目中,以及如何优化实现以提高效率。作者特别提到了他们采用了Spring MVC框架,这在处理后端数据分发和前端展示时非常常见,有助于简化工作流程。 核心部分是关于`xml-pager.tld`配置文件的编写,这是使用JSP标签库来实现分页的关键步骤。这个配置文件定义了一个名为`createPager`的标签,该标签接受几个参数:`curPage`(当前页数)、`totalPage`(总页数)和`pageSize`(每页显示的记录数)。这些参数都是必需的,并允许表达式值,以便动态处理页面请求。 `<tag-class>getui.util.Pager</tag-class>`指定用于创建分页控件的类,这可能是一个自定义的工具类或者来自第三方库的分页组件。`<body-content>JSP</body-content>`表明这个标签在JSP页面中可以直接使用,无需额外的Java代码。 作者遇到的问题可能是缺少具体的代码片段或者对某些依赖的理解,比如如何将用户请求转换为这些参数,以及如何调用`createPager`标签来渲染分页导航。解决这些问题可能涉及到整合Spring MVC的`@ModelAttribute`、`@PathVariable`或`@RequestParam`注解,以及与视图层的交互。 通过这篇文章,读者可以学习到如何在Spring MVC环境中利用Bootstrap实现分页,理解配置文件的编写以及与前端模板的结合,同时也可以从中吸取如何解决实际开发中遇到的技术难题的经验。最后,作者提到还有进一步思考的空间,这意味着可能存在多种实现方式和优化手段,鼓励读者根据自己的项目需求进行深入研究和探索。
2014-05-27 上传
package com.org.controller; import java.io.PrintWriter; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.ModelAndView; import com.org.BaseController; import com.org.model.User; import com.org.service.IUserService; /** * @Author:liangjilong * @Date:2014-2-25 * @Version:1.0 * @Description: */ @Controller public class UserController extends BaseController { @Resource private IUserService userService; /*** * 方法一请求使用String * * 请求@RequestMapping匹配的URL request * * @param response * @return * @throws Exception */ @RequestMapping(value = "/userList1.do") public String userList1(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); List<User> lists = userService.getUserList(); if (lists != null) { request.setAttribute("userList", lists); } // userList指跳转到userList.jsp页面 return "userList"; } /** * 方法二请求使用ModelAndView * * @param request * @param response * @return * @throws Exception */ @RequestMapping(value = "/userList2.do") public ModelAndView userList2(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); List<User> lists = userService.getUserList(); if (lists != null) { request.setAttribute("userList", lists); } // userList指跳转到userList.jsp页面 return new ModelAndView("userList"); } /*** * 自定义标签实现分页 * * @param request * @param response * @param @ResponseBody ajax响应 * @param method={RequestMethod.POST,RequestMethod.GET}表单请求 * @param consumes="application/json; charset=UTF-8"请求格式是json * @return * @throws UnsupportedEncodingException * @throws Exception */ @RequestMapping(value = "/pageList.do" ,method={RequestMethod.POST,RequestMethod.GET}) public @ResponseBody ModelAndView getUserInfo(Model model, @RequestParam(required = false) String username, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize) { try { String userName = new String(username.getBytes("ISO-8859-1"),"UTF-8");//处理乱码 Map<String, Object> map = new HashMap<String, Object>(); username=(username==null)?"":username; map.put("username", username);//username必须要和ibatis配置的property=username一致 Integer totalCount = this.userService.getUserCount(map); this.initPage(map, pageNum, pageSize, totalCount); List list = this.userService.getUserLists(map); this.initResult(model, list, map); return new ModelAndView("pagerList"); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 添加用户 * @param type * @param model * @return */ @RequestMapping("/addUser.do") public ModelAndView addUser(@RequestParam String username, Model model) { User user = new User(); user.setUsername(username); this.userService.addUser(user); return this.getUserInfo(model, null, null, null); } /*** * 删除用户 * @param id * @param pageNum * @param pageSize * @param model * @return */ @RequestMapping(value="/delUser.do",method={RequestMethod.POST,RequestMethod.GET},consumes="application/json; charset=UTF-8") @ResponseBody public ModelAndView delUser(@RequestParam(required = true) Integer id, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, Model model, HttpServletRequest request,HttpServletResponse response) { PrintWriter out=null; JSONObject result=new JSONObject(); try { out=response.getWriter(); this.userService.delUserById(id); result.put("flag", true); out.write(result.toString()); } catch (Exception e) { try { result.put("flag", false); out.write(result.toString()); } catch (JSONException e1) { e1.printStackTrace(); } } return null; //return this.getUserInfo(model, null, pageNum, pageSize); } /*** * 编辑用户 * @param id * @param model * @return */ @RequestMapping("/getUserById.do") public ModelAndView getUserById(@RequestParam(required = true) Integer id, Model model) { User u = this.userService.getUserById(id); model.addAttribute("user", u); return new ModelAndView("editUser"); } /*** * 编辑用户 * @param id * @param model * @return */ @RequestMapping("/editUser.do") public ModelAndView editUser(@RequestParam(required = true) Integer id, @RequestParam String username, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, Model model) { User u = new User(); u.setUsername(username); this.userService.editUser(u); return this.getUserInfo(model, null, pageNum, pageNum); } }