springmvc之url参数传递
在学习 Spring Mvc 过程中,有必要来先了解几个关键参数:
@Controller:
在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射。
|
1
2
3
4
5
|
@Controllerpublic 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("王坤");
return model;
}
请求:
/web1/urlinfo/getuser.json
输出:
{"userId":100,"userName":"张三"}
springmvc之url参数传递的更多相关文章
- CSS样式表、JS脚本加载顺序与SpringMVC在URL路径中传参数与SpringMVC 拦截器
CSS样式表和JS脚本加载顺序 Css样式表文件要在<head>中先加载,这样网页显示时可以第一次就渲染出正确的布局和样式,网页就不会闪烁,或跳变 JS脚本尽可能放在<body> ...
- springMvc 通过url传值,实现访问
springMvc 通过url传值,实现访问 1.创建web项目,引入相关jar包,并完成相应配置,在上一篇博客已经实现 2.在WEB-INF下创建jsp文件夹,并创建hello.jsp文件 < ...
- SpringMvc的Url映射和传参案例(转)
Springmvc的基本使用,包括url映射.参数映射.页面跳转.ajax和文件上传 以前学习的时候写的代码案例,今天整理笔记的时候找到了,很久没有来园子了,发上来当个在线笔记用吧,免的时间长了又忘了 ...
- SpringMvc的Url映射和传参案例
Springmvc的基本使用,包括url映射.参数映射.页面跳转.ajax和文件上传 以前学习的时候写的代码案例,今天整理笔记的时候找到了,很久没有来园子了,发上来当个在线笔记用吧,免的时间长了又忘了 ...
- springmvc注解和参数传递
一.SpringMVC注解入门 1. 创建web项目2. 在springmvc的配置文件中指定注解驱动,配置扫描器 <!-- mvc的注解驱动 --> <mvc:annotation ...
- SpringMVC中url映射到Controller
SpringMVC也是一种基于请求驱动的WEB框架,并且使用了前端控制器的设计模式.前端控制器就是DispatcherServlet控制器,只要满足web.xml文件中的[url-pattern]的规 ...
- JS URL参数传递 谷歌乱码解决
//第一个页面 var name=encodeURIComponent("参数"); var url="test1.html?name="+name; //第二 ...
- [原创]Spring MVC 学习 之 - URL参数传递
原文参考地址: http://www.cnblogs.com/rhythmK/p/3971191.html 目的和缘由: 本人想做一个分享的页面,分析给朋友注册,注册按分享ID进行级联; 过程: 很多 ...
- form表单参数传递和url参数传递的区别
template: form表单: <form action="" method='GET'> <div class="input-group" ...
随机推荐
- putty的保存功能如何使用
Putty的设置保存功能隐藏的实在太好了,原来在Connection菜单中修改设置后,并不能在该界面保存. 保存步骤: 1.需要点击Session菜单,选中下面的Default Setting. 2. ...
- 跟我学Spring3(9.2):Spring的事务之事务管理器
原文出处: 张开涛9.2.1 概述 Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口PlatformTransactionManage ...
- Coursera课程python中的一些程序
Index of /code Name Last modified Size Description Parent Directory - BeautifulSoup.py 07-Aug-2015 1 ...
- 【BZOJ】【3239】Discrete Logging
BSGS BSGS裸题,嗯题目中也有提示:求a^m (mod p)的逆元可用快速幂,即 pow(a,P-m-1,P) * (a^m) = 1 (mod p) /******************** ...
- ELK kibana查询与过滤
在kibana中,可通过搜索查询过滤事务或者在visualization界面点击元素过滤. 创建查询 在Discover界面的搜索栏输入要查询的字段.查询语法是基于Lucene的查询语法.允许布尔运算 ...
- 数据库实例: STOREBOOK > 用户 > 编辑 用户: DBSNMP
ylbtech-Oracle:数据库实例: STOREBOOK > 用户 > 编辑 用户: DBSNMP 编辑 用户: DBSNMP 1. 一般信息返回顶部 1.1, 1.2, 2 ...
- 6个原则、50条秘技提高HTML5应用及网站性能
Jatinder Mann是微软Internet Explorer产品的一名项目经理,在BUILD 2012大会上,他做了题为“提高HTML5应用和网站性能的50条秘技(50 performance ...
- scala 学习笔记十 元组
1.元组初始化 2.元组作为函数返回值 3.元组拆包 上面168行 ,单个val后面跟着一个由五个标识符构成的元组,表示对ff返回的元组进行拆包 上面174行,将整个元组捕获到单个val或var中,那 ...
- Ubuntu14.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 说在前面的话 首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu 14.04.4 LTS ...
- ubuntu添加默认路由才可以访问网络