码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc CRUD控制器写法和jsp页面写法 总结大全。

时间:2020-01-15 09:59:51      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:previous   rom   BMI   delattr   author   primary   integer   prim   date   

本例介绍控制器和jsp页面的写法,分两部分。

本文中,jsp页面,一共有3个,浏览页面,增加页面,修改页面。

控制器一共有6个。浏览getAuthors,增加页面跳转authorAdd,增加业务authorSave,修改页面跳转authorModify,修改业务authorUpdate,删除业务authorDel。

一、控制器

1,入参。

其中控制器中,需要带id传值的,有2个,一个删除业务,一个是修改页面跳转。这里两个同样需要路径参数注解。

带bean传值的,有3个,一个是增加页面跳转,一个是增加业务,一个是修改业务。

带BindingResult,error返回值的,有2个,一个是增加业务。一个是修改业务。

带Valid校验bean的,也是2个,一个是增加业务。一个是修改业务。

带路径参数注解 @PathVariable的,有2个。一个是删除业务,一个是修改页面跳转。

带请求参数注解 @RequestParam 的,有1个,浏览所有getAuthors。且该请求参数后需要跟着实参。

代码如下:

@Controller
public class ZhouyiAuthorController {
    

    @Autowired
    private ZhouyiAuthorService zhouyiAuthorService;
    
    @RequestMapping("/getAuthors")
    public ModelAndView getAuhoros(@RequestParam(value="pn",defaultValue = "1")Integer pn) {
        PageHelper.startPage(pn, 12);
        ModelAndView mv = new ModelAndView("zhouyiAuthor");
        List<ZhouyiAuthor> zhouyiContentAuthors = zhouyiAuthorService.getZhouyiContentAuthors();
        PageInfo page = new PageInfo(zhouyiContentAuthors,7);
        mv.addObject("pageinfo", page);
        return mv;
    }
    
    @RequestMapping("/zhouyiAuthorDel/{id}")
    public ModelAndView zhouyiAuthorDel(@PathVariable("id") Integer id) {
        ModelAndView mv = new ModelAndView();
        List<ZhouyiAuthor> zhouyiAuthorsFromContents = zhouyiAuthorService.getZhouyiAuthorsFromContents(id);
        if(zhouyiAuthorsFromContents.size()>0) {
            mv.addObject("errors", "*该作者下有子类目,不允许删除!");
            mv.setViewName("forward:/getAuthors");
            return mv;
        }
        int deleteZhouyiContentAuthor = zhouyiAuthorService.deleteZhouyiContentAuthor(id);
        if(deleteZhouyiContentAuthor!=1) {
            mv.addObject("errors", "*删除异常!");
            mv.setViewName("forward:/getAuthors");
            return mv;
        }
        
        mv.setViewName("redirect:/getAuthors");
        return mv;
    }
    
    @RequestMapping("/zhouyiAuthorAdd")
    public ModelAndView zhouyiAuthorAdd(ZhouyiAuthor zhouyiAuthor){
        ModelAndView mv = new ModelAndView("zhouyiAuthorAdd");
        return mv;
    }
    
    @RequestMapping("/zhouyiAuthorSave")
    public ModelAndView zhouyiAuthorSave(@Valid ZhouyiAuthor zhouyiAuthor,BindingResult errors) {
        ModelAndView mv = new ModelAndView();
        if(errors.getFieldErrorCount()>0) {
            for (FieldError err : errors.getFieldErrors()) {
            System.out.println("err.field:= "+err.getField()+ "  | message:==="+ err.getDefaultMessage());
            }
            mv.setViewName("zhouyiAuthorAdd");
            return mv;
        }
        zhouyiAuthorService.addZhouyiContentAuthor(zhouyiAuthor);
        mv.setViewName("forward:/getAuthors");
        return mv;
    }
    
    @RequestMapping("/zhouyiAuthorModify/{id}")
    public ModelAndView zhouyiAuthorModify(@PathVariable("id") Integer id) {
        ModelAndView mv = new ModelAndView("zhouyiAuthorModify");
        mv.addObject("zhouyiAuthor",zhouyiAuthorService.getZhouyiContentAuthorById(id) );
        return mv;
    }
    
    @RequestMapping("/zhouyiAuthorUpdate")
    public ModelAndView zhouyiAuthorUpdate(@Valid ZhouyiAuthor zhouyiAuthor,BindingResult errors) {
        ModelAndView mv = new ModelAndView();
        if(zhouyiAuthor.getOrderId()==null) {
            errors.addError(new FieldError("zhouyiAuthor", "orderId", "不能为空"));
        }
        if(errors.getErrorCount()>0) {
            mv.setViewName("zhouyiAuthorModify");
            return mv;
        }
        zhouyiAuthorService.modifyZhouyiContentAuthor(zhouyiAuthor);
        mv.setViewName("redirect:/getAuthors");
        return mv;
    
    }
    
}

 

2.jsp页面。

有3个,如果能用springmvc标签写的,就用springmvc标签写,不要用html普通标签写,因为有一些附件功能,比如path在修改页面时,若有列表可以自动检查当前值并且选中。

 头如下:

<title>Yiwiki - 作者</title>
<% pageContext.setAttribute("APP_PATH", request.getContextPath()); %>
<script  src="${APP_PATH}/static/js/jquery-1.12.4.min.js"></script>
<link href="${APP_PATH}/static/bootstrap-3.3.7-dist/css/bootstrap-theme.css" rel="stylesheet">
<link href="${APP_PATH}/static/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script  src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.js" ></script>

</head>
<body>

头文件包括如下:

<body>
<jsp:include page="/header.jsp"></jsp:include>
    <ol class="breadcrumb">
        <li><a href="#">Home</a></li>
        <li><a href="#">作者分类</a></li>
        <li class="active">修改</li>
    </ol>
//下面为主体部分
<div class="container">

 

 浏览页面如下:

<div class="container">
        <span style="margin-left:3px;color:#ce4844;font-weight:bold">${errors }</span>
        
        <a href="${APP_PATH }/zhouyiAuthorAdd"><button type="button" class="btn btn-primary "
            style="margin-bottom: 8px; float: right">新增</button></a>
        <table class="table table-striped table-hover">
            <tr>
                <td>ID</td>
                <td>排序</td>
                <td>作者名称</td>
                <td>说明</td>
                <td>操作</td>
            </tr>

            <c:forEach items="${pageinfo.list }" var="zhouyiAuthor">
                <tr>
                    <td>${zhouyiAuthor.id }</td>
                    <td>${zhouyiAuthor.orderId }</td>
                    
                    <td>${zhouyiAuthor.name }</td>
                    <td>${zhouyiAuthor.info }</td>
                    <td><a href="${APP_PATH }/zhouyiAuthorModify/${zhouyiAuthor.id}"><button type="button" class="btn btn-primary btn-xs btn_edit" edit_id="${zhouyiAuthor.id }">修改</button></a>
                        <a href="${APP_PATH }/zhouyiAuthorDel/${zhouyiAuthor.id}"><button type="button" class="btn btn-primary btn-xs" onclick="return confirm(‘确定要删除吗?‘)">删除</button></a></td>
                </tr>
            </c:forEach>
        </table>
        共${pageinfo.total }条记录,共${pageinfo.pages }页
        <nav style="float: right" aria-label="Page navigation">
            <ul class="pagination">
                <li><a
                    href="${APP_PATH }/getAuthors?pn=1"
                    aria-label="Previous"> <span aria-hidden="true">&laquo;</span>
                </a></li>

                <c:forEach items="${pageinfo.navigatepageNums }" var="nums">
                    <c:if test="${pageinfo.pageNum == nums }">
                        <li class="active"><a
                            href="${APP_PATH }/getAuthors?pn=${nums}">${nums }</a></li>
                    </c:if>
                    <c:if test="${pageinfo.pageNum != nums }">
                        <li><a
                            href="${APP_PATH }/getAuthors?pn=${nums}">${nums }</a></li>
                    </c:if>
                </c:forEach>

                <li><a
                    href="${APP_PATH }/getAuthors?pn=${pageinfo.pages}"
                    aria-label="Next"> <span aria-hidden="true">&raquo;</span>
                </a></li>
            </ul>
        </nav>

    

    </div>

增加跳转页面如下:

<div class="container">
<form:form  modelAttribute="zhouyiAuthor" class="form-horizontal" id="saveForm"  >

  <div class="form-group">
 
    <label for="name" class="col-sm-2 control-label">作者</label>
    <div class="col-sm-6">
     <form:input type="text" path="name" class="form-control" id="name" placeholder="姓名" /><form:errors path="name"></form:errors>
    </div>
  </div>
  
<div class="form-group">
    <label for="info" class="col-sm-2 control-label">说明</label>
    <div class="col-sm-6">
      <form:input type="text"  path="info" class="form-control" id="info" placeholder="备注"/><form:errors path="info"></form:errors>
    </div>
  </div>
   
    <div class="form-group">
    <label for="" class="col-sm-2 control-label"></label>
    <div class="col-sm-6" >
      <button type="submit" id="saveIndex" class="btn btn-primary" formenctype="multipart/form-data"   formaction="${APP_PATH }/zhouyiAuthorSave" formmethod="post"  >保存</button>
    </div>
  </div>

   </form:form>
      
    </div>

修改页面如下:

<div class="container">
<form:form  modelAttribute="zhouyiAuthor" class="form-horizontal" id="saveForm"  >

  <div class="form-group">
 <form:hidden path="id"/>
    <label for="name" class="col-sm-2 control-label">作者</label>
    <div class="col-sm-6">
     <form:input type="text" path="name" class="form-control" id="name" placeholder="姓名" /><form:errors path="name"></form:errors>
    </div>
  </div>
  
<div class="form-group">
    <label for="info" class="col-sm-2 control-label">说明</label>
    <div class="col-sm-6">
      <form:input type="text"  path="info" class="form-control" id="info" placeholder="备注"/><form:errors path="info"></form:errors>
    </div>
  </div>
   <div class="form-group">
    <label for="orderId" class="col-sm-2 control-label">排序</label>
    <div class="col-sm-2">
      <form:input type="text"  path="orderId" class="form-control" id="orderId" placeholder="排序"/><form:errors path="orderId"></form:errors>
    </div>
  </div>
    <div class="form-group">
    <label for="" class="col-sm-2 control-label"></label>
    <div class="col-sm-6" >
      <button type="submit" id="saveIndex" class="btn btn-primary" formenctype="multipart/form-data"   formaction="${APP_PATH }/zhouyiAuthorUpdate" formmethod="post"  >保存</button>
    </div>
  </div>
  </form:form>
    </div>

 

springmvc CRUD控制器写法和jsp页面写法 总结大全。

标签:previous   rom   BMI   delattr   author   primary   integer   prim   date   

原文地址:https://www.cnblogs.com/sdgtxuyong/p/12194880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!