我想从桌面应用程序登录到wicket网页。因此,我想定期检查是否存在某些特定参数。
但是当我运行我的代码时,会抛出以下异常。
Exception in thread "Thread-13" org.apache.wicket.WicketRuntimeException: There is no application attached to current thread Thread-13
这是我的代码:
public class SignIn extends WebPage {
public SignIn()
{
// Create feedback panel and add to page
add(new FeedbackPanel("feedback"));
// Add sign-in form to page
add(new SignInForm("signInForm"));
startLoginChecker(new LoginRunnable());
}
/**
* Sign in form
*/
public final class SignInForm extends Form<Void>
{
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
// El-cheapo model for form
private final ValueMap properties = new ValueMap();
/**
* Constructor
*
* @param id
* id of the form component
*/
public SignInForm(final String id)
{
super(id);
// Attach textfield components that edit properties map model
add(new TextField<String>(USERNAME, new PropertyModel<String>(properties, USERNAME)));
add(new PasswordTextField(PASSWORD, new PropertyModel<String>(properties, PASSWORD)));
}
/**
* @see org.apache.wicket.markup.html.form.Form#onSubmit()
*/
@Override
public final void onSubmit()
{
// Get session info
SignInSession session = getMySession();
// Sign the user in
if (session.signIn(getUsername(), getPassword()))
{
setResponsePage(getApplication().getHomePage());
}
else
{
// Get the error message from the properties file associated with the Component
String errmsg = getString("loginError", null, "Unable to sign you in");
// Register the error message with the feedback panel
error(errmsg);
}
}
/**
* @return
*/
private String getPassword()
{
return properties.getString(PASSWORD);
}
/**
* @return
*/
private String getUsername()
{
return properties.getString(USERNAME);
}
/**
* @return
*/
private SignInSession getMySession()
{
return (SignInSession)getSession();
}
}
public void startLoginChecker(Runnable runnable) {
Thread loginThread = new Thread(runnable);
loginThread.start();
}
private class LoginRunnable implements Runnable {
private int checkLoginPeriod = 1000; //1sec in milliseconds
@Override
public void run() {
try {
Thread.sleep(checkLoginPeriod);
} catch (InterruptedException e) {
e.printStackTrace();
}
final SignInSession session = (SignInSession)getSession();
while(!session.isSignedIn()) {
if (session.signIn("wicket", "wicket"))
{
setResponsePage(getApplication().getHomePage());
} else
{
// Get the error message from the properties file associated with the Component
String errmsg = getString("loginError", null, "Unable to sign you in");
// Register the error message with the feedback panel
error(errmsg);
}
}
}
}
}
如何避免此问题并定期从线程中检查多个参数。
格拉西亚斯提前。
答案 0 :(得分:2)
我认为您应该使用AjaxSelfUpdatingTimerBehavior
来实现所需的功能。在这种情况下(因为你要移动到另一个页面),我认为使用它的超类更合适:AbstractAjaxTimerBehavior
只需将其添加到您的Page / Panel
即可 add(new AbstractAjaxTimerBehavior(Duration.seconds(1)){
protected void onTimer(AjaxRequestTarget target)
{
// Get session info
SignInSession session = getMySession();
// Sign the user in
if (session.signIn(getUsername(), getPassword()))
{
setResponsePage(getApplication().getHomePage());
}
}
});
答案 1 :(得分:1)
我想是的:
将页面中的(应用程序应用程序)带到线程池,在池中添加if:
if (!Application.exists()) {
ThreadContext.setApplication(application);
}