页面跳转和Ajax增删改查传输数据

 常用跳转

<a href="${pageContext.request.contextPath }/logout.action">
<form action="${pageContext.request.contextPath }/list.action">

选择框 

<label for="customerFrom">客户来源</label> 
	<select	class="form-control" id="customerFrom" name="custSource">
		<option value="">--请选择--</option>
			<c:forEach items="${fromType}" var="item">
				<option value="${item.dict_id}"
					<c:if test="${item.dict_id == custSource}">selected</c:if>>
								${item.dict_item_name }
			</option>
		</c:forEach>
	</select>

ajax操作

新增用户

    function addteacher() {
        $.post("${pageContext.request.contextPath}/admin/addTeacher",
        $('#addteacher').serialize(),
            function(data){
            alert(data.msg);
            window.location.reload();
        });
    }
    @ResponseBody
    public ResponseResult addTeacher(Teacher teacher){
        ResponseResult result = null;
        if (service.addTeacher(teacher)){
            result = new ResponseResult("success","新增成功");
        }else {
            result = new ResponseResult("error","新增失败");
        }
        return result;
    }

查询用户

<button type="button" onclick="showediter(${c.id})" >编辑</button>
    function showediter(id){
        $.ajax({
            type:'GET',
            url:"${pageContext.request.contextPath}/admin/getTeacherById",
            data:{"id":id},
            success:function (data) {
                $('#id').val(data.id);
                $('#teachername').val(data.teachername);
                $('#degree').val(data.degree);
                $('#phone').val(data.phone);
                $('#password').val(data.password);
                $("input[name=gender][value="+data.gender+"]").attr("checked",true);
            }
        })
    }
    @RequestMapping("/getTeacherById")
    @ResponseBody
    public Teacher getTeacherById(Long id){
        Teacher teacher = service.getTeacherById(id);
        return teacher;
    }

 更新用户

    function updateTeacher() {
        $.post("${pageContext.request.contextPath}/admin/editerTeacher",
            $('#editerTeacher').serialize(),
            function (data) {
                alert(data.msg);
                window.location.reload();
            }
        )
    }
    @RequestMapping("/editerTeacher")
    @ResponseBody
    public ResponseResult editerTeacher(Teacher teacher){
        ResponseResult result = null;
        if(service.editerTeacher(teacher)){
            result = new ResponseResult("success","修改成功");
        }else {
            result = new ResponseResult("error","修改失败");
        }
        return result;
    }

 删除单条

<button type="button" onclick="deleteTeacher('${c.teachername}',${c.id})" >编辑</button>
    function deleteTeacher(teachername,id) {
        if(confirm('确定要删除用户:'+teachername+"吗?"))
        $.post(
            "${pageContext.request.contextPath}/admin/deleteTeacher",{"id":id},
            function (data) {
                alert(data.msg);
                window.location.reload();
            }
        )
    }
    @RequestMapping("/deleteTeacher")
    @ResponseBody
    public ResponseResult deleteTeacher(Long id){
        ResponseResult result = null;
        if(service.deleteTeacher(id)){
            result = new ResponseResult("success","删除成功");
        }else {
            result = new ResponseResult("error","删除失败");
        }
        return result;
    }

 删除多条

<input type="checkbox" name="ids" value="${c.id}">
    function deleteTeachers() {
        if(confirm('确定删除所选吗?')){
            $.post(
                "${pageContext.request.contextPath}/admin/deleteTeachers",
                $('#formids').serialize(),
                function (data) {
                    alert(data.msg);
                    window.location.reload();
                }
            )
        }
    }
    @RequestMapping("/deleteTeachers")
    @ResponseBody
    public ResponseResult deleteTeachers(String[] ids){
        ResponseResult result = null;
        if(service.deleteTeachers(ids)){
            result = new ResponseResult("success","删除成功");
        }else {
            result = new ResponseResult("error","删除失败");
        }
        return result;
    }

猜你喜欢

转载自blog.csdn.net/Milan__Kundera/article/details/82819638