私は初心者で、これが私の最初の投稿です。私は ajax と struts を組み合わせた単純なプロジェクトを行っています。私のコードは次のとおりです。私の質問は次のとおりです。jspページのアクションから正しい値(部門)を取得した後、「クエリを返す」ため、正しい結果が更新されます。ただし、QueryAction.java では、「return query;」を使用する必要がある場合 query.jsp の値を取得するには....どうすれば解決できますか?
クエリ.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;
}
}