コード例 #1
0
 @SuppressWarnings("unchecked")
 @Override
 public ActionResult execute(WebContext webContext) {
   ResourceBundle messagesBundle = webContext.getMessagesBundle();
   CategoryService categoryService = new CategoryService();
   ActionResult backToPreviousPage = new ActionResult(webContext.getPreviousURI(), true);
   String name = webContext.getParameter(CATEGORY);
   if (name == null || name.isEmpty()) {
     webContext.setAttribute(
         "errorMessage",
         messagesBundle.getString("add-category.message.categoryIsEmpty"),
         Scope.FLASH);
     return backToPreviousPage;
   }
   Category createdCategory = categoryService.createCategory(name);
   // add new category to the list in app context
   if (createdCategory != null) {
     List<Category> categories =
         (List<Category>)
             webContext.getAttribute(ContextListener.CATEGORIES_LIST, Scope.APPLICATION);
     categories.add(createdCategory);
     webContext.setAttribute(
         "successMessage", messagesBundle.getString("add-category.message.success"), Scope.FLASH);
   } else {
     webContext.setAttribute(
         "errorMessage", messagesBundle.getString("add-category.message.exist"), Scope.FLASH);
   }
   return backToPreviousPage;
 }
コード例 #2
0
 protected void setAttributesToFlashScope(WebContext webContext) {
   webContext.setAttribute(CATEGORY_NAME, webContext.getParameter(CATEGORY_NAME), Scope.FLASH);
   webContext.setAttribute(PRODUCT_NAME, webContext.getParameter(PRODUCT_NAME), Scope.FLASH);
   webContext.setAttribute(
       PRODUCT_MANUFACTURER, webContext.getParameter(PRODUCT_MANUFACTURER), Scope.FLASH);
   webContext.setAttribute(PRICE, webContext.getParameter(PRICE), Scope.FLASH);
   webContext.setAttribute(DESCRIPTION, webContext.getParameter(DESCRIPTION), Scope.FLASH);
 }