继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

spring框架下ajax的url地址如何定位到spring对应的类

假装obj
关注TA
已关注
手记 6
粉丝 0
获赞 44

通过html页面的点击事件执行ajax方法,定位到springboot的对应java类的对应方法中;

HTML代码部分

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <script th:substituteby="merchant/fragments/head::header"/>

    <!-- 组件 -->
    <link rel="stylesheet"
          href="https://a.alipayobjects.com/g/antui/antui/10.0.12/rem/??widget/dialog.css"/>

</head>
<body ontouchstart="">

<div class="am-flexbox" style="text-align:center">
    <div class="am-flexbox-item">
        欢迎使用XXXXX平台
        <br/>
        请输入以下信息完成验证
    </div>
</div>

<form class="am-list am-list-form">
    <div class="am-list-body">
        <div th:substituteby="merchant/fragments/head::input('alipayAccount','XXX账号','请输入','text',null,false)"/>
        <div th:substituteby="merchant/fragments/head::input('principalPerson','XXX姓名','请输入','text',null,false)"/>
        <div th:substituteby="merchant/fragments/head::input('principalMobile','XXX手机','请输入','text',null,false)"/>
        <div th:substituteby="merchant/fragments/head::input('certNo','XXX身份证号','请输入','text',null,false)"/>
    </div>
    <button type="button" class="am-button" **onclick="confirmID()"** style="background-color:rgba(63, 164, 237, 1);position:fixed;bottom: 0px;width: 100%;">确认</button>

    <script >
        function confirmID(){
            ap.showLoading({
                content: '执行中'
            });
            var form=$('.am-list-form').serializeArray();
            console.log(form);
            AJAX({
                type:'POST',
                url:'/rest/merchant/validate.json',
                data:form,
                dataType:'json',
                success: function(data){
                    if (data.status != 'OK') {
                        alert("验证失败,请检查输入项");
                        ap.hideLoading();
                        return;
                    }
                    $("#dialog [name='content']").html('验证成功');
                    ap.hideLoading();
                    window.location.href='/merchant/signStepOne';
                },
                error: function(e){
                    ap.hideLoading();
                    console.log(e);
                    $("#dialog [name='content']").html(e.responseJSON.message);
                    $('#dialog').show();
                }
            });
        }
    </script>
</form>
</body>
</html>

后台springboot框架的代码部分;


package com.controller;

@RestController//标注此类是一个控制器类
@RequestMapping("/rest/merchant")//RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径
public class MerchantH5RestController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(MerchantH5RestController.class);

    @Value("${qrcode.storeManage}")//在spring 3.0中,可以通过使用@value,对一些如xxx.properties文件中的文件,进行键值对的注入
    private String              qrcodeStoreManage;

    @RequestMapping(value = "/validate.json", method = { RequestMethod.POST, RequestMethod.GET })
    public HttpResult validate(HttpServletRequest request, HttpServletResponse response,@RequestParam String alipayAccount) {
        //url会根据@RequestMapping先匹配到class类上的字符串,然后再匹配到方法上的字符串,会进入到这个方法中继续执行
        //代码片段
    }

}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP