springMVC注解中@RequestMapping中常用参数value params 以及@RequestParam 详解

package cn.hive.action;

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

/**
 * Created with IntelliJ IDEA.
 * Author: DAX
 * Date: 2016/10/13
 * 测试action类 
 * Time: 20:08
 */

@Controller
@RequestMapping(value = "/{abc}")
public class InitAction {
    /*
     *  @RequestMapping  value  和params 的详解
     *
     *
     * 如类没有定义请求映射 类方法中的value代表根路径  如果在类方法中有点类似于struts中 action的id
     * params 为请求参数的数组 支持一些简单的表达式      params={"!id","name!=James"}  表示不能带名称为id的参数  而且name的值不能为James  等等表达式   
     *
     * @RequestMapping(value = "/init", params = {"id=myValue"}) 只有存在了请求参数id=myValue  /init.action?id=myValue 才会被initData处理
     * @RequestMapping(value = "/init", params = {"name=kobe", "number=23"}) /init.action?name=kobe&&number=23 否则 404错误
     *
     * 一旦abc  init  为占位符即用{}包括起来 该请求默认为下面
     * http://localhost:8080/abc/init.action
     * 如果被赋值  例如  abc = "hello";   init = "world";  则下面网址也可以访问ininData方法
     * http://localhost:8080/hello/world.action
     * 这形成了具有REST(表现层状态转化)风格的请求形式  表示 abc 的id为 init的实际赋值 但是请求的方法必须为GET
     *
     * @RequestParam 详解  接收 请求参数
     * required参数默认为false   表示   可以为空
     * 如果为 数据的基本类型     一旦没有赋值  提交  会被赋值null
     * 抛出异常 一般推荐用包装类 来接收  比如  int  用 Integer   double  用Double  等
     */
    @RequestMapping(value = "/{init}")
    public String initData(@PathVariable("abc") String abc, @PathVariable("init") String init, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "age", required = false) Integer age) {
        abc = "hello";
        init = "world";
        System.out.println(name + age);
        return "test";
    }

}


测试页面  index.jsp



<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/10/13
  Time: 16:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="<c:url value="/hello/world.action"/>" method="post" >
    <label>
        <input type="text" name="name">
        <input type="text" name="age">
    </label>
    <input type="submit" value="提交">
</form>
<a href="<c:url value="/hello/world.action"/>">test</a>
</body>
</html>

成功页面


<%--
  Created by IntelliJ IDEA.
  User: felord
  Date: 2016/10/13
  Time: 20:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
aaaaaaaaaaaaa
${param.name}
${param.age}
bbbbbbbbbbbb
${param.id}

</body>
</html>

对于 params  已经解释过了 因为 有冲突  没有测试代码    可自行测试   



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农小胖哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值