发表于: 2018-04-01 22:23:19

1 496


今天完成的事情:

完成ssm框架的增删改查,以及分页

首先看一下项目目录

ssm框架的配置文件基本的配置大同小异,主要看看jsp页面和controller的交互过程。

最终效果如下:


controller如下

@Controller
public class CategoryController {
@Autowired
   private CategoryService categoryService;
   /**
    * 分页查询用户信息
    * @param pn 默认从第一页开始  请求参数
    */
   @RequestMapping("/listCategory")
public String listCategory(@RequestParam(value = "pn",defaultValue = "1")Integer pn,Model model) {
PageHelper.startPage(pn, 5);
       List<Category> list = categoryService.list();
       PageInfo<Category> pageInfo = new PageInfo(list, 5);
       model.addAttribute("pageInfo", pageInfo);
       return "listCategory";
   }
/**
    * 跳转到添加用户界面
    */
   @RequestMapping("/toAddCategory")
public String toAdd() {
return "addCategory";
   }
/**
    * 添加用户并重定向
    */
   @RequestMapping("/addCategory")
public String addCategory(Model model, Category category) {
if (category != null) {
categoryService.add(category);
       }
return "redirect:/listCategory";
   }
/**
    * 修改用户
    */
   @RequestMapping("/updateCategory")
public String updateCategory(Model model, Category category) {
if (categoryService.update(category) == 1) {
return "redirect:/listCategory";
       }
return "/error";
   }
/**
    * 查询单个用户
    */
   @RequestMapping("/getById")
public String getById(int id, Model model) {
model.addAttribute("category", categoryService.getById(id));
       return "editCategory";
   
/**
    * 删除用户
    */
   @RequestMapping("deleteCategory")
public String deleteCategory(int id, Model model) {
model.addAttribute("category", categoryService.delete(id));
       return "redirect:listCategory";
   }

}

1,分页查询所有用户

listCategory.jsp

<c:set var="path" value="${pageContext.request.contextPath}"/>
<div style="width:500px;margin:0px auto;text-align:center">
   <div class="row">
       <div class="col-md-4 col-md-offset-8">
           <a class="btn btn-primary" href="${path}/toAddCategory">新增</a>
       </div>
   </div>
   <%--分页--%>
<table align='center' border='1' cellspacing='0'>
         <tr>
             <td>id</td>
             <td>name</td>
             <td colspan="2">操作</td>
         </tr>
       <c:forEach items="${pageInfo.list}" var="c" >
           <tr>
               <td>${c.id}</td>
               <td>${c.name}</td>
               <td><a href="${path}/getById?id=${c.id}">编辑</a></td>
               <td><a href="${path}/deleteCategory?id=${c.id}">删除</a></td>
           </tr>
       </c:forEach>
</table>
   <div style="text-align:center">
           <a href="${path}/listCategory?pn=1">首页</a>
           <c:if test="${pageInfo.pageNum>1}">
              <a href="${path}/listCategory?pn=${pageInfo.pageNum-1}">上一页</a>
           </c:if>
           <c:if test="${pageInfo.pageNum<=1}">
              <a href="#">上一页</a>
           </c:if>
           <c:if test="${pageInfo.pageNum<pageInfo.pages}">
              <a href="${path}/listCategory?pn=${pageInfo.pageNum+1}">下一页</a>
           </c:if>
           <c:if test="${pageInfo.pageNum>=pageInfo.pages}">
              <a href="#">下一页</a>
           </c:if>
           <a href="${path}/listCategory?pn=${pageInfo.pages}">末页</a>
   </div>
</div>

这里的

其中Model 是一个接口, 其实现类为ExtendedModelMap,继承了ModelMap类。 modelmap对象主要用于传递controller处理的数据到jsp页面,也就是说我们把页面上的数据放在modelmap里面供我们调用。具体用法如下:addAttribute(String key,Object value);

在页面中我们可以通过el表达式${key}来获取并调用modelmap里面的数据。

这里实现分页是通过插件是实现的,并且内部封装的pageInfo对象有一系列的属性和方法我们可以直接调用。

我这里实现上一页

用pageInfo.pageNum来获取当前页数,并做判断,若当前页大于1则令pageNum-1,

若当前页小于等于1则链接当前页href="#"。实现下一页的方法相似。

其中pageInfo.pages返回的是总页数,即跳到最后一页。


2,增加数据  addCategory.jsp

<form action="${path}/addCategory" method="post">
   <tabel>
       <tr>
           <label>类别:</label> <input type="text" id="name" name="name" tabindex="1"/>
       </tr>
       <tr>
           <input id="reset" type="reset" tabindex="2" value="取消"/>
           <input id="submit"  type="submit" tabindex="3" value="添加"/>
       </tr>
   </tabel>
</form>

当在所有用户页面中,点击新增

根据请求/toAddCategory会被控制器

拦截到,并进行跳转到addCategory页面。

在添加页面提交表单后,控制器

会拦截到

并重定向到查询所有用户页面,完成新增步骤。

3,编辑用户editCategory.jsp

<form action="${path}/updateCategory" method="post">
   <table>
       <tr>
           <input type="hidden" id="id" name="id" value="${category.id}"/>
           类别:<input type="text" id="name" name="name" value="${category.name}" tabindex="1"/><br>
       </tr>
       <tr>
           <input type="reset" id="reset"   tabindex="2" value="取消"/>
           <input type="submit" id="submit"  tabindex="3" value="提交"/>
       </tr>
   </table>
</form>

在用户页面点击编辑

,控制器

会拦截到其请求/getById获得一个用户,并跳转到editCategory页面进行编辑。

编辑完成后提交表单

给控制器修改用户,完成后重定向到所有用户界面。

4,删除用户

当点击删除时

控制器拦截其请求

然后根据用户ID来进行删除,删除完成后,利用重定向来重新查询所有用户显示。


明天计划的事情:

完成restful接口

遇到的问题:

暂无

收获:

了解了增删改查的运行过程。

进度:

任务二步骤二

任务开始时间:3.23

预计demo时间:4.3

是否延期:否

禅道地址:http://task.ptteng.com/zentao/project-task-562.html



返回列表 返回列表
评论

    分享到