SSM项目 Jquery实现From表单提交 json后台并接受

2019-6-21 山西太原工作 前台整理
文章总结
主要是前后台的交互代码,浪费了两个小时才弄好,注意 前台往后台返回的时候 使用的并非json格式传入后台 , 后台使用@ModelAttribute 可以实现自动的封装,装填对象
@ModelAttribute在参数前面加的作用 从model中取出key为user的User对象,model没有User会自动创建,并把前台表单的请求参数赋给这个User对象的对应属性。

from表单在后台一般对应的是一个实体类所以使用@ModelAttribute注解,自己装配,还会上传一些其他的参数,实体类中没有,这时可以使用@RequestParam参数获取

前台Html

<form>
 <input type="text" id="name" name="name" maxlength="10" placeholder="请填写您的姓名">
  <input type="text" id="telphone" name="telphone" maxlength="11" placeholder="请填写您的手机号">
   <button type="button" onclick="myfun_submit()“>确认提交 </button>
</form>

前台Js

‘比方说 格式化from表单之后 还要提交其他参数,可以采用下面的方式
// var data = "pageNo="+no+"&"+ $("form").serialize();
           
function myfun_submit() {
        $.ajax({
            type : "post",
            dataType : "json",
            url : "/bookContact/joinContactSave",
            contentType:"application/x-www-form-urlencoded",
            data:   $("form").serialize(),
            success:function (data) {

                if(data.status == 1){
                    $(".tanchuang").show();
                }
            },//响应成功后的回调函数

        });
    }

后台接受

@Controller
@RequestMapping("/bookContact")
public class BookContactController  {
public void joinContactSave(Model model,
								  @ModelAttribute  BookContactMember bookContactMember,
                                 @RequestParam(required = false, value = "contactItem", defaultValue = "") String contactItem ,HttpServletRequest request,HttpServletResponse response) {
System.out.println(bookContactMember);
}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值