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


  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

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

  • json + js + java

    Yesterday evening on the json + js + servlet to carry out a study of communication study. JJS present sense of self, communication is very simple, a large number of packets launched JJS greatly simplifies the difficulty of this model. From the back of the

  • JavaScript's "this"

    Read recently in Programming Languages Pragmatics when they see this, although does not seem to do anything worth mentioning, in order to avoid forgotten or recorded. JavaScript's this is called object. Say the image is that "." Prior t ...

  • javascript implementation of telecommunications network automatically select two-lane

    javascript implementation of telecommunications network automatically select two-lane

  • Ajax Portal

    The first test procedure: <html> <title>Full use XMLHttpRequest to load the document example </title> <head> <script type='text/javascript'> var req=null; var console=null; var READY_STATE_UNINITIALIZED=0; var READY_STATE_LOA

  • Read "prototype.js 1.4 Developer Edition Manual" excerpt

    Information Address: http://thinhunan.cnblogs.com/archive/2006/04/01/DeveloperNotesForPrototype.html Extract 1, $ F () function Function: Return of any form input control value, the parameters are the element id or the element itself Example: <script

  • IBM Ajax Tutorial Series

    Links: http://ibm.csdn.net/ISN_J.aspx?action=JMP&pointid=1550 Part 10: Using JSON for data transmission In the asynchronous application to send and receive information, you can choose to plain text and XML as data format. Ajax grasp of this issue ...

  • Depth understanding of the eval function in javascript

    http://wanyij.blog.51cto.com/46570/43794 In this paper, the discovery of an appropriate title is not so easy, huh, huh, so in this note under the first two purposes of this article: (1) introduction of the eval function in javascript usage (2) how to func

  • jquery ajax example

    1. Check js file $. ajax (( type: "GET", url: "test.js", dataType: "script" )) 2.post data $. ajax (( type: "POST", url: "xxxx", data: "name = zhaoaiqing & age = 25", success: function ( ...

  • java read file

    java read documents in two ways: java.io and java.lang.ClassLoader When using the java.io, when java.lang.ClassLoader use it? (Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to understa

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries