我们在代码中使用@ModelAttribute的地方?

时间:2015-07-15 04:18:05

标签: java spring spring-mvc

@Controller
public class CustomerController {
    @Autowired
    private CustomerBS customerBS;

    @Autowired
    private CustomerExtService customerExt;

    /**
    * This method fetches CustomerCategory details
    * @Method getCustomerCategory 
    * @param  request
    * @return categoryMap
    * @throws BusinessException
    */
    @ModelAttribute("categoryMap")  
    public  Map<Integer, String> getCustomerCategory(HttpServletRequest request) throws BusinessException{
        HttpSession session=request.getSession();
        UserDetails userDetails=(UserDetails) session.getAttribute(CollaborationConstants.USERSESSION);
        Map<Integer, String> categoryMap= customerBS.fetchCategoryList(userDetails.getTenantId(),userDetails.getLocaleId());
        return categoryMap;
    }

    /**
       * This method fetches CustomerType details
       * @Method getCustomerType 
       * @param  request
       * @return customer type
       * @throws BusinessException
     */
    @ModelAttribute(value = "custType")
    public Map<Integer, String> getCustomerType(HttpServletRequest request)
            throws BusinessException {
        HttpSession session = request.getSession();
        UserDetails userDetails = (UserDetails) session
                .getAttribute(CollaborationConstants.USERSESSION);
        Map<Integer, String> customertype = customerBS
                .fetchCustomerType(userDetails.getTenantId(), userDetails.getLocaleId());
        return customertype;
    }

    /**
     * This method fetches Customer SearchView details
     * @Method customerSearchView
     * @param  customer
     * @throws BusinessException
     * @return search customer
     */
    @PreAuthorize("hasAnyRole('CUSTOMER MANAGEMENT_SEARCH')")
    @RequestMapping(value="/searchCustomerView.do")
    public String customerSearchView(@ModelAttribute("customer") Customer customer) throws BusinessException{
        return "customer/searchcustomer";
    }

    /**
     * This method  Create Retail CustomerView 
     * @Method createRetailCustomerView
     * @param  customer
     * @param  categoryName
     * @param  categoryId
     * @return ModelAndView
     */
    @PreAuthorize("hasRole('CUSTOMER MANAGEMENT_CREATE')")
    @RequestMapping(value="/createCustomerView.do")
    public ModelAndView createRetailCustomerView(@ModelAttribute("customer") Customer customer, @RequestParam String categoryName,@RequestParam String categoryId){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("categoryName", categoryName);
        modelAndView.addObject("categoryId", categoryId);
        modelAndView.addObject("addressList", new ArrayList());
        modelAndView.setViewName("customer/createcustomer");
        return modelAndView;
    }

    /**
     * This method  fetch List of Customers details
     * @Method fetchCustomerList
     * @param customer
     * @param customerCode
     * @param customerName
     * @param categoryId
     * @param session
     * @throws CustomerBusinessException
     * @return List of Customers
     */
    @PreAuthorize("hasAnyRole('CUSTOMER MANAGEMENT_SEARCH','CUSTOMER MANAGEMENT_VIEW')")
    @RequestMapping(value = "fetchCustomerList.do")
        public @ResponseBody JsonResult fetchCustomerList(@ModelAttribute("customer") Customer customer,@RequestParam("custCode") String customerCode, @RequestParam("custName") String customerName, @RequestParam("categoryId") Integer categoryId, HttpSession session) throws CustomerBusinessException {
        UserDetails userDetails = (UserDetails) session.getAttribute(CollaborationConstants.USERSESSION);
            JsonResult jsonResult = new JsonResult();
           List<Customer> customerList= customerBS.fetchCustomerList( customer ,customerCode, customerName,userDetails,categoryId, 0, -1);
           jsonResult.setResult(customerList);
            jsonResult.setPage(customer.getPage());
            jsonResult.setTotal(customer.getTotalRows());
            jsonResult.setRecords(customer.getTotalRecords());
            // jsonResult.setRows(saemployee.getRows());

            jsonResult.setResult(customerList);
            return  jsonResult;
     }

    /**
     * This method  fetch List of Country details
     * @Method fetchCountryList
     * @param  session
     * @throws CustomerBusinessException
     * @return List of Country
     */
    @PreAuthorize("hasAnyRole('CUSTOMER MANAGEMENT_CREATE','CUSTOMER MANAGEMENT_EDIT')")
     @RequestMapping(value="fetchCountries.do")
     public @ResponseBody JsonResult fetchCountryList(HttpSession session) throws CustomerBusinessException{
         JsonResult jsonResult = new JsonResult();
        UserDetails userDetails = (UserDetails) session.getAttribute(CollaborationConstants.USERSESSION);
        List<Country> countryList= customerBS.fetchCountryList(userDetails.getTenantId(), userDetails.getLocaleId());
        jsonResult.setResult(countryList);
        return jsonResult;
     }


    /**
     * This method  fetch List of AddressType details
     * @Method fetchAddressTypeList
     * @param  request
     * @throws CustomerBusinessException
     * @return List of AddressType
     */
    @PreAuthorize("hasAnyRole('CUSTOMER MANAGEMENT_CREATE','CUSTOMER MANAGEMENT_EDIT')")
    @RequestMapping(value="fetchAddressTypeList.do")
     public @ResponseBody JsonResult fetchAddressTypeList(HttpServletRequest request) throws CustomerBusinessException{
         JsonResult jsonResult = new JsonResult();
         HttpSession session = request.getSession();
         UserDetails userDetails = (UserDetails) session
                    .getAttribute(CollaborationConstants.USERSESSION);
        List<AddressType> addressTypeList= customerBS.fetchAddressTypeList(userDetails.getTenantId(), userDetails.getLocaleId());
        jsonResult.setResult(addressTypeList);
        return jsonResult;
     }


    /**
     * This method View Customer Details 
     * @Method viewCustomerDetails
     * @param  customerId
     * @param  categoryName
     * @param  request
     * @throws BusinessException
     * @return ModelAndView
     */
    @PreAuthorize("hasAnyRole('CUSTOMER MANAGEMENT_VIEW','CUSTOMER MANAGEMENT_EDIT')")
    @RequestMapping(value="viewCustomerDetails.do")
    public ModelAndView viewCustomerDetails(@RequestParam("custId") Integer customerId, @RequestParam("categoryName") String categoryName, HttpServletRequest request) throws BusinessException{
        HttpSession session = request.getSession();
        UserDetails userDetails = (UserDetails) session
                .getAttribute(CollaborationConstants.USERSESSION);
        Map<String, Integer> alignmentList = customerExt.fetchAlgmntTemplates(userDetails, customerId);
        Customer customerDetails=customerBS.fetchCustomerDetails(customerId,categoryName,0,-1);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("customer", customerDetails);
        modelAndView.addObject("alignmentList", alignmentList);
        if(customerDetails.getAddressList()!=null)
        {
        modelAndView.addObject("addressList", JSONUtil.toJSON(customerDetails.getAddressList()));
        }
        else
        {
            modelAndView.addObject("addressList",new ArrayList());
        }
        modelAndView.setViewName("customer/viewcustomerwithalignment");
        return modelAndView;
    }

    /**
     * This method fetch Customer Details
     * @Method fetchCustomerDetails
     * @param  customerId
     * @param  categoryName
     * @param  request
     * @throws BusinessException
     * @return ModelAndView
     */
    @PreAuthorize("hasRole('CUSTOMER MANAGEMENT_EDIT')")
    @RequestMapping(value = "/editCustomerView.do", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView fetchCustomerDetails(@RequestParam("custId") Integer customerId, @RequestParam("categoryName") String categoryName, HttpServletRequest request)
            throws BusinessException {
        HttpSession session = request.getSession();
        UserDetails userDetails = (UserDetails) session
                .getAttribute(CollaborationConstants.USERSESSION);
        Map<String, Integer> alignmentList = customerExt.fetchAlgmntTemplates(userDetails, customerId);
        Customer customerDetails=customerBS.fetchCustomerDetails(customerId,categoryName,0,-1);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("customer", customerDetails);
        modelAndView.addObject("alignmentList", alignmentList);
        if(customerDetails.getAddressList()!=null)
        {
        modelAndView.addObject("addressList", JSONUtil.toJSON(customerDetails.getAddressList()));
        }
        else
        {
            modelAndView.addObject("addressList",new ArrayList());
        }
        modelAndView.setViewName("customer/modifycustomer");
        return modelAndView;
    }


    /**
      * This method Add Customer
      * @Method addCustomer
      * @param  customer
      * @param  request
      * @param  session
      * @throws BusinessException
      * @return ModelAndView
      */
     @PreAuthorize("hasRole('CUSTOMER MANAGEMENT_CREATE')")
     @RequestMapping(value = "/addCustomer.do", method = RequestMethod.POST)
    public ModelAndView addCustomer(@ModelAttribute("customer") Customer customer, HttpServletRequest request, HttpSession session)
            throws BusinessException {
        UserDetails userDetails = (UserDetails) session.getAttribute(CollaborationConstants.USERSESSION);
        if((customer.getCustId()==null)){
         customer =customerBS.addCustomer(customer,userDetails);
         ModelAndView modelAndView=new ModelAndView();
         modelAndView.addObject("customer", customer);
         modelAndView.addObject("categoryName", customer.getCategoryName());
         modelAndView.addObject("categoryId", customer.getCategoryId());
         if(customer.getAddressList()!=null)
         {
         modelAndView.addObject("addressList", JSONUtil.toJSON(customer.getAddressList()));

         }else
         {
             modelAndView.addObject("addressList", new ArrayList());     
         }
         if(customer.getCustCodeFlag()!=null && customer.getCustCodeFlag().equals("Y"))
         {
             modelAndView.setViewName("customer/createcustomer");
         }else
          modelAndView.setViewName("customer/viewcustomer");

        return modelAndView;
        }
        else{
            customerBS.updateCustomer(customer,userDetails);
            customer=customerBS.fetchCustomerDetails(customer.getCustId(),customer.getCategoryName(),0,-1);
        }
         ModelAndView modelAndView=new ModelAndView();
         modelAndView.addObject("customer", customer);
         if(customer.getAddressList()!=null)
         {
         modelAndView.addObject("addressList", JSONUtil.toJSON(customer.getAddressList()));
         }else
            {
                modelAndView.addObject("addressList",new ArrayList());
            }
         modelAndView.setViewName("customer/viewcustomer");
        return modelAndView;
    }




}

当我浏览一些代码时,在@ModelAttribute注释中出现了一些混淆。在googeling之后我开始知道为什么使用这个注释并且它被两种类型使用。 但是这里@ModelAttribute("categoryMap")getCustomerCategory()之前的位置。它在调用任何控制器之前被调用但我们在代码中调用该方法的地方我没能弄明白。如果我们没有使用该方法我们的代码然后是什么使用这个方法。这是任何程序员的错误或屏幕后面我们正在使用该方法的其他地方。

我可以根据需要分享其他代码,以防我们知道。

0 个答案:

没有答案