我正在使用 struts2-jquery-plugin。通常在我的表格中,我需要按学校科目从数据库中填充我的第一个选择,并且根据所选的学校科目,我需要更新第二个选择并由教授该科目的教师从数据库中填充它。
我在下面的链接中找到了上面的示例代码,但我猜不出它是如何工作的。他们不解释。 http://struts2-jquery.appspot.com/home.action#。
我是否被迫使用 JSON?任何可以帮助我解决问题的解释或代码将不胜感激。
<label>First Select:</label>
<sj:select id="firstSelect" name="firstSelect" onChangeTopics="firstSelectChanged" src="firstSelect.action"/>
<label>Second Select:</label>
<sj:select id="secondSelect" reloadTopics="firstSelectChanged" src="secondSelect.action" elementIds="firstSelect"/>
更新
我尝试了这个链接http://struts.jgeppert.com/struts2-jquery-showcase/index.action(Ajax Link > AJAX Select部分)在nmc的这篇文章中建议,但我得到这个错误:
Struts Problem Report
Struts has detected an unhandled exception:
Messages: •There is no Action mapped for namespace / and action name echo.
--------------------------------------------------------------------------------
Stacktraces
There is no Action mapped for namespace / and action name echo. - [unknown location]
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:177)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:458)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Unknown Source)
java Action 类:
package test.action;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage( value = "showcase")
public class JsonSample extends ActionSupport {
private static final long serialVersionUID = -2223948287805083119L;
private List<String> languageList;
private List<String> reloadList;
private String language;
@Actions( {
@Action(value = "/jsonsample", results = {
@Result(name = "success", type = "json")
})
})
public String execute()
{
languageList = new ArrayList<String>();
languageList.add("Java");
languageList.add("PHP");
languageList.add("C#");
reloadList = new ArrayList<String>();
if (language != null && language.equalsIgnoreCase("Java"))
{
reloadList.add("Struts2");
reloadList.add("MyFaces");
reloadList.add("Tapestry");
}
else if (language != null && language.equalsIgnoreCase("PHP"))
{
reloadList.add("CakePHP");
reloadList.add("Symfony");
reloadList.add("Zend");
}
else if (language != null && language.equalsIgnoreCase("C#"))
{
reloadList.add("NStruts");
reloadList.add("ProMesh.NET");
reloadList.add("Websharp");
}
return SUCCESS;
}
public String getJSON()
{
return execute();
}
public List<String> getLanguageList()
{
return languageList;
}
public List<String> getReloadList()
{
return reloadList;
}
public void setLanguage(String language)
{
this.language = language;
}
public String getLanguage() {
return language;
}
public void setLanguageList(List<String> languageList) {
this.languageList = languageList;
}
public void setReloadList(List<String> reloadList) {
this.reloadList = reloadList;
}
}
JSP 页面:
<s:form id="formSelectReload" action="echo" theme="simple" cssClass="yform">
<fieldset>
<legend>AJAX Form</legend>
<div class="type-text">
<label for="language">Language: </label>
<s:url id="remoteurl" action="jsonsample"/>
<sj:select
href="%{remoteurl}"
id="language"
onChangeTopics="reloadsecondlist"
name="language"
list="languageList"
listKey="myKey"
listValue="languageList"
emptyOption="true"
headerKey="-1"
headerValue="Please Select a Language"
/>
</div>
<div class="type-text">
<label for="echo">Framework: </label>
<s:url id="remoteurl" action="jsonsample"/>
<sj:select
href="%{remoteurl}"
id="selectWithReloadTopic"
formIds="formSelectReload"
reloadTopics="reloadsecondlist"
name="echo"
list="reloadList"
emptyOption="true"
headerKey="-1"
headerValue="Please Select a Framework"
/>
</div>
<div class="type-button">
<sj:submit
id="submitFormSelectReload"
targets="result"
value="AJAX Submit"
indicator="indicator"
button="true"
/>
<img id="indicator"
src="images/indicator.gif"
alt="Loading..."
style="display:none"
/>
</div>
</fieldset>
</s:form>
当我从表单中删除action="echo"时,我没有更多错误,但结果我得到了图片中显示的某种包含。当我单击 Ajax 提交时,我在结果中获得了选定的选项,而不是在结果 div 中获得了表单。再次感谢你。