Null question to Springboot Controller

Asked 2 years ago, Updated 2 years ago, 90 views

There are other tables, Member and Vote. It's registered in DB, but it's registered as shown in the log below, and when I looked up what the problem was, I found out that there was a null in the controller. I'm writing this question because I'm studying but it's not being solved.

Controller

    @RequestMapping(value = "member/insert", method = RequestMethod.GET) 
    public String memberInsertForm() {
        return "member/memberInsert";
    }

    @RequestMapping(value = "member/insert", method = RequestMethod.POST)
    public @ResponseBody int memberInsert( Map<String, Object> dto) throws Exception{

        HashMap<String, Object> map = new HashMap<>();
        MemberDTO dto2 = new MemberDTO();
        int reVal = 0;
        reVal = memberService.memberInsert(dto);
        System.out.println("name : >>>>> " + dto2.getName());
        if (reVal > 0) {
            map.put("result", "success");
            memberService.voteInsert(map);
        } } else {
            map.put("result", "failed");
        }
        return reVal;
    }

Mapper.xml

<insert id="memberInsert" parameterType="Map">
    INSERT INTO MEMBER (
              id
            , , name
            , , regdate
    ) VALUES (
            (SELECT NVL(MAX(id),0)+1 FROM MEMBER)
            , , #{name}
            , , sysdate
    )
    </insert>
    <insert id="voteInsert" parameterType="Map">
    INSERT INTO VOTE (
              vid
            , , vTitle
            , , vContent
            , , regdate
    ) VALUES (
            (SELECT NVL(MAX(vid),0)+1 FROM VOTE)
            , , #{vTitle}
            , , #{vContent}
            , , sysdate
    )
    </insert>

Mapper.java

    public int memberInsert(Map<String, Object> map) throws Exception;

    public int voteInsert(Map<String, Object> map) throws Exception;

Service.java

    public int memberInsert(Map<String, Object> map) throws Exception {
        return mapper.memberInsert(map);
    }

    public int voteInsert(Map<String, Object> map) throws Exception {
        return mapper.voteInsert(map);
    }

insert

function insertSubmit(){

    if(!confirm("Do you want to submit it?")){return;}

    if(!$("#name").val()){
        alert("Please enter a name");
        return;
    }
    if(!$("#vTitle").val()){
        alert("Please enter a voting title");
        return;
    }
        $.ajax({
            type: "POST", 
            url : insertVote,
            dataType : 'json',
            contentType : "application/json;charset=UTF-8",
            cache : false,
            async: false,
            data: $("#form1").serialize(),
            success : function(result) {
                alert("entry into successful
                if(result.result == 'success'){
                    alert ("Submission completed");
                    location.href="/member/list";
                } } else {
                    alert("Submission failed")
                }
        }
    });
}

Log

c.v.b.mapper.MemberMapper.memberInsert   : ==>  Preparing: INSERT INTO MEMBER ( id , name , regdate ) VALUES ( (SELECT NVL(MAX(id),0)+1 FROM MEMBER) , ? , sysdate ) 
DEBUG 7336 --- [p-nio-80-exec-1] c.v.b.mapper.MemberMapper.memberInsert   : ==> Parameters: null
DEBUG 7336 --- [p-nio-80-exec-1] c.v.b.mapper.MemberMapper.memberInsert   : <==    Updates: 1
name : >>>>> null
DEBUG 7336 --- [p-nio-80-exec-1] c.v.boot.mapper.MemberMapper.voteInsert  : ==>  Preparing: INSERT INTO VOTE ( vid , vTitle , vContent , regdate ) VALUES ( (SELECT NVL(MAX(vid),0)+1 FROM VOTE) , ? , ? , sysdate ) 
DEBUG 7336 --- [p-nio-80-exec-1] c.v.boot.mapper.MemberMapper.voteInsert  : ==> Parameters: null, null
DEBUG 7336 --- [p-nio-80-exec-1] c.v.boot.mapper.MemberMapper.voteInsert  : <==    Updates: 1

spring spring-boot ajax

2022-09-21 16:39

1 Answers

@RequestBodyMap<String, Object> dto must be declared to receive the value.

If it doesn't work out, I think you need to do more debugging.

$("#form1") before sending ajax.Is the serialize() value properly similar? Probably console.log ($("#form1")serialize()); print it out.

If the values look exactly the same here, then the controller will You can check if the value is similar to Mapto.


2022-09-21 16:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.