SpringMVC(三) RequestMapping修饰类

SpringMVC使用@RequestMapping 注解为控制器指定可以处理哪些URL请求。

可以用于类定义以及方法定义:

  类定义:提供初步的请求映射信息。相对于WEB应用的根目录。

  方法处:提供进一步的细分映射信息。相对于类定义处的URL。若类定义处没有定义,则是相对于根目录。

  如:针对类设置了@RequestMapping("pathclass")注解,针对方法设置了@RequestMapping("method"),则最终调用到方法的url为pathclass/method,完整路径如http://localhost:8080/HelloWorld/pathclass/helloworld.

 

参考如下Controller测试代码:

复制代码
package com.tiekui.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("pathclass")
public class RequestMappingTest {
    private static String SUCCESS="success";
    
    @RequestMapping("helloworld")
    public String hello(){
        System.out.println("hello world from " + getClass());
        return SUCCESS;
    }
}
复制代码

jsp中调用这个方法的参考代码如下,可以将以下代码加在HelloWorld工程中的index.jsp中。

<a href="pathclass/helloworld">Pathclass Hello world Test</a>
posted @ 2018-12-01 16:39  尐鱼儿  阅读(340)  评论(0编辑  收藏  举报