SSM整合框架实现ajax校验

刚学习了ssm框架,ajax校验成功,分享下

1.导入jar包

2.配置spring-servlet.xml

 1 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
 2             <property name="messageConverters">  
 3                 <list>  
 4                     <ref bean="mappingJackson2HttpMessageConverter" />  
 5                 </list>  
 6             </property>  
 7         </bean>  
 8         <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
 9             <property name="supportedMediaTypes">  
10                 <list>  
11                     <value>text/html;charset=UTF-8</value>  
12                     <value>text/json;charset=UTF-8</value>  
13                     <value>application/json;charset=UTF-8</value>  
14                 </list>  
15             </property>  
16         </bean>

3.控制器中用@ResponseBody实现返回json数据格式

1 @ResponseBody
2      @RequestMapping(value="queryByUser", method=RequestMethod.POST,produces="application/json;charset=UTF-8")  
3     public User queryByName(User user,HttpServletRequest request){
4         User u = userBiz.queryByName(user);
5         return u;
6     }

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("用户名可用");
              }
            }
          });
      }

实现结果就是以下这样的:

posted @ 2016-10-13 14:54  小盐  阅读(8250)  评论(1编辑  收藏  举报