Spring MVC 注解

URL路径映射

  • 这三个等价
@RequestMapping("/hello")
@RequestMapping("/hello.html")
@RequestMapping("hello")
  • 可以配置多级路径
@RequestMapping("/test/hello.html")
@RequestMapping("test/hello")
  • 同一个方法可以配置多个路径
@RequestMapping(value={"/test/hello.html","wow.html"})
@RequestMapping(path={"/test/hello.html","haha"})
  • url映射可以使用通配* ,* 这里可以填任意字符用来表示一级目录,**匹配多级目录,?匹配一个字符(示例中可填任意字符)
@RequestMapping("/*/hello.html")
@RequestMapping("/**/hello.html")
@RequestMapping("/**/*hello?")

窄化请求映射

@RequestMapping("/user")
@Controller
public class SecondController{}

//等价于
@RequestMapping("/user")
@Controller
public class SecondController{}

这俩的作用是在方法的url路径之前增加/user

请求方法限定

@ RequestMapping(path = “/haha2”,method = RequestMethod.POST)
只处理POST请求

其他

  • params的使用
    @RequestMapping(path = "/haha2",method = RequestMethod.GET,params = {"id"})
// http://localhost:8080/haha2?id

    @RequestMapping(path = "/haha2",method = RequestMethod.GET,params = {"id!=123"})
//http://localhost:8080/haha2?id=1234

    @RequestMapping(path = "/haha2",method = RequestMethod.GET,params = {"id=123","age=1234"})
//http://localhost:8080/haha2?id=123&age=1234
  • 请求头需要满足的条件
    @RequestMapping(path = "/haha2",method = RequestMethod.GET,params = {"id=123","age=1234"},headers = "localhost")

Controller方法返回值

返回ModelAndView
  • 我们之前的就是返回的ModelAndView
    @RequestMapping(path = "/haha2",method = RequestMethod.GET,params = {"id=123","age=1234"},headers = "localhost")
    public ModelAndView handleRequest(HttpServletRequest request) throws Exception {
        //模型和视图
        ModelAndView mv = new ModelAndView();
        User user = new User();
        user.setUsername("苟利国家生死以");
        user.setPassword("岂因祸福避趋之");
        //把对象放进去了
        mv.addObject("user",user);
        //把视图放进去了
        mv.setViewName("/index.jsp");
        return mv;
    }
void 方式(最原始的方式)
@RequestMapping(path = "/haha3")
    public void test(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("test");
        User user = new User();
        user.setId(1);
        user.setUsername("haha");
        user.setPassword("12345");
        request.setAttribute("user",user);
        request.getRequestDispatcher("WEB-INF/index.jsp").forward(request,response);
    }
返回字符串
  • 表示以web根目录作为起点的路径(绝对路径)
    @RequestMapping(path = "/haha4")
    public String test2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("test2");
        User user = new User();
        user.setId(1);
        user.setUsername("test2");
        user.setPassword("12345");
        request.setAttribute("user",user);
        return "/WEB-INF/index.jsp";
    }
  • 表示以当前目录作为起点的路径(相对路径)
    @RequestMapping(path = "/haha5")
    public String test3(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("test3");
        User user = new User();
        user.setId(1);
        user.setUsername("test3");
        user.setPassword("12345");
        request.setAttribute("user",user);
        return "index.jsp";
    }

可以用return返回WEB-INF下的资源

逻辑视图

返回的字符串是物理视图的一部分,完整的物理视图这样:

  • 物理视图名
    mv.setViewName(“/WEB-INF/userlist.jsp”);
  • 页面解析器注入两个参数
<!--页面解析器-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
  • 之后返回的视图名称——逻辑视图名:
    mv.setViewName(“userlist”);
返回字符串的时候model的返回
  • request.setAttribute(“user”,user);
  • model.addAttribute(“user”,user);
返回字符串的时候可以转发(默认实现)
  • return “forword:/haha/index.jsp”;
返回字符串的时候可以重定向
  • return “redirect:/haha/index.jsp”;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值