Ajax 框架之SSM整合框架实现ajax校验
程序员文章站
2022-06-24 16:09:47
刚学习了ssm框架,ajax校验成功,分享下
1.导入jar包
2.配置spring-servlet.xml
刚学习了ssm框架,ajax校验成功,分享下
1.导入jar包
2.配置spring-servlet.xml
<bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter"> <property name="messageconverters"> <list> <ref bean="mappingjackson2httpmessageconverter" /> </list> </property> </bean> <bean id="mappingjackson2httpmessageconverter" class="org.springframework.http.converter.json.mappingjackson2httpmessageconverter"> <property name="supportedmediatypes"> <list> <value>text/html;charset=utf-8</value> <value>text/json;charset=utf-8</value> <value>application/json;charset=utf-8</value> </list> </property> </bean>
3.控制器中用@responsebody实现返回json数据格式
@responsebody @requestmapping(value="querybyuser", method=requestmethod.post,produces="application/json;charset=utf-8") public user querybyname(user user,httpservletrequest request){ user u = userbiz.querybyname(user); return u; }
4.页面中接收查询到的结果
function checkuser(){ var username=$("#username").val(); $.ajax({ url:"querybyuser", type:"post", data:{"username":username}, datatype:"json", success:function(data){ if (data!=null ) { $("#userspan").text("用户名已存在"); $("#username").val(''); }else if(data==null && username !=''){ $("#userspan").text("用户名可用"); } } }); }
实现结果就是以下这样的:
以上所述是小编给大家介绍的ajax 框架之ssm整合框架实现ajax校验,希望对大家有所帮助
上一篇: JSP+Ajax 添加、删除多选框