json传值问题!!!!!!

使用spring spring MVC mybits 框架。前台用ajax获取表单内容,通过json传到controller,调用service 插到数据库,为什么没有值,数据库里插进去的是key,我检查了好几次,求大神解答

8个回答

简化下代码,既然使用框架了就利用好框架。还有前端的一下校验可以交给jquery的validator,为了安全后端应该加一些数据的校验

在各个层次中断了看json数组的值,确定在哪个层次出的问题;

package com.neusoft.resume.studylive.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.neusoft.resume.page.Pageinfo;
import com.neusoft.resume.resume.pojo.Resume;
import com.neusoft.resume.resume.service.IResumeService;
import com.neusoft.resume.studylive.pojo.StudyLive;
import com.neusoft.resume.studylive.service.IStudyLiveService;
import com.neusoft.resume.userinfo.pojo.UserInfo;
@Controller
@RequestMapping("studylive")
public class StudyLiveController {
@Resource
private IStudyLiveService studyliveservice;
@Resource
private IResumeService resumeservice;
@RequestMapping(value="add/save",produces="text/html;charset=UTF-8")
@ResponseBody
public String addSave(
HttpServletRequest request,
@RequestParam(value="studyBeginTime",required=true)String studyBeginTime,
@RequestParam(value="studyEndTime",required=true)String studyEndTime,
@RequestParam(value="studyContext",required=true)String studyContext,
@RequestParam(value="studyAddress",required=true)String studyAddress,
@RequestParam(value="studyLetter",required=true)String studyLetter
){
JSONObject objs=new JSONObject();
try{
Object obj=request.getSession().getAttribute("userinfo");
if(obj==null){
return "redirect:/demo4/login.jsp";
}
UserInfo userinfo=(UserInfo)obj;
StudyLive studylive=new StudyLive();
studylive.setStudyBeginTime(studyBeginTime);
studylive.setStudyEndTime(studyEndTime);
studylive.setStudyLetter(studyLetter);
studylive.setStudyContext(studyContext);
studylive.setStudyAddress(studyAddress);
studylive.setResumeid(resumeservice.getUserResume(userinfo.getId()).getId());
studyliveservice.add(studylive);
objs.put("error_num", 0);
objs.put("error_msg", "ok");
}catch(Exception ex){
objs.put("error_num", 1000);
objs.put("error_msg", ex.getMessage());
}
return objs.toJSONString();

}

前台
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>




学习经历添加页





<%@include file="../nav.jsp"%>





添加学习经历





开始时间 :
                     <div class="form-group has-success">
                            结束时间 : <input class="form-control" name="studyEndTime">
                     </div>
                     <div class="form-group has-success">
                            内容: <input class="form-control" name="studyContext">
                     </div>
                     <div class="form-group has-success">
                                                    地址:<input class="form-control"  name="studyAddress">
                     </div>
                   <div class="form-group has-success">
                                                    获得证书:<input class="form-control"  name="studyLetter">
                     </div>

                      <div class="panel-footer">
                        <a class="btn btn-outline btn-primary" >确认添加</a>
                      </div>    
                   </fieldset>          
                 </form>
               </div>   
          </div>
        </div>

    </div>

  </div>
$(function(){ $(".btn").click(function(){ var studyBeginTime=$("input[name=studyBeginTime]").val(); var studyEndTime=$("input[name=studyEndTime]").val(); var studyContext=$("input[name=studyContext]").val(); var studyAddress=$("input[name=studyAddress]").val(); var studyLetter=$("input[name=studyLetter]").val(); if(studyBeginTime.length==0){ alert("开始日期不能为空"); return false; }else{ $("#username").html(""); } if(studyEndTime.length==0){ alert("结束日期不能为空"); return false; }else{ $("#username").html(""); } if(studyContext.length==0){ alert("内容不能为空"); return false; }else{ $("#username").html(""); } var sendData={ studyBeginTime:"studyBeginTime", studyEndTime:"studyEndTime", studyContext:"studyContext", studyAddress:"studyAddress", studyLetter:"studyLetter" }; alert(studyBeginTime); $.ajax({ url:"/demo4/studylive/add/save", type:"post", data:sendData, dataType:"json", success:function(msg){ if (msg.error_num==0) { alert("添加成功!"); location.href="/demo4/studylive/index" } else { alert("添加失败,"+msg.error_msg); } }, error:function(a,b,c){ alert(c); } }) return false; }) })


 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学习经历添加页</title>
    <link href="/demo4/css/bootstrap.min.css" rel="stylesheet">
    <link href="/demo4/font-awesome/css/font-awesome.css" rel="stylesheet">
    <link href="/demo4/css/sb-admin.css" rel="stylesheet">
</head>
<body>
    <%@include file="../nav.jsp"%>
    <div id="page-wrapper">
       <div class="row">
          <div class="col-lg-12">
              <div class="panel panel-primary">
                 <div class="panel-heading">
                                                              添加学习经历
                 </div>
                   <div class="panel-body">
                     <form role="form">
                       <fieldset>
                         <div class="form-group has-success">
                                 开始时间 :<input class="form-control"  name="studyBeginTime">
                         </div>

                         <div class="form-group has-success">
                                结束时间 : <input class="form-control" name="studyEndTime">
                         </div>
                         <div class="form-group has-success">
                                内容: <input class="form-control" name="studyContext">
                         </div>
                         <div class="form-group has-success">
                                                        地址:<input class="form-control"  name="studyAddress">
                         </div>
                       <div class="form-group has-success">
                                                        获得证书:<input class="form-control"  name="studyLetter">
                         </div>

                          <div class="panel-footer">
                            <a class="btn btn-outline btn-primary" >确认添加</a>
                          </div>    
                       </fieldset>          
                     </form>
                   </div>   
              </div>
            </div>

        </div>

      </div>
<script src="/demo4/js/jquery-1.10.2.js"></script>
<script src="/demo4/js/bootstrap.min.js"></script>
<script src="/demo4/js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="/demo4/js/sb-admin.js"></script>
<script type="text/javascript">
    $(function(){
        $(".btn").click(function(){
            var studyBeginTime=$("input[name=studyBeginTime]").val();
            var studyEndTime=$("input[name=studyEndTime]").val();
            var studyContext=$("input[name=studyContext]").val();
            var studyAddress=$("input[name=studyAddress]").val();
            var studyLetter=$("input[name=studyLetter]").val();
            if(studyBeginTime.length==0){
                alert("开始日期不能为空");
                return false;
            }else{
                $("#username").html("");
            }
            if(studyEndTime.length==0){
                alert("结束日期不能为空");
                return false;
            }else{
                $("#username").html("");
            }
            if(studyContext.length==0){
                alert("内容不能为空");
                return false;
            }else{
                $("#username").html("");
            }

            var sendData={
                    studyBeginTime:"studyBeginTime",
                    studyEndTime:"studyEndTime",
                    studyContext:"studyContext",
                    studyAddress:"studyAddress",
                    studyLetter:"studyLetter"
            };
            alert(studyBeginTime);
            $.ajax({
                url:"/demo4/studylive/add/save",
                type:"post",
                data:sendData,
                dataType:"json",
                success:function(msg){
                    if (msg.error_num==0) {
                        alert("添加成功!");
                        location.href="/demo4/studylive/index"
                    } else {
                        alert("添加失败,"+msg.error_msg);
                    }
                },
                error:function(a,b,c){
                    alert(c);
                }

            })


            return false;
        })





    })


</script>
</body>
</html>

添加一个括号,我也遇到过这样的坑

plazyj
navyzhengyj 在哪?
大约 3 年之前 回复

已自行解决,在前台页面中,key与value写反了,后台代码无任何问题

你写的真复杂,
springMvc前台可以封装 实体的属性 后台自动映射到该实体对应的属性中,

只要你把这个属性的name属性给对就行 不存在这么多的代码问题 也没人看的

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问