localhost:8080 /返回状态404 - Spring

时间:2015-10-15 07:51:07

标签: spring spring-mvc

这是我的代码:

@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;
    }

据我所知,当我向localhost发送请求时:8080 /它应该打开localhost:8080 /商家,但它无效。有人有什么建议吗?

2 个答案:

答案 0 :(得分:0)

当您向localhost:8080 /商家发送请求时,将调用您的showMerchantsList方法。这个方法你会再次重定向localhost:8080 /商家。但是如果你想发送请求为localhost:8080 /并将你引导到localhost:8080 /商家,那么你应该创建另一个方法:

@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;
}

当您调用localhost时,此方法会将您重定向到localhost:8080 / merchants:8080 /

答案 1 :(得分:0)

正常方式你应该使用

@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;
    }

我理解你的要求愚蠢的方式:

@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;
}

注意:因为/始终表示为root。