springMVC框架中的ajax驗證


當然,你在使用springMVC之前需要進行環境的配置,這里就不講了,直接上代碼.

在使用springMVC之前,我在使用ajax驗證的時候,需要用到一個解析json的jar包:將數據通過ajax拿到后台servlet,再通過jsonObject對象進行來像前台進行數據的傳遞.像下面這樣:

JSONObject j = new JSONObject();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

j.put("msg", "該用戶名已被注冊");
out.write(j.toString());

使用起來還是很方便的.但是在學習到了框架之后,尤其是使用到了springMVC之后,使用起來就更方便了.下面是使用過程,其實和之前的使用是一樣的.

 

這是實體類:set/get代碼就不貼了

 

1 public class User {
2     private int uid;
3     private String uname;
4     private String password;

jsp代碼:注意導入jquery包,在這里我們使用jquery的ajax驗證

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    
    <script type="text/javascript" src="resource/js/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        $(function(){
        
            $("#password").blur(function(){
                $.ajax({
                    url:"user/ajax",
                    data:{uname:$("#uname").val(),password:$("#password").val()},
                    type:"post",
                    success:function(data){
                        alert(data);
                    }
                });
            });
        });
    </script>
  </head>
  
  <body>
        用戶名<input id="uname" type="text" name="uname">
        密碼<input id="password" type="password" name="password">
  </body>
</html>

后台代碼:這里使用的是springMVC的注解方式,需要在具體的ajax驗證方法的上面標注@ResponseBody.return的內容就是你要在前台頁面上要處理的數據.

 
        
 
        

 

package com.mi.controller;



import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.mi.entity.User;
@Controller
@RequestMapping("user")
public class MyController extends MultiActionController{
    @RequestMapping(value="ajax",produces={"text/html;charset=utf-8;"})
    @ResponseBody
    public Object ajax(User user){
        System.out.println(user.getUname());
        System.out.println(user.getPassword());
        System.out.println(user);
        return user.getPassword();
    }
}

大概就是這些,暫時先學了這么一些,做一個小小的總結,之后有新的知識再進行更新.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM