Spring MVC URL传参
Spring MVC 学习 之 - URL参数传递
在学习 Spring Mvc 过程中,有必要来先了解几个关键参数:
@Controller:
在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射。
1
2
3
4
5
|
@Controller public class UserAction { } |
@RequestMapping
指定URL映射路径,如果在控制器上配置 RequestMapping ,具体请求方法也配置路径则映射的路径为两者路径的叠加 常用映射如:RequestMapping("url.html")
配置映射路径:

- @Controller
- public class UserAction
- {
- @RequestMapping(value = "/get_alluser.html")
- public ModelAndView GetAllUser(String Id)
- {
- }
- }

以上配置映射
http://***:8080:web1/get_alluser.html:
如在 @Controller添加 @RequestMapping(value = "/user"),则映射路径变成
http://***:8080:web1/user/get_alluser.html
@ResponseBody
将注解方法对应的字符串直接返回
@RequestParam
自动映射URL对应的参数到Action上面的数值,RequestParam 默认为必填参数。
@PathVariable
获取@RequestMapping 配置指定格式的URL映射参数

- /*
- * 直接输出 HTML,或JSON 字符串
- * 请求路径:
- * /web1/urlinfo/getcontent.html?key=rhythmk
- * /web1/urlinfo/getcontent.json?key=rhythmk
- * */
- @ResponseBody
- @RequestMapping(value = "/getcontent.**")
- public String GetContent(
- @RequestParam("key") String key,
- @RequestParam(value = "key2", required = false, defaultValue = "defaultValue") String key2) {
- System.out.println("getcontent 被调用");
- String result = "直接返回内容 - key:" + key + ",key2:" + key2;
- System.out.println(result);
- return result;
- }


- /*
- * RequestMapping 支持 Ant 风格的URL配置 :
- * 请求路径:
- * /urlinfo/geturlant/config.html?key=adddd
- */
- @ResponseBody
- @RequestMapping(value = "/geturlant/**.html")
- public String getUrlAnt(HttpServletRequest request) {
- String result = "?后面的参数为:" + request.getQueryString();
- return result;
- }


- /*
- * 配置指定格式的URL,映射到对应的参数
- * 请求路径:/web1/urlinfo/geturlparam/12_123.html
- *
- * */
- @RequestMapping(value = "/geturlparam/{id}_{menuId}.html")
- public ModelAndView getUrlParam(@PathVariable("id") String id,
- @PathVariable("menuId") String menuId) {
- ModelAndView mode = new ModelAndView(ShowMsg);
- mode.addObject("msg", "获取到的Id:" + id + ",menuId:" + menuId);
- return mode;
- }


- /*
- * 只接收Post 请求
- */
- @ResponseBody
- @RequestMapping(value = "/posturl.html", method = RequestMethod.POST)
- public String UrlMethod(@RequestParam String id) {
- return "只能是Post请求,获取到的Id:" + id;
- }


- /*
- * 写入 cookie
- * */
- @RequestMapping("/writecookies.html")
- public ModelAndView writeCookies(@RequestParam String value,
- HttpServletResponse response) {
- response.addCookie(new Cookie("key", value));
- ModelAndView mode = new ModelAndView(ShowMsg);
- mode.addObject("msg", "cookies 写入成功");
- return mode ;
- }


- /*
- * 通过 @CookieValue 获取对应的key的值
- * */
- @RequestMapping("/getcookies.html")
- public ModelAndView getCookie(@CookieValue("key") String cookvalue) {
- ModelAndView mode = new ModelAndView(ShowMsg);
- mode.addObject("msg", "cookies=" + cookvalue);
- return mode;
- }


- /*
- * 将 Servlet Api 作为参数传入
- * 可以在action中直接使用 HttpServletResponse,HttpServletRequest
- * */
- @RequestMapping("/servlet.html")
- public String Servlet1(HttpServletResponse response,
- HttpServletRequest request) {
- Boolean result = (request != null && response != null);
- ModelAndView mode = new ModelAndView();
- mode.addObject("msg", "result=" + result.toString());
- return ShowMsg;
- }


- /*
- * 根据URL传入的参数实例化对象
- *
- * 如: http://127.0.0.1:8080/web1/urlinfo/getobject.html?UserId=1&UserName=ad
- * */
- @RequestMapping("getobject.html")
- public ModelAndView getObject(UserInfo user) {
- String result = "用户ID:" + user.getUserId().toString() + ",用户名:"
- + user.getUserName().toString();
- ModelAndView mode = new ModelAndView(ShowMsg);
- mode.addObject("msg", "result=" + result.toString());
- return mode;
- }

实现页面跳转:

- /*
- * 实现页面跳转
- * /web1/urlinfo/redirectpage.html
- * */
- @RequestMapping("/redirectpage.html")
- public String RedirectPage()
- {
- return "redirect:getcookies.html?r=10";
- }

直接回传JSON
请求的URL地址一定是以.json结尾,否则异常
Failed to load resource: the server responded with a status of 406 (Not Acceptable) : The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()
回传实体:

- @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
- public class UserInfo {
- private Integer UserId;
- public Integer getUserId() {
- return UserId;
- }
- public void setUserId(Integer userId) {
- UserId = userId;
- }
- public String getUserName() {
- return UserName;
- }
- public void setUserName(String userName) {
- UserName = userName;
- }
- private String UserName;
- }

回传 action

- @ResponseBody
- @RequestMapping("/getuser.json")
- public UserInfo GetUser()
- {
- System.out.println("getuser");
- UserInfo model=new UserInfo();
- model.setUserId(100);
- model.setUserName("aa");
- return model;
- }

请求:
/web1/urlinfo/getuser.json
输出:
- {"userId":100,"userName":"aa"}
Spring MVC URL传参的更多相关文章
- Spring Swagger URL传参问题(转)
代码例子: @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息") @ApiImplicitPar ...
- spring MVC 尝试传参json(应用部分)
spring 3.1后增加新的注解:@Requestbody,@Responsebody, 暂不论Requestdody, 若想让后端代码直接返回json字符串,可使用@Responsebody, 用 ...
- spring mvc 重定向传参
参考链接如下: http://bbs.csdn.net/topics/391034118?page=1 自己的示例程序: 详细页面提交一个修改动作,修改完成后跳转到检索页面,把检索条件重新赋值给检索页 ...
- 解决Spring MVC前台传参中文乱码问题
在web.xml文件中配置字符编码过滤器: <filter> <filter-name>CharacterEncoding</filter-name> <fi ...
- Spring MVC POJO传参方式
有两POJO类 Address.java package com.proc; public class Address { private String province; private Strin ...
- Spring MVC url提交参数和获取参数
[转载:http://blog.csdn.net/mahoking] 普通URL提交参数 该格式url为:url.do?param1=mahc¶m2=8888.00 需 ...
- 点击div 跳转并通过URL传参
点击div前要先给div绑定要传的参数: //给panel绑定自定义属性,方便在跳转时传带参数,键/值对排列 panel.attr("user_age",user_age); pa ...
- Spring MVC上传文件
Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...
- 关于url传参中文乱码问题
之前都一直很不了解中文编码得问题,之前在做项目中没碰到那么头痛的问题.所以一直没有了解中文乱码的问题. 问题描述: 地址: http://localhost:8080/sun-government/c ...
随机推荐
- JSON stringify and parse
来源 : http://javascript.ruanyifeng.com/stdlib/date.html //解析json也可以传入一个方法, 基本上和stringify差不多,不过是逆序的, 要 ...
- [LeetCode 119] - 杨辉三角形II(Pascal's Triangle II)
问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O(k)的额外空间吗? 初始思路 首先来复习复习杨辉三角形的性质 ...
- 华夏的理财30天A和华夏财富宝货币哪个收益比较好?
1.收益性比较 华夏理财30天A001057属于债券型基金,华夏财富宝货币000343属于货币型基金(活期宝,类似余额宝)收益不同 2.流动性 活期宝是可以随时取现的产品,30天A是有封闭期的产品,这 ...
- java模拟http post
我们将使用java.net.URLConnection来完成一次post请求,假设要post数据到http://localhost:8080/doSome上: URLConnection urlCon ...
- hdu-1598
思路: 首先如果想到了Kruskal算法,那么下一步我们可能马上会想:那我们就从头开始写这个算法吧.然后一写就很容易发现一个问题——如果按照正常的Kruskal算法来做,那么start到end的舒适度 ...
- Spring MVC在接收复杂集合参数
Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是applica ...
- React Native 初识
Facebook 在 React.js Conf 2015 大会上推出了基于 JavaScript 的开源框架 React Native,本中文教程翻译自 React Native 官方文档. Rea ...
- 安装 Android 运行环境
如果你恰好有一些旧的. 过时的 Android SDK 版本,请务必把所需的包更新至下面提到的版本并安装所有缺少的部分. 安装和配置 SDK 安装最新的 JDK. 使用 brew install an ...
- DynamicMBean(Java SE 6 新特性: JMX 与系统管理)
Dynamic MBean 是一种在运行时定义其管理接口的 MBean.例如,配置 MBean 可以通过解析 XML 文件来确定它所公开的属性名称和类型. 任何实现 DynamicMBean 接口的类 ...
- Using HttpClient properly to avoid CLOSE_WAIT TCP connections
Apache的HttpComponent组件,用的人不在少数.但是能用好的人,却微乎其微,为什么?很简单,TCP里面的细节实现不是每个人都能捕获到的(细节是魔鬼),像并发请求控制&资源释放,N ...