spring mvc基于注解的使用

参数

@RequestMapping("/params01")
public String params01(Integer age)

http://localhost:8080/params01?age=112
名字对应上了,自动做类型转换
匹配失败就报400错误
@RequestParam

@RequestMapping("/params01")
    public String params01(@RequestParam("ageName") Integer age)

在链接中匹配名为ageName;
required
用来指定参数是否必须传入值
默认为true 必须要传入
false 可以不用必须传入 自动传入null
注意:基础数据类型不接收null,会报错

//当设置多个属性的时候就需要加上 value="xxx"
  public String params01(@RequestParam(value = "ageName",required = false) Integer age)

defaultValue
没有传入值得时候给定一个默认值

//当设置了defaultValue 不需要设置required 
public String params01(@RequestParam(value = "ageName",defaultValue = "8") Integer age)

解决请求中文乱码问题
GET:直接设置tomcat目录下conf/server.xml 里面<Connector URIEncoding=“UTF-8”
POST:
在servlet的时期:在获取参数之前 设置 request.setCharacterEncoding(“UTF-8”) 每个方法里面都要设置一遍非常得麻烦
所以我们会用过滤器来处理编码的问题(终极解决方式)
使用springmvc提供给我们的编码过滤器来解决POST乱码问题:CharacterEncodingFilter
在web.xml里面配置.

 <!--配置编码过滤器 CharacterEncodingFilter 解决中文POST乱码问题 一定要写在所有过滤器前面-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--encoding 编码格式-->
        <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>  根据url请求进行匹配  *
            <servlet-name>  具体制定过滤哪个servlet
        -->
        <servlet-name>springmvc</servlet-name>
    </filter-mapping>

复杂数据类型
对象:
不用加上参数名字的,直接传入该对象对应的属性名字
如果是包装类型的简单变量 直接输入属性名字= 表单元素的name : name=“id”
数组 =保证这一组的表单元素都是同样的name: name=“alias”
List: 必须加上[索引] name=“list[0]” 如果List : name=“list[0].name”
map: 必须加上[key] name=“map[“key”]”
实体类: 只能给某个属性去赋值 name=“object.xxx”
注意:
如果出现多个对象比如:(User user,Role role)
: (List)
参数的情况 建议再次封装一层javaBean (DTO data transfer object)

@RequestMapping("/params01")
    public String params01(@RequestParam(value="username",defaultValue = "徐庶") String name){
        System.out.println(name);
        String x="";
        return "index.jsp";
    }


    /**
     * 复杂数据类型参数自动绑定演示 如果是List必须再使用JavaBean封装一层
     * @param userDTO
     * @return
     */
    @RequestMapping("/params02")
    public String params02(UserDTO userDTO){
        System.out.println(userDTO);
        return "/index.jsp";
    }
    //获取cookie
@RequestMapping("/cookie")
    public String cookie(@CookieValue("JSESSIONID") String jsessionId){
        System.out.println(jsessionId);
        return "/index.jsp";
    }
//========================================
private  Integer id;
    private  String name;
    private String[] alias;  //外号
    private List<String> hobbies; //兴趣爱好
    private Map<String,String> relatives;  //亲属
    private Role role;
    private List<User> friends;  //朋友

    public Integer getId() {
        return id;
    }

绝对路径前缀:${pageContext.request.contextPath}
对象:

<body>

<h2>简单参数演示</h2>
<form action="${pageContext.request.contextPath}/params03" method="post" enctype="multipart/form-data">
姓名:<input name="name" type="text"> <p></p>
    <input type="submit" value="提交">

</form>

<h2>复杂类型参数演示--List<User></h2>
<form action="${pageContext.request.contextPath}/params02" method="post"  >
    id:<input name="user.id" type="text"> <p></p>
    姓名:<input name="user.name" type="text"> <p></p>
    外号:<input name="user.alias" type="checkbox" value="狗剩" checked>狗剩
          <input name="user.alias" type="checkbox" value="柱子" checked>柱子 <p></p>
    爱好:<input name="user.hobbies[0]" type="checkbox" value="唱歌" checked>唱歌
    <input name="user.hobbies[1]" type="checkbox" value="跳舞" checked>跳舞 <p></p>
    亲属:<input name="user.relatives['father']" type="checkbox" value="爸爸" checked>爸爸
    <input name="user.relatives['mum']" type="checkbox" value="妈妈" checked>妈妈 <p></p>
    角色:<input name="user.role.name" type="text"> <p></p>
    朋友: <input name="user.friends[0].name" type="text" value="张三"><br>
    <input name="user.friends[1].name" type="text" value="李四"><p></p>

 
    <h2>新增角色</h2>
    id:<input name="role.id" type="text"> <p></p>
    角色:<input name="role.name" type="text"> <p></p>
    <input type="submit" value="提交">
</form>



<h2>复杂类型参数演示--User</h2>
<form action="${pageContext.request.contextPath}/params03" method="post"  >
    id:<input name="id" type="text"> <p></p>
    姓名:<input name="name" type="text"> <p></p>
    外号:<input name="alias" type="checkbox" value="狗剩" checked>狗剩
    <input name="alias" type="checkbox" value="柱子" checked>柱子 <p></p>
    爱好:<input name="hobbies[0]" type="checkbox" value="唱歌" checked>唱歌
    <input name="hobbies[1]" type="checkbox" value="跳舞" checked>跳舞 <p></p>
    亲属:<input name="relatives['father']" type="checkbox" value="爸爸" checked>爸爸
    <input name="relatives['mum']" type="checkbox" value="妈妈" checked>妈妈 <p></p>

    角色:<input name="role.name" type="text"> <p></p>
    朋友: <input name="friends[0].name" type="text" value="张三"><br>
    <input name="friends[1].name" type="text" value="李四"><p></p>


    <input type="submit" value="提交">
</form>


</body>

请求映射处理
@RequestMapping
方法上:将请求映射到方法中
类上:规范控制器里得方法,解决方法不同类中请求路径重复问题

//访问  http://localhost:8080/mapp/params01
@RequestMapping("/mapp")
public class ParamsContrller {
    @RequestMapping("/params01")
    public String params01(){}
    @RequestMapping(value="/params01",method=RequestMethod.POST)
    //只允许post请求,不写就都可以匹配 
    @RequestMapping(value="/params01",params={"username"})
    //必须要有参数username
     @RequestMapping(value="/params01",params={"!username"})
      //必须不能有参数username
      @RequestMapping(value="/params01",params={"username=王"})
       //参数username必须是王,必须不能是加上!
//通配符
@RequestMapping(value="/ant*")
    public String mapping08(){
        System.out.println("通配符——*");
        return "/index.jsp";
    }
    //?比*更加细粒度
    @RequestMapping(value="/ant?")
    public String mapping07(){
        System.out.println("通配符——?");
        return "/index.jsp";
    }
    //**可以中间加很多级
    //http://localhost:8080/mapp/1/2/3/ant
    @RequestMapping(value="/**/ant")
    public String mapping09(){
        System.out.println("通配符——**");
        return "/index.jsp";
    }

@PathVariable
请求参数得值

//http://localhost:8080/user/1/name
@RequestMapping("/user/{id}/{username}")
    public String path01(@PathVariable("id") Integer id, @PathVariable("username") String name){
    //可以取到1和name
        System.out.println(id);
        System.out.println(name);
        return "/index.jsp";
    }

    @RequestMapping("/user02/{id}/{name}")
    public String path02(User user){
        System.out.println(user);
        return "/index.jsp";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值