Use the Struts controller to verify that the user name is duplicated asynchronously

Source: Internet
Author: User

Operating system: Windows 7

Application Server: apache-tomcat-6.0.18

Development tools: MyEclipse8.6

Java version: JDK1.6

mybatis:3.2.2

Struts version: struts-2.3

jquery version: Jquery-1.4.2.js

MySQL version: 5.0 mysql Community Server (GPL)

mysql-connector-java:5.0.18


April 16, 2013 note: The JSON data format used in this example recommends using Jackson with better performance to serialize, instead of using json-lib


Key points:

1. Introduction Package: Struts2 JSON serialization of the data, must use the Struts2 Json-lib,struts2-json-plugin package;

2.json Data Processing: Asynchronous submissions are in JSON format, Struts2-json-plugin data is JSON-processed, so the extends of the package node in the STRUTS2 configuration file Struts.xml must Is :extends= "Json-default";

Example:<package name= "Default" namespace= "/test" extends= "Json-default" >

3. Similarly, because the JSON data is returned, the type of result must also be JSON-based;

Note: Struts2 can only be used when introducing the Json-lib,struts2-json-plugin Package, the Type property of result setting JSON does not error:<result name= " UserInfo "type=" JSON "></result>

4. For asynchronous commits, the value of the result node of the action should be empty, i.e. no more steering (for example: only <result name= "UserInfo" type= "JSON" ></result >);

Timing:

Green represents the client request; The purple represents the return from the database to the client.

JSP-----(form submission)---->jQuery------(Ajax async)----->Struts2-----(Action gets JSON data)---- Call service-------> Other (mybatis, etc.)-----(model)----->DB

DB----(model)------>mybatis--------->service-------Struts2 (action,result)------->jquery (Ajax)-------- -->jsp

Data table structure


Domain model

Package Qh.zcy.entity;import Java.util.date;public class UserInfo {private Integer ID;    Private String username;    private String password;    Private Integer age; Private Date insertdata;public UserInfo (integer ID, string username, string password, integer age,date insertdata) {super ( ); this.id = Id;this.username = Username;this.password = Password;this.age = Age;this.insertdata = InsertData;}    Public UserInfo () {super ();} @Override public String toString () {//TODO auto-generated Method stub return "id=" +id+ ", username=" +username+ ", p    Assword= "+password+", age= "+age+", insertdata= "+insertdata"; }public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public Integer Getage () {return age;} public void Setage (Integer age{this.age = age;} Public Date Getinsertdata () {return insertdata;}            public void Setinsertdata (Date insertdata) {this.insertdata = InsertData;} }

Userinfomapper.xml

Bottom Basedaoimpl.java

public int isIn (map<string, string> isinmap) {sqlsession sqlsession=getsqlsession.getsqlsession (); int count= Sqlsession.selectone ("Qh.zcy.dao.UserInfo.isIn", Isinmap); return count;}
Userinfodaoimpl.java

public class Userinfodaoimpl implements Userinfodao {private Basedao Dao=getbasedao.getbasedao (); @Overridepublic int IsIn (map<string, string> isinmap) {//TODO auto-generated method Stubint count=-1;try {count=dao.isin (ISINMAP);} catch (Exception e) {//Todo:handle exceptione.printstacktrace (); Getsqlsession.rollback ();} Finally{getsqlsession.commit ();} return count;}}

Business Layer Userinfoserviceimpl.java

public class Userinfoserviceimpl implements Userinfoservice {private Userinfodao dao=new userinfodaoimpl (); Overridepublic Boolean isIn (map<string, string> isinmap) {//TODO auto-generated method Stubboolean Flag=false;int Count =dao.isin (ISINMAP); Log.getlog (). info ("count=" +count), if (count >0) {flag=false;} Else{flag=true;} return flag;}}

Struts Controller Isinaction.java

public class Isinaction extends Actionsupport{private string msg;private string Username;public string getmsg () {return MS g;} public void Setmsg (String msg) {this.msg = msg;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} @Overridepublic String Execute () throws Exception {//TODO auto-generated method Stubuserinfoservice service=new UserInfo Serviceimpl (); Log.getlog (). info ("username=" +username); Map<string, string> isinmap=new hashmap<string, string> (); Isinmap.put ("username", username); @ Suppresswarnings ("unused") Boolean flag=service.isin (Isinmap); Log.getlog (). info ("flag=" +flag), if (Flag==false) {this.setmsg ("the user name already exists");} Else{this.setmsg ("You can use this user name");} return SUCCESS;}}

Struts.xml Configuration

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/dtds/ Struts-2.3.dtd "><struts>    <constant name=" struts.action.extension "value=" action "></ constant>    <constant name= "Struts.devmode" value= "true" ></constant>    <package name= " Jsonname "namespace="/"extends=" Json-default ">        <action name=" isIn "class=" Qh.zcy.action.IsInAction ">            <result type= "JSON" name= "Success" ></result>                </action>            </package>         </ Struts>
Presentation Layer JSON Ajax jquery

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
My project:



Use the Struts controller to verify that the user name is duplicated asynchronously

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.