springMVC

springMVC技术与servlet技术相同,均属于web层开发技术。

springMVC是一种基于Java实现MVC模型的轻量级web框架。springMVC中的bean仅仅指的是表现层的bean,不包括数据层和业务逻辑层的bean,为了避免springConfig错误的加载到springMVC的bean,我们需要在springConfig加载控制的bean时去掉springMVC控制的bean,也就是表现层的bean。

springConfig去掉表现层的bean的方法有俩种:

  法一:不加载表现层的bean

package org.example.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

@Configuration
//在spring加载控制bean时,去掉加载的springMVC控制的bean
//法一:不加载表现层的bean
@ComponentScan({"org.example.dao","org.example.service"})
public class springConfig {
}

 法二:把表现层的bean从扫描的范围中去掉

package org.example.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

@Configuration
//在spring加载控制bean时,去掉加载的springMVC控制的bean
//法二:把表现层的bean从扫描范围中去掉
@ComponentScan(value = "org.example",
excludeFilters = @ComponentScan.Filter(
   type = FilterType.ANNOTATION,  //按注解排除扫描的bean
   classes = Controller.class  //告诉它排除什么注解的bean
  )
)
public class springConfig {
}

然后是springMVCConfig的配置

package org.example.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan("org.example.Controller")

@EnableWebMvc
public class springMVCConfig {
}

@EnableWebMvc  接收json数据时,开启json数据转换为我们需要的数据类型对象   这仅仅是该注解的功能之一。

然后是Controller层的代码:

package org.example.Controller;

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

@Controller
@RequestMapping("/postman")
public class userRequest {

}

@Controller 注解 表示该类为表现层的bean

@RequestMapping("/postman") 该注解用于声明访问该类的路径http://localhost:8080/postman/*   可以访问该类中的所有方法。

表现层的方法:

        传入简单类型的数据:

package org.example.Controller;

import org.example.pojo.user;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/postman")
public class userRequest {

    @RequestMapping("/postman1")
    @ResponseBody //作用是把return的返回值作为响应数据
    public String postman1(String name,int age){ //将get请求的参数作为形参输入就可以接收了。
        System.out.println("name==>"+name);
        System.out.println("age==>"+age);
        return "{'module':'postman1'}";
    }
}

@RequestMapping("/postman1")注解是声明该方法的访问路径,http://localhost:3306/postman/postman1?name=zhangsan&age=18 就可以访问到该类中的该方法。并把参数传入,注意访问路径中的参数与方法中形参的名字要一致。

@ResponseBody 注解是把return的返回值作为响应数据。

      请求路径参数名与形参名不一致时

@RequestMapping("/postman2")
    @ResponseBody
    public String postman2(@RequestParam("name") String username, int age){

        System.out.println("name==>"+username);
        System.out.println("age==>"+age);
        return"{'module':'postman2'}";
    }

@RequestParam注解是把请求路径参数name的值对应到该形参。

      

       传入引用类型的数据:对引用类型数据的默认处理是先把引用类型数据创建出一个对象,然后把获取的数据参数insert到对象中。

@RequestMapping("/postman3")
    @ResponseBody
    public String postman3(user user){
        System.out.println("user==>"+user);
        return "{'module':'postman3'}";
    }

   如果user对象中的有引用类型的数据时需要按图中所例进行传参

     

         传入数组数据

//数组参数
    @RequestMapping("/postman5")
    @ResponseBody
    public String postman5(String[] likes){
        System.out.println("likes==>"+ Arrays.toString(likes));
        return "{'module':'postman5'}";
    }

     传入集合对象 

@RequestMapping("/postman6")
    @ResponseBody
    public String postman6(@RequestParam List<String> likes){
        System.out.println("likes==>"+likes);
        return "{'module':'postman6'}";
    }

因为引用类型数据list集合是个接口,没有构造方法所以不能创建对象,所以不能用默认的方法,
    解决办法:加上@RequestParam注解,告诉它,是把获取的请求参数放入list集合中,而不是作为list集合的属性。

   传入json格式的集合

@RequestMapping("/postman7")
    @ResponseBody
    public String postman7(@RequestBody List<String> likes){
        System.out.println("likes==>"+likes);
        return "'module':'postman7'";
    }

  因为此时数据我们写在了请求体中,不能用requestParam了,需要换成requestBody。

        传入json格式的引用类型数据:

    @RequestMapping("/postman8")
    @ResponseBody
    public String postman8(@RequestBody user user){
        System.out.println("user==>"+user);
        return "'module':'postman8'";
    }

     

    传入日期类型数据:

 @RequestMapping("/postman10")
    @ResponseBody
    public String postman10(Date date,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date2){
        System.out.println("date==>"+date);
        System.out.println("date1==>"+date1);
        System.out.println("date2==>"+date2);
        return "{;module':'postman10'}";
    }

日期,年月日之间的分隔符,/ 可以直接用; - 因为格式不匹配,需要加@DateTimeFormat注解来配格式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值