Struts2 + jQuery ajax (2) ADemo-1.0

Source: Internet
Author: User

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;
}

}

 

 


 

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.