我正在使用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中通过 nmc 尝试了此链接this post( Ajax链接&gt; AJAX选择部分)但我收到此错误:
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 Submit时,而不是我在结果中得到选中的选项,我在结果div中得到了表单。再次感谢你。
答案 0 :(得分:0)
我认为你会发现http://struts.jgeppert.com/struts2-jquery-showcase/index.action的例子更有用,因为他们也有Java代码。查看 Ajax表单 - &gt; AJAX选择(Doubleselect)。
The documentation也有一些很好的代码示例。
有些注意事项:
该操作必须返回JSON。
也就是说,填充href
标记的<sj:select>
属性中引用的选择列表的操作必须返回JSON作为示例中的注释:
@Actions({
@Action(
value="/jsonsample",
results={
@Result(name="success",type="json")
})
})
还有一种方法可以在struts.xml
文件中指定它。在Stackoverflow和互联网上应该有很多这方面的例子。
您的第二个选择应包含formIds
属性
它指定其字段值应序列化并随请求一起发送的表单。这是填充第二个列表的操作接收第一个列表中设置的值的方式。