如何将Ajax和Struts2组合在一起?

时间:2012-11-12 07:03:52

标签: java ajax spring struts2

我是新手,这是我的第一篇文章,我正在做一个结合了ajax和struts的简单项目。我的代码如下,我的问题是:在我从jsp页面上的Action获得正确的值(部门)之后,由于“返回查询”,正确的结果将被刷新。但是,在QueryAction.java中,如果我必须使用“return query;”获取query.jsp的值....如何解决?

query.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

    <head>
    <s:head />
      <title>Query Page</title>
      <h1 align="center" id="h1"></h1>
    </head>
<body>
    <p id="demo"></p>

       <s:include value="/msg.jsp" />
       <s:form action="query" id="form">  
            <s:textfield name="name" id="name" label="Search the Department" />          
                    <s:submit value="Search" onclick="return myFunction()" id="submitRowAdd" />                    
       </s:form>

       <table border="1" id="depTable">
        <tr> 
            <td>Department ID</td>
            <td>Department Name</td>
            <td>Manager ID</td>
            <td>Repeal</td>
        </tr>           
       </table>
       <br>
       <script type="text/javascript">
    var btnAdd = document.getElementById("form");
    btnAdd.onsubmit = function(){
        var txtTd = document.createTextNode("<s:property value="did"/>");
        var eleTd = document.createElement("td");
        eleTd.appendChild(txtTd);  

        var txtTd1 = document.createTextNode("<s:property value="dname"/>");
        var eleTd1 = document.createElement("td");
        eleTd1.appendChild(txtTd1);  

        var txtTd2 = document.createTextNode("<s:property value="mid"/>");
        var eleTd2 = document.createElement("td");
        eleTd2.appendChild(txtTd2);  

        var txtTd3 = document.createTextNode("<s:property value="rep"/>");
        var eleTd3 = document.createElement("td");
        eleTd3.appendChild(txtTd3);  

        var eleTr = document.createElement("tr");
        eleTr.appendChild(eleTd); 
        eleTr.appendChild(eleTd1); 
        eleTr.appendChild(eleTd2); 
        eleTr.appendChild(eleTd3);

        var theTable = document.getElementById("depTable");
        theTable.appendChild(eleTr);                        
    }
</script>

QueryAction.java

package actions;
import com.Department;
import com.opensymphony.xwork2.ActionSupport;
import service.DepartmentService;

import net.sf.json.JSONObject;

public class QueryAction extends ActionSupport {

private String name;
DepartmentService ds;
long did;
String dname;
String mid;
char rep;
    private String result; 

public String execute() {   

    if(name != null && !"".equals(name)) {
        Long lname = Long.valueOf(name.trim());
        Department ppp = ds.findById(lname);
        if(ppp==null) {
            return "sss";
        }
        //did = ppp.getDepartmentId();
        //dname = ppp.getDepartmentName();
        //mid = ppp.getManagerId();
        //rep = ppp.getRepeal();

        JSONObject json=JSONObject.fromObject(ppp); //I run debug it fail on here !
                  result=json.toString();
                  System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" +  result);
    } //return "result";
    return SUCCESS;                          
     }

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public DepartmentService getDs() {
    return ds;
}
public void setDs(DepartmentService ds) {
    this.ds = ds;
}
public long getDid() {
    return did;
}
public void setDid(long did) {
    this.did = did;
}
public String getDname() {
    return dname;
}
public void setDname(String dname) {
    this.dname = dname;
}
public String getMid() {
    return mid;
}
public void setMid(String mid) {
    this.mid = mid;
}
public char getRep() {
    return rep;
}
public void setRep(char rep) {
    this.rep = rep;
}
    public String getResult() {
    return result;
}

public void setResult(String result) {
    this.result = result;
}


}

0 个答案:

没有答案