springMVC架構中的ajax驗證

來源:互聯網
上載者:User

標籤:驗證   this   utf-8   ase   value   res   ret   success   UI   

當然,你在使用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();    }}

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

 

springMVC架構中的ajax驗證

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.