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 pMsg = $("p#p_msg");
            if(data.result=="ok"){
             pMsg.html("ok!");
            }else{
             pMsg.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><p id="p_msg" class="p_msg_ok">&nbsp;</p></td>
      </tr>    
    </table>
   
    <table width="60%" align="center">
      <tr>
       <td><br>
         <input type="button" value="確定" 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.equalsIgnoreCase("hairui123")){
            this.result = "ok";
        }else{
            this.result = "no";
        }
       
        return "success";
    }

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

    public String getResult() {
        return result;
    }

}

 

 

 

發佈留言