-1

This is my code:

@Controller
@RequestMapping("/")
public class MerchantsController {

    @Autowired
    MerchantsService merchantsService;

    @Autowired
    ProductsService productsService;

    @Autowired
    OrdersService ordersService;

    @RequestMapping(value = "/merchants", method = RequestMethod.GET)
    public ModelAndView showMerchantsList() {
        ModelAndView modelAndView = new ModelAndView("merchantsList");
        List<Merchant> merchants = merchantsService.getMerchantsList();
        for (Merchant merchant : merchants) {
            if(merchant.getOrder_type() == OrderType.NO_ORDERING){
                merchant.setOrderUntil(Time.valueOf("00:00:00"));
            }
        }
        modelAndView.addObject("merchants", merchants);
        return modelAndView;
    }

As I understand when I send request to localhost:8080/ it should open localhost:8080/merchants, but it is not working. Anyone has any suggestions?

fishera
  • 789
  • 2
  • 7
  • 26
  • Are you talking about redirection? There is no redirection if you call `/` , you have to call `/merchants` or if you want redirection redirect from the root to merchants. Redirection: http://stackoverflow.com/questions/4584410/redirect-in-spring-mvc – Ákos Ratku Oct 15 '15 at 07:59
  • Did you tried `localhost:8080/ApplicationContext/merchants`? – Arpit Aggarwal Oct 15 '15 at 08:03
  • can you explain me how to make so that when I send request to localhost:8080/ it opened localhsot:8080/merchants? – fishera Oct 15 '15 at 08:03

2 Answers2

0

Your showMerchantsList method will be called when you send request to localhost:8080/merchants. And this method will you redirect again localhost:8080/merchants. But if you want send request as localhost:8080/ and direct you to localhost:8080/merchants, then you should create another method as this:

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView showMerchantsListWithoutRequestMapping() {
    ModelAndView modelAndView = new ModelAndView("merchantsList");
    List<Merchant> merchants = merchantsService.getMerchantsList();
    for (Merchant merchant : merchants) {
        if(merchant.getOrder_type() == OrderType.NO_ORDERING){
            merchant.setOrderUntil(Time.valueOf("00:00:00"));
        }
    }
    modelAndView.addObject("merchants", merchants);
    return modelAndView;
}

This method will redirect you to localhost:8080/merchants, when you called localhost:8080/

Burak Keceli
  • 933
  • 1
  • 15
  • 31
0

Normal way you should use

@Controller
public class MerchantsController {

    @Autowired
    MerchantsService merchantsService;

    @Autowired
    ProductsService productsService;

    @Autowired
    OrdersService ordersService;

    @RequestMapping(value = "/merchants", method = RequestMethod.GET)
    public ModelAndView showMerchantsList() {
        ModelAndView modelAndView = new ModelAndView("merchantsList");
        List<Merchant> merchants = merchantsService.getMerchantsList();
        for (Merchant merchant : merchants) {
            if(merchant.getOrder_type() == OrderType.NO_ORDERING){
                merchant.setOrderUntil(Time.valueOf("00:00:00"));
            }
        }
        modelAndView.addObject("merchants", merchants);
        return modelAndView;
    }

As i understand your requirement silly way:

@Controller
public class MerchantsController {
@Autowired
MerchantsService merchantsService;

@Autowired
ProductsService productsService;

@Autowired
OrdersService ordersService;

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView showMerchantsList() {
    ModelAndView model=new ModelAndView("redirect:/merchants");
    return model;
}

@RequestMapping(value = "/merchants", method = RequestMethod.GET)
public ModelAndView showMerchantsList() {
    ModelAndView modelAndView = new ModelAndView("merchantsList");
    List<Merchant> merchants = merchantsService.getMerchantsList();
    for (Merchant merchant : merchants) {
        if(merchant.getOrder_type() == OrderType.NO_ORDERING){
            merchant.setOrderUntil(Time.valueOf("00:00:00"));
        }
    }
    modelAndView.addObject("merchants", merchants);
    return modelAndView;
}

Note: Because / always denotes to root.

erhun
  • 3,549
  • 2
  • 35
  • 44