美文网首页
SpringMVC注解式开发

SpringMVC注解式开发

作者: 叫我不矜持 | 来源:发表于2019-02-19 20:06 被阅读1次

一.搭建环境

  1. 后端控制器无需实现接口,添加相应注解
  2. springmvc中无需注册controller控制器
  3. springmvc配置文件中添加组件扫描,和mvc注解驱动的标签
<!-- 注册组件扫描器 -->        
<context:component-scan base-package="com.bjsxt.handlers"></context:component-scan>

<!-- 注册mvc注解驱动 -->    
<mvc:annotation-driven/>
二.常用注解
  1. @Controller 将当前类交给spring容器去管理
@Controller(value="demoController")  //value为id值,可以通过applicationContext.getBean(..)获取
public class DemoController{

}
  1. @RequestMapping()
    在类上注解作用:起到命名空间的作用,限定访问范围
    在方法上注解的作用:定义handlerMethod的访问路径
@RequestMapping("/registry")//起到命名空间的作用,限定访问范围
public class DemoController{

        @RequestMapping("/register2.do")
        public String demo03(@RequestParam("name1")String name,@RequestParam(defaultValue="0",value="age2")int age,Demo people){
        System.out.println(name+":"+age);
        System.out.println(people);
        return "welcome";
      }
        
        //可以匹配多个访问路径  因为该注解value的类型是String[] 字符串数组  
    @RequestMapping({"/register.do","/hello.do"})
    public ModelAndView hello(){
        ModelAndView mv = new ModelAndView();
        //向mv中添加数据  以下代码相当于request.setAttribute();
        mv.addObject("name", "zhangsan");
        //设置跳转的视图路径
        mv.setViewName("/WEB-INF/welcome.jsp");
        return mv;
    }
}

  1. @RequestParam(value="",defaultValue="b",required=true)
//@RequestParam 矫正形参和接受参数名不一致的问题  同时可以矫正对象类型如@RequestParam List<String> hobby
@RequestMapping("demo03")
    public String demo03(@RequestParam("name1")String name,@RequestParam(defaultValue="0",value="age2")int age,Demo people){
        System.out.println(name+":"+age);
        System.out.println(people);
        return "welcome";
        }

三.处理器方法(handlerMethod)中常用的参数

  1. HttpServletRequest
  2. HttpServletResponse
  3. HttpSession
  4. 用于承载数据的Model、Map、ModelMap
  5. 请求中所携带的请求参数

四.接受请求参数的方法

  1. 逐个接受
@RequestMapping("demo02")
    public String demo02(String name,int age){
        System.out.println(name+":"+age);
        return "welcome";
    }


  1. 对象形式接受
//JSP页面提交的name名称要和对象的属性名一直
@RequestMapping("demo02")
    public String demo02(People people){
        System.out.println(people);
        return "welcome";
    }

  1. 域属性接受
//Demo对象中有一个对象属性people
//此时jsp页面提交的name形式为people.age people.name
@RequestMapping("demo03")
    public String demo03(Demo demo){
        System.out.println(demo);
        return "welcome";
    }

  1. 集合域属性接受
//Demo对象中有一个对象属性List<People> peoples
//此时jsp页面提交的name形式为
//peoples[0].age    peoples[0].name
//peoples[1].age    peoples[1].name
@RequestMapping("demo03")
    public String demo03(Demo demo){
        System.out.println(demo);
        return "welcome";
    }

  1. 集合数组形式的接受
//数组
//jsp页面中的checkbox中的name为hobby,handlerMethod的接受的数据形参名也要为hobby
@RequestMapping("demo03")
    public String demo03(String[] hobby){
        System.out.println(Arrays.toString(hobby));
        return "welcome";
    }


//集合(对集合需要配置@RequestParam注解矫正类型)
//读取checkbox的参数,封装成集合形式
    @RequestMapping("demo02")
    public String demo02(@RequestParam List<String> hobby){
        System.out.println(hobby);
        return "welcome";
    }

6.restfull风格传参(@PathVariable)

//url为  demo01/lisi/555
//需要在@RequestMapping中对要接受的参数配置demo01/{name}/{age} 
//{}表示路径为变量
//在@PathVariable中如果不配置value的值,则默认以形参列表对应的参数名到PathVariable中去找对应的值

    @RequestMapping("demo01/{name1}/{age}")
    public String demo02(@PathVariable("name1") String name,@PathVariable int age){
        System.out.println(name+":"+age);
        return "welcome";
    }

7.接受Json字符串(涉及注解@RequestBody,注册mvc注解驱动,导入jackson.jar包)

    @RequestMapping(value="/demo06",produces="application/json;charset=utf-8")
    @ResponseBody
    //在方法上只有@RequestMapping时,如果返回值类型不为void,则视图解析器认为需要页面跳转
    //在方法上添加@ResponseBody时恒不跳转
    //1.如果返回值满足key-value形式(对象,数组,集合等)
    //  把响应头设置为application/json;charset=utf-8
    //  把返回的对象转换成json字符串以流的方式响应给客户端
    //2.如果返回值不满足key-value(基本数据类型和字符串等)
    //  把响应头设置为text/html
    //  把返回值以流的方式输出
    //  如果返回值包含乱码,(中文乱码)在RequsetMapping中设置produces="text/html;charset=utf-8"
    
    public People demo06(){
        return new People("李四",55);
    }
    

8.解决中文乱码问题(CharacterEncodingFilter)

<filter>
    <filter-name>encoding</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>

相关文章

网友评论

      本文标题:SpringMVC注解式开发

      本文链接:https://www.haomeiwen.com/subject/wuofyqtx.html