SpringMVC之注解RequestMapping

一、@RequestMaapping的基本介绍

            @RequestMaapping的功能就是将请求和处理请求和处理请求的控制器关联起来,建立映射关系,当DispathcerServlet接收到请求,会从Controller中找对应的方法来处理该请求。

eg:

@Controller
@RequestMapping("/test")
public class TestRequestMappingController {
    @RequestMapping("/hello")
    public String hello(){
        return "success";
    }
}

当浏览器中的请求是url/test,此时服务器端通过dispatcherservlet处理之后从此项目的配置文件中寻找控制器中与之对应的路径。

1、位置

可以放在类上也可以放在方法上:

        放在类上声明就是请求路径的初始信息

        放在方法上声明就是请求路径的具体信息

比如上面的例子,当想要实现hello页面的时候,具体的路径应为:url/test/hello,而不是url/hello。

  <a th:href="@{/test/hello}">测试RequestMapping</a><br/>

2、属性

        该注解中还有几个属性,这里只说说:value、method

        Value:

        其就是通过value属性的值匹配请求地址中的url。相当于

 @RequestMapping(
        value = {"/testRequestMapping", "/test"}
        )

        method:

        与请求中的请求方式进行匹配,当满足时才可以调用对应的方法。

@RequestMapping( value = {"/testRequestMapping", "/test"}, 
                method = {RequestMethod.GET, RequestMethod.POST} )

3、占位符

        之后在SpringMVC中路径都是以RestFul的形式发送:
       
 原始方式:/deleteUser?id=1
 rest方式: /user/delete/1
        即就是不通过“?”来作为路径和属性了分割的标志了。所以对应的映射需要重新设置为如下格式:
        通过@PathVariable注解将请求中的数据赋给形参了。
<a th:href="@{/testRest/1/admin}">测试路径中的占位符-->/testRest</a><br>
@RequestMapping("/testRest/{id}/{username}") 
public String testRest(@PathVariable("id") String id,
                     @PathVariable("username") String username)
{ System.out.println("id:"+id+",username:"+username); return "success"; }

当我们想要从一个页面上直接访问到一个特定的信息中可以用此方法。

比如QQ空间这类项目,在我们的空间中想要访问到指定好友的空间,就需要点击一些图片类的超链接,然而超链接中就有这些数据,我们只需要想办法接收即可。

二、获取请求参数

1、通过ServletAPI获取(老方法)

  <form th:action="@{/param/servletAPI}" method="get">
      用户名:<input type="text" name="username"><br/>
      密码:<input type="password" name="password"><br/>
      <input type="submit" value="登录"><br/>
  </form>
    @RequestMapping("/param/servletAPI")
    public String getParamByServletAPI(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username+",password:"+password);
        return "success";
    }

2、通过控制器方法的形参直接获取        

    @RequestMapping("/param")
    public String getParam(@RequestParam(value = "userName",required = false,defaultValue = "hello") String username, String password){
        System.out.println("username:"+username+",password:"+password);
        return "success";
    }

        最简单的时候,我们都不需要设置@RequestParam这个注释来处理。只将控制器方法中的i形参和发送过来数据的name一致就能匹配上。

        @RequestParam是为了处理方法中的形参和name值不一致的时候,我们手动设置。

        value:请求中的name,具体指要将哪个值赋予方法中的形参。

        Required:指是否需要有值,当为true的时候,若没有值传过来就会报错。

        dafalueValue:是指当没有对应的值在请求中时我们赋予的默认参数,不管required的值时啥。只要没有值,就默认赋值。

3、通过pojo类获取

        这个很简单,只需要请求中的参数和参数名一一匹配即可。

    @RequestMapping("/param/pojo")
    public String getParamByPojo(User user){
        System.out.println(user);
        return "success";
    }

4、处理乱码的问题:

        需要有一个认知:最初只有serlvet的时候,我们都是通过Servlet的API:charactersetEncoding这段代码,放在代码的最初,来设置字符类型。但在SpringMVC下,我们的dispatcherServlet会处理所有的请求然后在通过配置文件扫描到我们对应的控制器中,然后匹配对应的方法。所以请求信息中有参数的时候,如果我们在方法里设置字符类型,已经不起作用了(因为已经接收到了,就差赋值给形参这一步了)。所以我们需要在web.xml配置文件中早早的设置这个字符类型。

        通过过滤器的方式来设置的:

 <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <!--不但会设置请求的编码类型,也会设置响应的编码类型-->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>

三、域对象共享数据

现在只需要处理三个域类型即可,把page删了。

1、请求域:

        方法一:通过ServletAPI

@RequestMapping("/testServletAPI") 
public String testServletAPI(HttpServletRequest request)
{ request.setAttribute("testScope", "hello,servletAPI");
     return "success"; }

        方法二:使用ModelAndView(也是SpringMVC底层的代码)

  @RequestMapping("/test/mav")
    public ModelAndView testMAV(){

        /*
        * modelAndView   包含model和view功能
        * model:向请求域中共享数据
        * view:设置逻辑视图实现页面跳转
        * */
        ModelAndView mav = new ModelAndView();
        //向请求域中共享数据
        mav.addObject("testRequestScope","hello.modelandView");
        //设置逻辑视图
        mav.setViewName("success");
        return mav;
    }

        方法三:使用Model(其实也是方法二的子类(或者是实现类))

    @RequestMapping("/test/model")
    public String testModel(Model model){
        model.addAttribute("testRequestScope","hello,model");
        return "success";
    }

2、Session和Application

        这两个一样,都是通过ServletAPI中的方法即可(老师讲说这个最简便)

@RequestMapping("/testSession") 
public String testSession(HttpSession session)
{ session.setAttribute("testSessionScope", "hello,session"); 
    return "success"; }



@RequestMapping("/testApplication") 
public String testApplication(HttpSession session)
{ 
ServletContext application = session.getServletContext(); application.setAttribute("testApplicationScope", "hello,application");
     return "success"; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值