Ajax interaction Struts2 action (Client/Server)

Source: Internet
Author: User

1. Client webpage code
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GB18030">
<Title> check whether the user name is unique </title>
<Script language = "javascript">
Function createRequest (url ){
Http_request = false;
If (window. XMLHttpRequest) {// non-IE browser
Http_request = new XMLHttpRequest (); // create an XMLHttpRequest object
} Else if (window. ActiveXObject) {// IE browser
Try {
Http_request = new ActiveXObject ("Msxml2.XMLHTTP"); // create an XMLHttpRequest object
} Catch (e ){
Try {
Http_request = new ActiveXObject ("Microsoft. XMLHTTP"); // create an XMLHttpRequest object
} Catch (e ){
}
}
}
If (! Http_request ){
Alert ("You cannot create an XMLHttpRequest object instance! ");
Return false;
}
Http_request.onreadystatechange = getResult; // call the return result processing function.
Http_request.open ('get', url, true); // create a connection with the server
Http_request.send (null); // send a request to the server
}
Function getResult (){
If (http_request.readyState = 4) {// determine the Request status
If (http_request.status = 200) {// The request is successful and the returned results are processed.
Document. getElementById ("toolTip"). innerHTML = http_request.responseText; // set the prompt content
Document. getElementById ("toolTip"). style. display = "block"; // a prompt box is displayed.
} Else {// error on the request page
Alert ("the page you requested has an error! ");
}
}
}
Function checkUser (userName ){
If (userName. value = ""){
Alert ("Enter the user name! ");
UserName. focus ();
Return;
} Else {
// CreateRequest ('HTTP: // 10.65.9.181: 8090/ajax/checkUser. jsp? User = '+ userName. value );
CreateRequest ('HTTP: // 10.65.9.181: 8090/ajax/checkUser. action? User ='
+ UserName. value );
}
}
</Script>
<Style type = "text/css">
<! --
# ToolTip {
Position: absolute;
Left: 331px;
Top: 39px;
Width: 98px;
Height: 48px;
Padding-top: 45px;
Padding-left: 25px;
Padding-right: 25px;
Z-index: 1;
Display: none;
Color: red;
Background-image: url (images/tooltip.jpg );
}
-->
</Style>
</Head>
<Body style = "margin: 0px;">
<Form method = "post" action = "" name = "form1">
<Table width = "509" height = "352" border = "0" align = "center"
Cellpadding = "0" cellspacing = "0" background = "images/bg.gif">
<Tr>
<Td height = "54"> </td>
</Tr>
<Tr>
& Lt; td height = "253" valign = "top" & gt;
<Div style = "position: absolute;">
<Table width = "100%" height = "250" border = "0" cellpadding = "0"
Cellspacing = "0">
<Tr>
<Td width = "18%" height = "54" align = "right" style = "color: #8e6723"> <B> User name: </B> </td>
<Td width = "49%"> <input name = "username" type = "text"
Id = "username" size = "32"> </td>
<Td width = "33%"> Height = "23" style = "cursor: hand ;"
OnClick = "checkUser (form1.username);"> </td>
</Tr>
<Tr>
<Td height = "51" align = "right" style = "color: #8e6723"> <B> password: </B> </td>
<Td> <input name = "pwd1" type = "password" id = "pwd1" size = "35"> </td>
<Td rowspan = "2">
<Div id = "toolTip"> </div>
</Td>
</Tr>
<Tr>
<Td height = "56" align = "right" style = "color: #8e6723"> <B> Confirm Password: </B> </td>
<Td> <input name = "pwd2" type = "password" id = "pwd2" size = "35"> </td>
</Tr>
<Tr>
<Td height = "55" align = "right" style = "color: #8e6723"> <B> Email: </B> </td>
<Td colspan = "2"> <input name = "email" type = "text" id = "email"
Size = "45"> </td>
</Tr>
<Tr>
<Td> </td>
<Td colspan = "2"> <input type = "image" name = "imageField"
Src = "images/registerBt.jpg"> </td>
</Tr>
</Table>
</Div>
</Td>
</Tr>
<Tr>
<Td> </td>
</Tr>
</Table>
</Form>
</Body>
</Html>

2. server code
Code of the Action class
Copy codeThe Code is as follows:
Package com. action;
Import java. util. Map;
Import com. opensymphony. xwork2.ActionContext;
Import com. opensymphony. xwork2.ActionSupport;
Import com. xzy. UserDAO;
Public class CheckUserAction extends ActionSupport {
Private String user;
Public String findUserByName (){
String info = null;
UserDAO userdao = new UserDAO ();
If (userdao. findUserByName (user )){
// Info = "the user name has been registered ";
Map map = (Map) ActionContext. getContext (). get ("request ");
Map. put ("info", "user name registered ");
Return "success ";
} Else {
// Info = "user name can be registered ";
Map map = (Map) ActionContext. getContext (). get ("request ");
Map. put ("info", "user names can be registered and used ");
Return "fail ";
}
}

Public String getUser (){
Return user;
}

Public void setUser (String user ){
This. user = user;
}
}

Struts. xml configuration
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1 // EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<Struts>
<Package name = "myPackage" extends = "struts-default">
<! -- Define action -->
<Action name = "checkUser" class = "com. action. CheckUserAction" method = "findUserByName">
<! -- Define the ing page after successful processing -->
<Result>/info. jsp </result>
</Action>
</Package>
</Struts>

Info. jsp: Display Information Page
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% = Request. getAttribute ("info") %>

Info. jsp is a jsp page. for jsp pages that interact with Android clients, remove unnecessary html code as much as possible, you only need to retain the processing code between the code that controls the encoding format and <%>, so as to avoid displaying unnecessary junk code on the android client and improve the execution efficiency, reduces server load.
Database:
 
Finally:

Related Article

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.