1. demo1.jsp
<% @ Page language = "java" pageEncoding = "UTF-8" %>
<Html>
<Head>
<Title> demo1 </title>
<Script type = "text/javascript" src = "<% = request. getContextPath () %>/js/jquery-1.8.2.min.js"> </script>
<Script type = "text/javascript">
Function clickButton (){
Var url = 'ajaxrequest. action ';
Var params = {
Code: $ ("form # form1: input [name = code]"). val ()
};
JQuery. post (url, params, callbackFun, 'json ');
}
Function callbackFun (data ){
Alert (data. result );
Var divMsg = $ ("div # div_msg ");
If (data. result = "OK "){
DivMsg.html ("OK! ");
} Else {
DivMsg.html ("no! ");
}
}
</Script>
</Head>
<Body>
<Br>
<Table width = "60%" align = "center">
<Tr>
<Td> <strong> Tomato ADemo1 <br> </strong>
</Td>
</Tr>
</Table>
<Form id = "form1" action = "" method = "post">
<Table width = "60%" align = "center" border = "1" cellspacing = "3" cellpadding = "3">
<Tr height = "25px">
<Td> No.: </td>
<Td> <input type = "text" name = "code"> </td>
<Td> <div id = "div_msg" class = "div_msg_ OK"> & nbsp; </div> </td>
</Tr>
</Table>
<Table width = "60%" align = "center">
<Tr>
<Td> <br>
<Input type = "button" value = "OK" onclick = "clickButton ();"> & nbsp;
</Td>
</Tr>
</Table>
</Form>
</Body>
</Html>
2. Struts. xml
<? 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. enable. DynamicMethodInvocation" value = "false"/>
<Constant name = "struts. devMode" value = "false"/>
<Package name = "default" namespace = "/" extends = "struts-default">
<Global-results>
<Result name = "error">/error. jsp </result>
</Global-results>
<Global-exception-mappings>
<Exception-mapping exception = "java. lang. Exception" result = "error"/>
</Global-exception-mappings>
</Package>
<Package name = "ajax" extends = "json-default">
<Action name = "ajaxRequest"
Class = "tomato. ademo. ValidateCodeAction">
<Result type = "json"> </result>
</Action>
</Package>
</Struts>
3. ValidateCodeAction. java
Package tomato. ademo;
/**
* ValidateCodeAction
*
* @ Author hairui 2012-10-20
* @ Since 2.0.5
*/
Public class ValidateCodeAction {
Private String code;
Private String result;
/**
* Execute
*/
Public String execute (){
If (code. inclusignorecase ("hairui123 ")){
This. result = "OK ";
} Else {
This. result = "no ";
}
Return "success ";
}
Public void setCode (String code ){
This. code = code;
}
Public String getResult (){
Return result;
}
}