当前位置:编程学习 > JAVA >>

Struts2 + jQuery 实现ajax (2) ADemo-1.0

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>编号:</td>
       <td><input type="text" name="code"></td>
       <td><div id="div_msg" class="div_msg_ok"> </div></td>
      </tr>    
    </table>
   
    <table width="60%" align="center">
      <tr>
       <td><br>
         <input type="button" value="确定" onclick="clickButton();"> 
       </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.equalsIgnoreCase("hairui123")){
            this.result = "ok";
        }else{
            this.result = "no";
        }
       
        return "success";
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getResult() {
        return result;
    }

}

 

 


 

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,