package org.xmx0632.deliciousfruit.web.admin; import java.util.List; import java.util.Locale; import java.util.Map; import javax.servlet.ServletRequest; import javax.validation.Valid; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.data.domain.Page; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import org.springside.modules.web.Servlets; import org.xmx0632.deliciousfruit.entity.FruitCategory; import org.xmx0632.deliciousfruit.entity.FruitSubcategory; import org.xmx0632.deliciousfruit.global.ConfigConstant; import org.xmx0632.deliciousfruit.service.ConfigService; import org.xmx0632.deliciousfruit.service.FruitCategoryService; import org.xmx0632.deliciousfruit.service.FruitSubcategoryService; import com.google.common.collect.Maps; @Controller @RequestMapping(value = "/admin/fruitSubcategory") public class FruitSubcategoryController { private static Logger log = LoggerFactory .getLogger(FruitSubcategoryController.class); @Autowired private ResourceBundleMessageSource messageSource; private static Map<String, String> sortTypes = null; private Map<String, String> getSortTypes(Locale locale) { if (sortTypes == null) { sortTypes = Maps.newLinkedHashMap(); sortTypes.put("auto", messageSource.getMessage("common.auto", null, locale)); sortTypes.put("fruitType", messageSource.getMessage( "fruitSubcategory.fruitType", null, locale)); } return sortTypes; } @Autowired private FruitSubcategoryService fruitSubcategoryService; @Autowired private FruitCategoryService fruitCategoryService; @Autowired private ConfigService configService; @RequestMapping(method = RequestMethod.GET) public String list( @RequestParam(value = "sortType", defaultValue = "auto") String sortType, @RequestParam(value = "page", defaultValue = "1") int pageNumber, Model model, ServletRequest request) { Map<String, Object> searchParams = Servlets.getParametersStartingWith( request, "search_"); Page<FruitSubcategory> fruitSubcategorys = fruitSubcategoryService .getFruitSubcategorys(searchParams, pageNumber, configService.getIntByName(ConfigConstant.PAGE_SIZE), sortType); model.addAttribute("fruitSubcategorys", fruitSubcategorys); model.addAttribute("sortType", sortType); model.addAttribute("sortTypes", getSortTypes(request.getLocale())); model.addAttribute("searchParams", Servlets .encodeParameterStringWithPrefix(searchParams, "search_")); return "admin/fruitSubcategoryList"; } @RequestMapping(value = "listAll/{fruitCategoryId}", method = RequestMethod.GET) public String listAll( @PathVariable("fruitCategoryId") Long fruitCategoryId, Model model, ServletRequest request, RedirectAttributes redirectAttributes) { List<FruitSubcategory> list = fruitSubcategoryService .getAllUnSelectedFruitSubcategory(); model.addAttribute("fruitSubcategorys", list); model.addAttribute("fruitCategoryId", fruitCategoryId); return "admin/fruitSubcategorySelectList"; } @RequestMapping(value = "bind", method = RequestMethod.POST) public String bind( @RequestParam("fruitCategoryId") Long fruitCategoryId, @RequestParam(value = "checkedSubCategoryList") List<Long> checkedSubCategoryList, RedirectAttributes redirectAttributes) { FruitCategory fruitCategory = fruitCategoryService .getFruitCategory(fruitCategoryId); for (Long subCategoryId : checkedSubCategoryList) { FruitSubcategory fruitSubcategory = fruitSubcategoryService .getFruitSubcategory(subCategoryId); fruitSubcategory.setFruitCategory(fruitCategory); fruitSubcategoryService.saveFruitSubcategory(fruitSubcategory); } redirectAttributes.addFlashAttribute("message", "添加分类到 [" + fruitCategory.getName() + "]成功"); return "redirect:/admin/fruitCategory/update/" + fruitCategoryId; } @RequestMapping(value = "unbind/{fruitCategoryId}/{fruitSubCategoryId}", method = RequestMethod.GET) public String unbind(@PathVariable("fruitCategoryId") Long fruitCategoryId, @PathVariable("fruitSubCategoryId") Long fruitSubCategoryId, RedirectAttributes redirectAttributes) { FruitSubcategory fruitSubcategory = fruitSubcategoryService .getFruitSubcategory(fruitSubCategoryId); fruitSubcategory.setFruitCategory(null); log.debug("fruitSubcategory:{}", fruitSubcategory); fruitSubcategoryService.saveFruitSubcategory(fruitSubcategory); redirectAttributes.addFlashAttribute("message", "解除绑定成功"); return "redirect:/admin/fruitCategory/update/" + fruitCategoryId; } @RequestMapping(value = "create", method = RequestMethod.GET) public String createForm(Model model) { FruitSubcategory fsc = new FruitSubcategory(); model.addAttribute("fruitSubcategory", fsc); model.addAttribute("action", "create"); return "admin/fruitSubcategoryForm"; } @RequestMapping(value = "create", method = RequestMethod.POST) public String create(@Valid FruitSubcategory newFruitSubcategory, RedirectAttributes redirectAttributes) { log.debug("newFruitSubcategory:{}", newFruitSubcategory); fruitSubcategoryService.saveFruitSubcategory(newFruitSubcategory); redirectAttributes.addFlashAttribute("message", "创建fruitSubcategory成功"); return "redirect:/admin/fruitSubcategory"; } @RequestMapping(value = "update/{id}", method = RequestMethod.GET) public String updateForm(@PathVariable("id") Long id, Model model) { model.addAttribute("fruitSubcategory", fruitSubcategoryService.getFruitSubcategory(id)); model.addAttribute("action", "update"); return "admin/fruitSubcategoryForm"; } @RequestMapping(value = "update", method = RequestMethod.POST) public String update( @Valid @ModelAttribute("preloadFruitSubcategory") FruitSubcategory fruitSubcategory, RedirectAttributes redirectAttributes) { fruitSubcategoryService.saveFruitSubcategory(fruitSubcategory); redirectAttributes.addFlashAttribute("message", "更新fruitSubcategory成功"); return "redirect:/admin/fruitSubcategory"; } @RequestMapping(value = "delete/{id}") public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) { fruitSubcategoryService.deleteFruitSubcategory(id); redirectAttributes.addFlashAttribute("message", "删除fruitSubcategory成功"); return "redirect:/admin/fruitSubcategory"; } /** * 使用@ModelAttribute, 实现Struts2 * Preparable二次部分绑定的效果,先根据form的id从数据库查出FruitSubcategory对象 * ,再把Form提交的内容绑定到该对象上. 因为仅update()方法的form中有id属性,因此本方法在该方法中执行. */ @ModelAttribute("preloadFruitSubcategory") public FruitSubcategory getFruitSubcategory( @RequestParam(value = "id", required = false) Long id) { if (id != null) { return fruitSubcategoryService.getFruitSubcategory(id); } return null; } }