springmvc03Controller和RequestMapping

本文探讨了两种Controller实现方式:通过继承Controller接口和使用@Controller注解配合@RequestMapping。讲解了两者如何映射URL并区分方法直接访问与类路径的配置。

Controller两种配置

1.继承Controller接口

public class ControllerTest01 implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","Controller");
        mv.setViewName("test");
        return mv;
    }
}

2.注释@Controller

@Controller
public class ControllerTest02 {
    @RequestMapping("/t1")
    public String test02(Model model){
        model.addAttribute("msg","Controller02");
        return "test";
    }
}

RequestMapping

可以放在类上或者方法上,放在方法上就是可以直接访问的地址,如果放在类上说明这个类中的方法都要先加上类的地址,然后再走方法上的地址。

@Controller
@RequestMapping("test")
public class ControllerTest02 {
    @RequestMapping("/t1")
    public String test02(Model model){
        model.addAttribute("msg","Controller02");
        return "test";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值