我就在struts2.0下做一个简单的Ajax功能 结果struts2老是先去执行execute方法
在网上看了好多例子 可没解决我的问题 唉 谁来棒棒忙啊
先谢过 88分献上
代码如下
index.jsp
- Java code
<%@ page language="java" pageEncoding="UTF-8"%><%@ include file="/Preamble.jspf" %><html> <head> <title></title> </head> <body> <hr> <s:form action="searchAcc" method="get"> <table align="center" class="table02"> <tr> <td class="content_L" width="40%">子公司代號:</td> <td width="60%"><input type="text" id="company_no" name="company_no"/></td> </tr> <tr> <td class="content_L">分行代號:</td> <td><input type="text" name="bank_id" id="bank_id"/></td> </tr> <tr> <td class="content_L">廠商代號:</td> <td><input type="text" id="factory_id" name="factory_id"/></td> </tr> <tr> <td id="label"></td> <td class="text_R"> <s:submit value="%{getText('text.confirm')}" onclick="confirmData()" cssClass="f_btn01"/> <s:reset cssClass="f_btn01" value="%{getText('text.reset')}"/></td> </tr> </table> </s:form> </body> <script type="text/javascript">function confirmData(){ var com=$("#company_no"); var bank=$("#bank_id"); var factory=$("#factory_id");alert(com.val()); $.get("searchAcc!ajax.action", { company_no: "56757", bank_id:"777",factory_id:"6666" }, function(data){ alert("Data Loaded: " + data); }); if(data){ $("#label").text("無此驗收單!"); $("#label").attr("class","overdue"); } alert(2) return data;} </script></html>
Action 代码
- Java code
package com.plat.acc.action;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts2.ServletActionContext;import util.BeanUtil;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.plat.acc.proxy.AcceptenceProxy;public class SearchAccAction extends ActionSupport{ private static final Log log = LogFactory.getLog(SearchAccAction.class); private String company_no;//能传到页面 private Integer bank_id; private Integer factory_id; /** *处理用户请求的excute()方法 *@return结果导航字符串 *@throws Exception */ public String execute() throws Exception{ return SUCCESS; } public void ajax(){ PrintWriter pw = null; HttpServletResponse response = ServletActionContext.getResponse(); //取response对象 try { response.setContentType("text/xml;charset=utf-8"); response.setHeader("Cache-Control","no-cache"); /*AcceptenceProxy proxy=(AcceptenceProxy)BeanUtil.getBean(AcceptenceProxy.ROLE); List list=(ArrayList)proxy.findAccByLtd_Bank_Fac(this.company_no,this.factory_id,this.bank_id);; */ pw = response.getWriter(); pw.print(""); pw.print(""); pw.print(""); } catch (IOException e) { log.error(e.getMessage()); e.printStackTrace(); } finally{ if(pw != null){ pw.close(); pw = null; } } } public String getCompany_no() { return company_no; } public void setCompany_no(String company_no) { this.company_no = company_no; } public Integer getBank_id() { return bank_id; } public void setBank_id(Integer bank_id) { this.bank_id = bank_id; } public Integer getFactory_id() { return factory_id; } public void setFactory_id(Integer factory_id) { this.factory_id = factory_id; }}