Hi ,
Sometime l found it is difficult to do programming . May be because of lack of docs and examples ? or lack some stupid people like me to ask stupid question , hihihi ....
. But now l wish to ask a series basic questions about SWF , hope that clear all my "basic HOW TO" . Hope the authors don't mind ....
Q1.
Is this the right implemetation for SWF ? doing messages and errors that different from spring MVC.
Code:
public class AbstractLibraryAction extends FormAction implements ApplicationContextAware {
private ApplicationContext applicationContext;
private MessageSourceAccessor messageSourceAccessor;
private LibraryFacade library;
private static final String DATE_PATTERN = "yyyy-MM-dd";
public final void setLibrary(LibraryFacade library){
this.library = library;
}
protected final LibraryFacade getLibrary(){
return this.library;
}
public AbstractLibraryAction() {
}
public final void setApplicationContext(ApplicationContext context) throws BeansException {
if (context == null && !isContextRequired()) {
// reset internal context state
this.applicationContext = null;
this.messageSourceAccessor = null;
}
if (this.applicationContext == null) {
// initialize with passed-in context
if (!requiredContextClass().isInstance(context)) {
throw new ApplicationContextException(
"Invalid application context: needs to be of type [" + requiredContextClass().getName() + "]");
}
this.applicationContext = context;
this.messageSourceAccessor = new MessageSourceAccessor(context);
initApplicationContext();
}
else {
// ignore reinitialization if same context passed in
if (this.applicationContext != context) {
throw new ApplicationContextException(
"Cannot reinitialize with different application context: current one is [" +
this.applicationContext + "], passed-in one is [" + context + "]");
}
}
}
protected boolean isContextRequired() {
return true;
}
protected Class requiredContextClass() {
return ApplicationContext.class;
}
protected void initApplicationContext() throws BeansException {
}
public final ApplicationContext getApplicationContext() throws IllegalStateException {
if (this.applicationContext == null && isContextRequired()) {
throw new IllegalStateException(
"ApplicationObjectSupport instance [" + this + "] does not run in an ApplicationContext");
}
return applicationContext;
}
protected final MessageSourceAccessor getMessageSourceAccessor() throws IllegalStateException {
if (this.messageSourceAccessor == null && isContextRequired()) {
throw new IllegalStateException(
"ApplicationObjectSupport instance [" + this + "] does not run in an ApplicationContext");
}
return this.messageSourceAccessor;
}
public final void afterPropertiesSet() {
if (this.library == null) throw new BeanCreationException("Library Facade is required");
initAction();
}
protected void initBinder(RequestContext context, DataBinder binder){
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, false));
binder.registerCustomEditor(String.class, null, new StringTrimmerEditor("\r\n\f", false));
}
}
Code:
public class PublisherCRUDAction extends AbstractLibraryAction {
private static final String PUBLISHER_FORM_OBJECT_NAME = "publisher";
public PublisherCRUDAction() {
setFormObjectName(PUBLISHER_FORM_OBJECT_NAME);
setFormObjectClass(Publisher.class);
setValidator(new PublisherValidator());
}
public Event Save(RequestContext context) throws Exception {
// store publisher
Publisher publisher = (Publisher)context.getRequestScope().get(PUBLISHER_FORM_OBJECT_NAME);
HttpServletRequest request = ((HttpServletRequestEvent)context.getOriginatingEvent()).getRequest();
Locale locale = RequestContextUtils.getLocale(request);
try{
getLibrary().storePublisher(publisher);
request.getSession().setAttribute("message_publisher_added",
getMessageSourceAccessor().getMessage("publisher.added",
new Object[] {publisher.getPublisherName()}, locale));
}
catch(org.springframework.dao.DataIntegrityViolationException eive){
BindException errors = createBinder(context, publisher).getErrors();
errors.rejectValue("publisherName","publisher.name.already.exists",
"Publisher name already exists ...");
exposeFormObjectAndErrors(context, publisher ,errors);
return error();
}
return success();
}
public Event Search(RequestContext context) throws Exception {
// search publisher
HttpServletRequest request = ((HttpServletRequestEvent)context.getOriginatingEvent()).getRequest();
String publisherName = request.getParameter("publisherName");
Locale locale = RequestContextUtils.getLocale(request);
if(publisherName != null){
ArrayList publishers = (ArrayList)getLibrary().findPublishersByName(publisherName);
if (publishers.size() < 1) {
// no subject found
context.getRequestScope().setAttribute("message_no_publisher_found",
getMessageSourceAccessor().getMessage("no.publisher.found", locale));
return success();
}
// multiple subjects found
context.getRequestScope().setAttribute("model_name_publishers", publishers);
return success();
}
return success();
}
}
Q2.
What is the different between the usage of RequestScope and FlowScope ?
sometimes l saw example using RequestScope and sometime FlowScope.
Code:
BirthDate birthDate = (BirthDate)context.getRequestScope().get(BIRTHDATE_FORM_OBJECT_NAME);
Code:
PhoneBookQuery query = (PhoneBookQuery)context.getFlowScope().getRequiredAttribute("query",PhoneBookQuery.class);