spring3 MVC jquery ajax under the combination of simple to use - Super Simple

Today need to write a simple application: user registration, check the user name is available.

Seemingly on the next before struts2 ajax support is good (actually irritated), and there are many online tutorials. But this is replaced springmvc, and it is spring3, few things online.

However, the discovery or springsource article .

The client uses the formValidtor plug, this plug-in is really good, huh, huh.

$("#loginName").formValidator({onshow:" Please enter a user name  ",onfocus:" The user name for at least 6 characters  , Up to 15 characters  ",oncorrect:" The username you can register  "}).inputValidator({min:6,max:15,onerror:" Did you enter the name of illegal, please confirm  "}).regexValidator({regexp:"username",datatype:"enum",onerror:" User name is not in the correct format  "}).ajaxValidator({
            type : "get",
                url : "${ctx}/ajax/checkLoginName",
                datatype : "json",
                success : function(data){
            if( data == "1" )
                        {
                return true;
                        }
            else
                        {
                return false;
                        }
                },
                buttons: $("#button"),
                error: function(){alert(" The server did not return the data, perhaps the server is busy  , Please try again  ");},
                onerror : " The user name is not available, please change username  ",
                onwait : " The user name to validate, please wait  ..."
        });

This is formValidator ajax form validation that comes with very clear.

In the article, found on the plot of a foreigner can have a more simple way ajax call:

jQuery.getJSON( url, [ data ], [ callback(data, textStatus) ] )

Known from jquey website:

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

Corresponds to the server can be too cool, and really is a standard spring control! ! ! !

@Controller
@RequestMapping("/ajax")
public class CommonServControl {

        @RequestMapping(value = "/checkLoginName", method = RequestMethod.GET)
        public @ResponseBody boolean checkLoginName(@RequestParam String loginName) {
                boolean b = userServ.checkLoginName(loginName);
                return b;
        }

        @Autowired
        private UserServ userServ;

}

I believe this code is enough "POJO" instead.

Of course, pay attention! ! ! This is not enough!

Wrote,

Underneath the covers, Spring MVC delegates to a HttpMessageConverter to perform the serialization. In this case, Spring MVC invokes a MappingJacksonHttpMessageConverter built on the Jackson JSON processor. This implementation is enabled automatically when you use the mvc: annotation-driven configuration element with Jackson present in your classpath.

This passage means, spring use "HttpMessageConverter" to work, so we need to do one thing:

In springMVC's XXX-servlet.xml file by adding such a line:

<mvc:annotation-driven />

 Namespace  :
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        default-autowire="byName"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

OK bin!

This is a post in a way which is almost, huh, huh

1. $( "#account" ).submit( function () {

2. var account = $( this ).serializeObject();

3. $.postJSON( "account" , account, function (data) {

4. $( "#assignedId" ).val(data.id);

5. showPopup();

6. });

7. return false ;

8. });

On the server-side, the Controller is more standard Java with Spring MVC annotations:

view source

Print ?

01. @RequestMapping (method=RequestMethod.POST)

02. public @ResponseBody Map<String, ? extends Object> create( @RequestBody Account account, HttpServletResponse response) {

03. Set<ConstraintViolation<Account>> failures = validator.validate(account);

04. if (!failures.isEmpty()) {

05. response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

06.
spring mvc bind the entity under the editor submitted a solution for ...

  • 21:06
  • View (0)
  • Comments (0)
  • Category: Education Management System
  • Related recommendations
Comment
Comment

spring3 MVC jquery ajax under the combination of simple to use - Super Simple

mxdba321123

  • View: 3013 times
  • Gender:
  • From: Hangzhou
  • Details book
Search this blog
Recent visitors>> More Visitors

dowsam

gis223

wwind

FQ_kevin

Blog Categories
  • All blog (28)
  • Database (3)
  • JAVA (3)
  • CSS (0)
  • IDE (1)
  • Spring (4)
  • Struts2 (2)
  • hibernate (3)
  • Miscellaneous notes (2)
  • freemarker (1)
  • Education Management System (9)
Other Categories
  • My Favorites (21)
  • My forum topics posted (0)
  • I posted all the forums (7)
  • I am the essence of a good paste (0)
  • I solved the problem (1)
Recently joined the circle
  • GT-Grid
Archive
  • 2010-04 (3)
  • 2010-03 (2)
  • 2010-02 (2)
  • More archives ...
Latest Comments
  • Use the annotation to reduce spring ...
    I am here to ask to use @ Autowired access serivce when the database operation ...
    - By Zhu Yuanyu
  • The basics of MySQL and I used in the design development tool, although not very familiar with, but the software will automatically choose the more appropriate type of data table ...
    - By poplar1986
Comments list
  • Use the annotation to reduce spring bean configuration
  • java file path summary (change)
  • freemark few tips
  • 3.2 Specific technology-fckeditor2.6
  • 3.1 Specific technology - related pages

分类:Java 时间:2010-04-13 人气:954
分享到:
blog comments powered by Disqus

相关文章

  • JQuery's json functionality expansion 2009-06-16

    In this paper, based on jQuery expanded its capacity to deal with json string, great jQuery will be great, ha ha! Writing javascript with jQuery is very efficient, jQuery for ajax package also in place; using jQuery after a certain period of time, jQuery

  • servlet + jquery ajax + json demo 2010-07-16

    1. The corresponding jar package imports. Json-lib-2.2.3-jdk15.jar, ezmorph-1.0.6.jar so. 2. Join jquery.js 3. Jquery each () method to use api. http://api.jquery.com/jQuery.each/ 4. Server servlet program import javax.servlet.http.HttpServlet; import jav

  • struts2 + jquery + ajax + json + struts2_json_plugin configuration example 2011-01-01

    1.struts.xml <constant name="struts.i18n.encoding" value="utf-8" /> <package name="jsondefault" namespace="/json" extends="json-default"> <action name="loginByAjax"> <result type="json"/> </action> </package> 2.Action p

  • Struts2 + jQuery + ajax + json example 2011-07-20

    struts2 + jquery + json + ajax application, write down, and after the backup. 1 Environment jquery: jquery-1.6.2.min.js struts2: Basic package is not said, to talk about the application json package, mainly struts2-json-plugin-2.2.1.1.jar json: json packa

  • asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码 2014-04-24

    本篇文章主要是对asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码进行了介绍,需要的朋友可以过来参考下,需要对大家有所帮助 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxJson.aspx.cs" Inherits="AjaxJson" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:

  • JQuery处理json与ajax返回JSON实例代码 2014-09-08

    json数据是一种经型的实时数据交互的数据存储方法,使用到最多的应该是ajax与json配合使用了,下面我来给大家介绍jquery处理json数据方法 一、JSON的一些基础知识。 JSON中对象通过"{}"来标识,一个"{}"代表一个对象,如{"AreaId":"123"},对象的值是键值对的形式(key:value)。 "[]",标识数组,数组内部各个数据之间通过","分割,如["AreaId":"123","AreaId":"345"]。 很多情况下是对象数组,那就是这样: [{"Area

  • JQuery's JSON caching to solve the problem 2010-01-21

    Phenomenon: the page to use jQuery to change the contents of the Ajax way, the deposit to the database using the jQuery.getJSON way to re-read data, page not updated, re-log on the system until after the page displays the changed data. Reasons: IE wi ...

  • Struts2, AJAX, json-plugin to use 2009-08-20

    Codes, please refer to http://acheron.javaeye.com/admin/blogs/402499 JSON Official Document http://www.json.org/json-zh.html jQuery Official Document http://docs.jquery.com/Main_Page Js Code <SPAN> Function removerecordbyid (recordid) ( $ ( "#

  • JQuery Applications 5: cascading drop-down box (JQuery and JSON) 2010-04-20

    Page as follows: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>JQuery_Study_Select</title> <link rel="stylesheet" type="text/css" href="css/select.css"> <script language="javasc

  • JQuery's JSON caching problem solving 2010-05-07

    Phenomenon: the Ajax page using the jQuery way to change the content, the author saved to the database using jQuery.getJSON way to re-read data, page not updated, re-sign-on system changes after the page to display the data. Reason: IE will cache jQu ...

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.305 (s). 12 q(s)