0
我正在使用Wicket 1.4.9 + Spring 3 + iBatis。我想在使用事務之前獲取會話數據,但是我遇到了一個很大的問題,因爲這些數據放在Wicket Web會話中(此數據在登錄頁面上選擇)。如何使用aop處理每個客戶端的Wicket會話
我正在使用spring aop進行點刪除,但是我無法爲每個客戶端指定切割會話。如何解決這個問題呢。
用戶類實體
package com.app.entity;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private String name;
private String lastname;
private String username;
private String password;
private String crtUser;
private Date crtDate;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCrtUser() {
return crtUser;
}
public void setCrtUser(String crtUser) {
this.crtUser = crtUser;
}
public Date getCrtDate() {
return crtDate;
}
public void setCrtDate(Date crtDate) {
this.crtDate = crtDate;
}
}
Web應用程序類
package com.app.conf;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import com.app.page.UserLoginPage;
public class WebApp extends WebApplication {
@Override
protected void init() {
addComponentInstantiationListener(new SpringComponentInjector(this));
}
@Override
public Class<? extends Page> getHomePage() {
return UserLoginPage.class;
}
@Override
public Session newSession(Request request, Response response) {
return new CustomSession(request);
}
}
自定義會話類
package com.app.conf;
import org.apache.wicket.Request;
import org.apache.wicket.protocol.http.WebSession;
import com.app.entity.User;
public class CustomSession extends WebSession {
private User user;
public CustomSession(Request request) {
super(request);
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
用戶登錄頁面:保持會話時登錄
package com.app.page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import com.app.conf.CustomSession;
import com.app.entity.User;
public class UserLoginPage extends WebPage {
private LoginForm form;
public UserLoginPage() {
form = new LoginForm("form");
add(form);
}
private class LoginForm extends Form{
private User user;
private TextField<String> username;
private PasswordTextField password;
private AjaxButton submitButton;
public LoginForm(String id) {
super(id);
user = new User();
username = new TextField<String>("username",
new PropertyModel<String>(this, "user.username"));
add(username);
password = new PasswordTextField("password" ,
new PropertyModel<String>(this, "user.password"));
add(password);
submitButton = new AjaxButton("submitButton", this) {
@Override
protected void onSubmit(AjaxRequestTarget arg0, Form<?> arg1) {
getCustomSession().setUser(user);
setResponsePage(new UserAddPage());
}
};
add(submitButton);
}
}
public CustomSession getCustomSession(){
return (CustomSession)getSession();
}
}
用戶添加頁面:我想切點上插入服務類方法,並得到會議的附加創建用戶和用戶創建日期實體類
package com.app.page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import com.app.entity.User;
import com.app.service.IUserService;
public class UserAddPage extends WebPage {
@SpringBean private IUserService userService;
private UserAddForm form;
public UserAddPage() {
form = new UserAddForm("form");
add(form);
}
private class UserAddForm extends Form {
private User user;
private TextField<String> name;
private TextField<String> lastname;
private TextField<String> username;
private PasswordTextField password;
private AjaxButton submitButton;
public UserAddForm(String id) {
super(id);
user = new User();
name = new TextField<String>("name",
new PropertyModel<String>(this, "user.name"));
add(name);
lastname = new TextField<String>("lastname" ,
new PropertyModel<String>(this, "user.lastname"));
add(lastname);
username = new TextField<String>("username",
new PropertyModel<String>(this, "user.username"));
add(username);
password = new PasswordTextField("password" ,
new PropertyModel<String>(this, "user.password"));
add(password);
submitButton = new AjaxButton("submitButton", this) {
@Override
protected void onSubmit(AjaxRequestTarget arg0, Form<?> arg1) {
/*
* HOW TO POINT CUT THIS METHOD AND GET SESSION BEFORE INSERT
*/
userService.insert(user);
}
};
add(submitButton);
}
}
}
用戶看點
package com.app.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import com.app.entity.User;
@Aspect
public class UserAspect {
@Pointcut("execution(* com.app.service..*.insert*(..))&&" + "args(user)")
public void userPointcut(User user){}
@Around("userPointcut(user)")
public Object aroundUser(ProceedingJoinPoint pj, User user) throws Throwable{
/*
* How to get Create User from Session
*/
return pj.proceed();
}
}
這很有趣,如果你提供一些更多的信息,我會很樂意給它一個鏡頭。什麼是數據,何時創建,何時創建會話,您需要什麼。你爲什麼不展示你迄今爲止嘗試過的一些示例代碼? – 2010-07-21 15:10:56
幫幫我!爲解決這個問題 – suchai 2010-07-28 13:56:10