SpringMVC之Controller详解

前言

本文主要是讲解在Controller中的开发,主要的知识点有如下:

编码过滤器
使用注解开发
注解@RequestMapping详解
业务方法接收参数
字符串转日期
重定向和转发
返回JSON
SpringMVC过滤编码器

在SpringMVC的控制器中,如果没有对编码进行任何的操作,那么获取到的中文数据是乱码!

即使我们在handle()方法中,使用request对象设置编码也不行!原因也非常简单,我们SpringMVC接收参数是通过控制器中的无参构造方法,再经过handle()方法的object对象来得到具体的参数类型的。

Struts2是使用拦截器来自动帮我们完成中文乱码的问题的。那么SpringMVC作为一个更加强大的框架,肯定也有对应的方法来帮我们完成中文乱码问题!

值得注意的是:该过滤编码器只能解决POST的乱码问题!

我们只需要在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>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
注解开发SpringMVC

我们在快速入门的例子中使用的是XML配置的方式来使用SpringMVC的,SpringMVC也能够支持注解。【个人非常喜欢注解的方式】

我们在使用Action的时候,要么继承着AbstractCommandController类,要么显示使用注解Controller接口。当我们使用了注解以后就不用显示地继承或实现任何类了!

开发流程

使用@Controller这个注解,就表明这是一个SpringMVC的控制器!

@Controller
public  class HelloAction  {
   
    
}

当然了,现在Spring是不知道有这么一个注解的,因此我们需要在配置文件中配置扫描注解

值得注意的是:在配置扫描路径的时候,后面不要加.*,不然扫描不了。

   <!--扫描注解,后面不要加.*-->
    <context:component-scan base-package="zhongfucheng"/>

在控制器中写业务方法

@Controller
public class HelloAction {
   

    /**
     *
     * @RequestMapping 表示只要是/hello.action的请求,就交由该方法处理。当然了.action可以去掉
     * @param model 它和ModelAndView类似,它这个Model就是把数据封装到request对象中,我们就可以获取出来
     * @return 返回跳转的页面【真实路径,就不用配置视图解析器了】
     * @throws Exception
     */
    @RequestMapping(value="/hello.action")

    public String hello(Model model) throws Exception{
   
        System.out.println("HelloAction::hello()");
        model.addAttribute("message","你好");
        return "/index.jsp";
    }
}

跳转到index页面,首页得到对应的值。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  这是我的首页 <br>
  ${message}
  </body>
</html>

当然了,基于注解和基于XML来开发SpringMVC,都是通过映射器、适配器和视图解析器的。 只是映射器、适配器略有不同。但是都是可以省略的!

<!-- 基于注解的映射器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

<!-- 基于注解的适配器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<!-- 视图解析器(可选) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

更新:上边的适配器和映射器只是Spring3.1版本之前使用的、3.1版本之后现在一般用以下的两个

映射器:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping


适配器:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

当然了,这上面两个配置也可以使用mvc:annotation-driven>替代注解处理器和适配器的配置。

RequestMapping

@RequestMapping能够控制请求路径和请求方式!

一个控制器写多个业务方法

到目前为止,我们都是一个控制器写一个业务方法,这肯定是不合理的。我们在Struts2中一个Action就对应多个业务方法了。那么我们在SpringMVC中又怎么写呢???

其实我们可以推理出来,@RequestMapping就是用于配置哪个请求对应哪个业务方法的!

public @interface RequestMapping {
   
    String[] value() default {
   };

    RequestMethod[] method() default {
   };

    String[] params() default {
   };

    String[] headers() default {
   };
}

当我们请求hello.action的时候,处理的业务方法是hello()…当我们请求bye.action的时候,处理的业务方法是bye()

@Controller
public class HelloAction {
   

    /**
     *
     * @RequestMapping 表示只要是/hello.action的请求,就交由该方法处理。当然了.action可以去掉
     * @param model 它和ModelAndView类似,它这个Model就是把数据封装到request对象中,我们就可以获取出来
     * @return 返回跳转的页面【真实路径,就不用配置视图解析器了】
     * @throws Exception
     */
    @RequestMapping(value="/hello.action")
    public String hello(Model model) throws Exception{
   
        System.out.println("HelloAction::hello()");
        model.addAttribute("message","你好");
        return "/index.jsp";
    }
    @RequestMapping(value = "/bye.action")
    public String bye(Model model) throws Exception {
   
        model.addAttribute("message","再见");
        return "/index.jsp";
    }
}
分模块开发

当然了,我们在Struts2常常使用namespace来进行分模块开发,在SpringMVC中我们也可以这样干,并且我们又是使用的是@RequestMapping这个注解!

只要把@RequestMapping这个注解写到类上面去,就代表了分模块。

@Controller
//我们知道,如果是value属性上的注解,我们可以把value省略掉的
@RequestMapping("/zhongfucheng")
public class HelloAction {
   

    /**
     * @param model 它和ModelAndView类似,它这个Model就是把数据封装到request对象中,我们就可以获取出来
     * @return 返回跳转的页面【真实路径,就不用配置视图解析器了】
     * @throws Exception
     * @RequestMapping 表示只要是/hello.action的请求,就交由该方法处理。当然了.action可以去掉
     */
    @RequestMapping(value = "/hello.action")
    public String hello(Model model) throws Exception {
   
        System.out.println("HelloAction::hello()");
        model.addAttribute("message", "你好");
        return "/index.jsp";
    }

    @RequestMapping(value = "/bye.action")
    public String bye(Model model) throws Exception {
   
        model.addAttribute("message", "再见");
        return "/index.jsp";
    }
}

那么我们想要HelloAction该控制器处理我们的请求,访问的地址要么是:http://localhost:8080/zhongfucheng/hello.action,或者要么是http://localhost:8080/zhongfucheng/bye.action

限定某个业务控制方法,只允许GET或POST请求方式访问

我们如果想要限定某个业务控制方法,只允许GET或POST请求方式访问。还是通过@RequestMapping来实现。只要设定它的method属性就行了!

   @RequestMapping(value = "/bye.action",method = RequestMethod.POST)
    public String bye(Model model) throws Exception {
   
        model.addAttribute("message", "再见");
        return "/index.jsp";
    }

当我把业务方法的请求设置为POST以后,我想要通过GET方式来访问该业务方法。就行不通了!

业务方法写入传统web参数

我们的业务方法除了可以写Model这个参数以外,如果有需要我们还可以写request,response等传统Servlet的参数。这是一样可以使用的…

但是呢,我们并不建议使用传统的web参数,因为会耦合

@RequestMapping(method=RequestMethod.POST,value="/register"
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值