Java 如何每隔x秒显示表单的结果
我有一些表格可以从数据库中搜索结果。如何每隔x秒自动显示来自db的结果?这意味着每x秒提交一次按钮。我所发现的关于刷新的所有内容都是这个类:Java 如何每隔x秒显示表单的结果,java,wicket,Java,Wicket,我有一些表格可以从数据库中搜索结果。如何每隔x秒自动显示来自db的结果?这意味着每x秒提交一次按钮。我所发现的关于刷新的所有内容都是这个类: add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(10)) { private static final long serialVersionUID = 1; }); 其中只刷新页面不提交表单。然后我想从wicket示例页面中得到启发:但当我点击源代码时,我只看到返
add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(10)) {
private static final long serialVersionUID = 1;
});
其中只刷新页面不提交表单。然后我想从wicket示例页面中得到启发:但当我点击源代码时,我只看到返回的URL:InternalError
更新:
我尝试调用简单的javascript,然后从js提交表单:
add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(10)) {
private static final long serialVersionUID = 1L;
@Override
protected void onPostProcessTarget(AjaxRequestTarget target) {
target.appendJavaScript("alert('hello');");
}
});
但是,要设置一个间隔并定期运行一些代码,如果没有成功,您可以使用JQuery:
$(document).ready(function(){
//ajax code here
myVar = setInterval(someCode, 10000);
});
那是你想要的吗
编辑
刚刚意识到。。。set Interval实际上不是JQuery函数
//use this to stop it
clearInterval(myVar);
您将需要的组合来引发触发事件的事件。我没有尝试过这个,但从我的wicket经验和这两种行为的JavaDocs来看,它应该是有效的 因为似乎需要一些演示代码 免责声明:这是由上述两个类的复制粘贴在几分钟内完成的。因此,这不是好代码,不是经过测试的代码,也不是我在没有对其进行仔细研究的情况下投入生产的任何东西。但它似乎奏效了 首先,您需要组合行为:
public abstract class AjaxTimerFormSubmitBehavior extends AbstractAjaxTimerBehavior {
/**
* should never be accessed directly (thus the __ cause its overkill to
* create a super class), instead always use #getForm()
*/
private Form<?> __form;
private boolean defaultProcessing = true;
/**
* @param updateInterval
*/
public AjaxTimerFormSubmitBehavior(Duration updateInterval) {
this(null, updateInterval);
}
public AjaxTimerFormSubmitBehavior(Form<?> form, Duration updateInterval) {
super(updateInterval);
__form = form;
if (form != null) {
form.setOutputMarkupId(true);
}
}
@Override
protected void onTimer(final AjaxRequestTarget target) {
getForm().getRootForm().onFormSubmitted(new IFormSubmitter() {
public Form<?> getForm() {
return AjaxTimerFormSubmitBehavior.this.getForm();
}
public boolean getDefaultFormProcessing() {
return AjaxTimerFormSubmitBehavior.this.getDefaultProcessing();
}
public void onSubmit() {
AjaxTimerFormSubmitBehavior.this.onSubmit(target);
}
public void onError() {
AjaxTimerFormSubmitBehavior.this.onError(target);
}
});
}
/**
* @return Form that will be submitted by this behavior
*/
public final Form<?> getForm() {
if (__form == null) {
__form = findForm();
if (__form == null) {
throw new IllegalStateException(
"form was not specified in the constructor and cannot "
+ "be found in the hierarchy of the component this behavior "
+ "is attached to: Component="
+ getComponent().toString(false));
}
}
return __form;
}
/**
* @see Button#getDefaultFormProcessing()
*
* @return {@code true} for default processing
*/
public boolean getDefaultProcessing() {
return defaultProcessing;
}
/**
* Finds form that will be submitted
*
* @return form to submit or {@code null} if none found
*/
protected Form<?> findForm() {
// try to find form in the hierarchy of owning component
Component component = getComponent();
if (component instanceof Form<?>) {
return (Form<?>) component;
} else {
return component.findParent(Form.class);
}
}
/**
* Listener method that is invoked after the form has been submitted and
* processed without errors
*
* @param target
*/
protected abstract void onSubmit(AjaxRequestTarget target);
/**
* Listener method invoked when the form has been processed and errors
* occurred
*
* @param target
*/
protected abstract void onError(AjaxRequestTarget target);
}
公共抽象类AjaxTimerFormSubmitBehavior扩展了AbstractAjaxTimerBehavior{
/**
*不应直接访问(因此_uu会导致其过杀
*创建一个超级类),而不是始终使用#getForm()
*/
私人表格_;表格;;
私有布尔值defaultProcessing=true;
/**
*@param updateInterval
*/
公共AjaxTimerFormSubmitBehavior(持续时间更新间隔){
这(null,updateInterval);
}
公共AjaxTimerFormSubmitBehavior(表单、持续时间更新间隔){
super(updateInterval);
__形式=形式;
if(form!=null){
form.setOutputMarkupId(true);
}
}
@凌驾
受保护的void onTimer(最终AjaxRequestTarget目标){
getForm().getRootForm().onFormSubmitted(新的IFormSubmitter()){
公共表单getForm(){
返回AjaxTimerFormSubmitBehavior.this.getForm();
}
公共布尔getDefaultFormProcessing(){
返回AjaxTimerFormSubmitBehavior.this.getDefaultProcessing();
}
提交时公共无效(){
AjaxTimerFormSubmitBehavior.this.onSubmit(目标);
}
公开无效{
AjaxTimerFormSubmitBehavior.this.onError(目标);
}
});
}
/**
*@此行为将提交的返回表单
*/
公共最终表单getForm(){
如果(_form==null){
__form=findForm();
如果(_form==null){
抛出新的非法状态异常(
“未在构造函数中指定表单,因此无法”
+“无法在组件的层次结构中找到此行为”
+“已附加到:组件=”
+getComponent().toString(false));
}
}
返回表格;
}
/**
*@see按钮#getDefaultFormProcessing()
*
*@return{@code true}用于默认处理
*/
公共布尔getDefaultProcessing(){
退货处理;
}
/**
*查找将要提交的表单
*
*@return form to submit或{@code null}如果没有找到
*/
受保护的表单findForm(){
//尝试在所属组件的层次结构中查找表单
Component=getComponent();
if(窗体的组件实例){
返回(表单)组件;
}否则{
返回component.findParent(Form.class);
}
}
/**
*提交表单后调用的侦听器方法,并且
*处理无误
*
*@param目标
*/
提交时受保护的抽象无效(AjaxRequestTarget);
/**
*当窗体已被处理且出现错误时调用的侦听器方法
*发生
*
*@param目标
*/
受保护的抽象void onError(AjaxRequestTarget目标);
}
然后你必须使用它
public class HomePage extends WebPage {
private static final long serialVersionUID = 1L;
private Integer counter = 0;
public HomePage(final PageParameters parameters) {
final Label label = new Label("counter", new PropertyModel<Integer>(this, "counter"));
label.setOutputMarkupId(true);
add(label);
Form form = new Form("form");
form.add(new AjaxTimerFormSubmitBehavior(form, Duration.seconds(10)) {
@Override
protected void onSubmit(AjaxRequestTarget target) {
counter++;
target.add(label);
}
@Override
protected void onError(AjaxRequestTarget target) {
// TODO Auto-generated method stub
}
});
add(form);
}
public Integer getCounter() {
return counter;
}
public void setCounter(Integer counter) {
this.counter = counter;
}
}
公共类主页扩展网页{
私有静态最终长serialVersionUID=1L;
专用整数计数器=0;
公共主页(最终页面参数){
最终标签=新标签(“计数器”,新属性模型(“计数器”);
label.setOutputMarkupId(true);
添加(标签);
表格=新表格(“表格”);
添加(新的AjaxTimerFormSubmitBehavior(表单,持续时间。秒(10)){
@凌驾
提交时受保护的void(AjaxRequestTarget目标){
计数器++;
添加(标签);
}
@凌驾
受保护的void onError(AjaxRequestTarget目标){
//TODO自动生成的方法存根
}
});
添加(表格);
}
公共整数getCounter(){
返回计数器;
}
公共无效设置计数器(整数计数器){
this.counter=计数器;
}
}
我希望这会给你一个想法
是一个小型的war演示文件。只需下载,扔掉你最喜欢的应用程序容器,看看它能做什么。它也包含源代码。我认为您做了正确的事情,我认为您将行为附加到了一个本身不会改变的组件上。您需要在
onPostProcessTarget
方法中写入更新逻辑,并将要刷新的组件添加到ajaxRequestTarget。查看Firebug
或Chrome
的网络选项卡,查看该行为是否触发对服务器的调用
如果不是这样,则可能是脚本的前提条件失败(标记id更改可以做到这一点)