Spring mvc 处理ajax请求

1.在页面上最好不要用submit触发ajax,因为这样会很容易出问题,今天学习的时候就遇到了触发不了ajax的问题。所以最好用button来触发。
2.若想利用ajax提交form表单,则可以在ajax的data属性中提供form表单的序列化,如:

data:$('#formId').serialize()

3.若要用submit提交信息,则可以用:

$('#formId').submit(function() {
//ajax代码
});

注意:此时的方式要在form表单中设置,在ajax设置是无效的!!!
基于上述三点提供一个例子

<body>
    <form action="#" id="userInfo" method="post">
        用户名:<input type="text" name="userName" id="name"/><br/>
        密码:<input type="password" name="password" id="password"/><br/>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>
<script type="text/javascript">
    $(function() {
        $('#userInfo').submit(function() {
            $.ajax({
                type:'POST',
                url:'<%=request.getContextPath()%>/user/createUser2',
                data:$('#userInfo').serialize(),
                success:function(data){
                    alert(data);
                },
                error:function()
                {
                    alert("服务器错误");
                    alert("hahhahah");
                    window.location.href = "<%=request.getContextPath()%>/faile.jsp";
                }
            });
        });
    });
</script>

4.spring mvc处理ajax请求时,若不利用json返回数据,则返回的是一个页面,如:

@RequestMapping(value="/createUser2", method=RequestMethod.POST)
    public Map<String, String> createUser2(ModelMap model, UserModel user)
    {
        System.out.println(user);
        model.addAttribute("user", user);
        System.out.println("createUser2:"+user.toString());
        return “user/success”;
    }

此时返回的是我定义的success.jsp页面代码,若改变返回值(没有映射到相应的页面),则ajax请求失败。
5.所以处理ajax返回时,最好是用json返回。这里我利用的是jackson技术,自动包装为json数据,首先在spring-servlet.xml中配置:

<mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                        <property name="dateFormat"><bean class="java.text.SimpleDateFormat">
                            <constructor-arg type="java.lang.String" value="yyyyMMddHHmmss"/>
                            </bean>
                        </property>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

然后在对应的Controller方法上添加@ResponseBody注解将信息转化为json并绑定到返回的参数中,如:

@ResponseBody
    @RequestMapping(value="/createUser2", method=RequestMethod.POST)
    public Map<String, String> createUser2(ModelMap model, UserModel user)
    {
        System.out.println(user);
        model.addAttribute("user", user);
        System.out.println("createUser2:"+user.toString());
        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "test1");
        map.put("2", "test2");
        return map;
    }

此时的返回值可以是String,list,map或者其他类型。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值