好了,今天我们来学习的是wicket的一个登陆和跳转例子,使用的注解的方式。最后做成了如下的界面。有点简陋。
对应的html页面如下所示。
<!DOCTYPE html>
<html lang="en" xmlns:wicket="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>Select one of the following links</h2>
<wicket:enclosure>
<span>You are signed in as:</span>
<span wicket:id="username"></span>
<br />
<br />
</wicket:enclosure>
<wicket:link>
<a href="SignInPage.html">Link to sign in page</a>
<br />
<a href="AdminOnlyPage.html">
Link to admin-only page
</a>
<br />
</wicket:link>
<br />
<a wicket:id="logOut">Log out.</a>
</body>
</html>
在wicket中,对应着一个java文件,如下所示。
public class HomePage extends WebPage {
public HomePage() {
add(new Link<Void>("logOut") {
@Override
public void onClick() {
AuthenticatedWebSession.get().invalidate();
setResponsePage(getApplication().getHomePage());
}
});
add(new Label("username", new PropertyModel(this, "session.username")){
@Override
protected void onConfigure() {
super.onConfigure();
setVisible(getDefaultModelObject() != null);
}
});
}
}
当点击到注册界面后,跳转到下图的界面。
注意,这个界面的html代码如下所示。
<html xmlns:wicket="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="" wicket:id="form">
<legend>
Type the same value for username and password to
authenticate. Use 'superuser' to sign in as ADMIN.
</legend>
<br />
<div style="display: table;border: solid 1px;padding:5px">
<div style="display: table-row;">
<div style="display: table-cell;">Username:</div>
<div style="display: table-cell;">
<input type="text" wicket:id="username" />
</div>
</div>
<div style="display: table-row;">
<div style="display: table-cell;">Password:</div>
<div style="display: table-cell;">
<input type="password" wicket:id="password" />
</div>
</div>
</div>
<input type="submit" value="Submit" />
<div wicket:id="feedbackPanel"></div>
</form>
</body>
</html>
对应的java代码如下所示。注意SignInpae类属性与form表单中数据字段的一一对应关系。
public class SignInPage extends WebPage {
private String username;
private String password;
public SignInPage() {
StatelessForm form = new StatelessForm("form"){
@Override
protected void onSubmit() {
if(Strings.isEmpty(username) || Strings.isEmpty(password))
return;
boolean authResult = AuthenticatedWebSession.get().signIn(username, password);
if(authResult){
continueToOriginalDestination();
setResponsePage(Application.get().getHomePage());
}else{
error("Username and password are not equal!");
}
}
};
form.setDefaultModel(new CompoundPropertyModel(this));
form.add(new TextField("username"));
form.add(new PasswordTextField("password"));
form.add(new FeedbackPanel("feedbackPanel"));
add(form);
}
}
还有一个AdminOnlyPage,我们来看一下界面
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>
Hi! If you are reading this message it means that you are
the admin.
</h2>
<a wicket:id="goToHomePage">Go back to the home page</a>
</body>
</html>
对应的java代码如下所示。
@AuthorizeInstantiation("ADMIN")
public class AdminOnlyPage extends WebPage {
@Override
protected void onInitialize() {
super.onInitialize();
add(new Link<Void>("goToHomePage") {
@Override
public void onClick() {
setResponsePage(getApplication().getHomePage());
}
});
}
}
首先来看一下登陆的过程。
public class BasicAuthenticationSession extends AuthenticatedWebSession {
private String username;
public BasicAuthenticationSession(Request request) {
super(request);
}
@Override
protected boolean authenticate(String username, String password) {
boolean authResult = username.equals(password);
if(authResult)
this.username = username;
return authResult;
}
@Override
public Roles getRoles() {
Roles resultRoles = new Roles();
if(isSignedIn())
resultRoles.add("SIGNED_IN");
if(username!= null && username.equals("superuser"))
resultRoles.add(Roles.ADMIN);
return resultRoles;
}
@Override
public void signOut() {
super.signOut();
username = null;
}
}
在BasicAuthenticationSession中,我们来打一下debug试一下。我们重写了自己的验证方式,那就是判断用户名和密码是否相等。
当密码和用户名相同后,就返回到homepage界面。