使用 @PathVariable 获取URL变量
controller 代码
jsp页面
URL变量
在博客中,学习了如何在
@Controller
中创建@RequestMapping
(或者响应的简写)来处理不同的URL请求。但是在Web应用中URL通常不是一成不变的,例如微博两个不同用户的个人主页对应两个不同的URL:http://weibo.com/user1
,http://weibo.com/user2
。我们不能对于每一个用户都编写一个被
@RequestMapping
注解的方法来处理其请求,也就是说,对于相同模式的URL(例如不同用户的主页,它们仅仅是URL中的某一部分不同,为他们各自的用户名,我们说它们具有相同的模式)。定义URL变量规则
可以在@RequestMapping注解中用{}来表明它的变量部分,例如:
@RequestMapping("/users/{username}")
这里
{username}
就是我们定义的变量规则,username
是变量的名字,那么这个URL路由可以匹配下列任意URL并进行处理:
需要注意的是,在默认情况下,变量中不可以包含URL的分隔符/,例如路由不能匹配/users/tianmaying/ricky
,即使你认为tianmaying/ricky
是一个存在的用户名。
在路由中定义变量规则后,通常我们需要在处理方法(也就是@RequestMapping
注解的方法)中获取这个URL的具体值,并根据这个值(例如用户名)做相应的操作,SpringMVC
提供的@PathVariable
可以帮助我们:
@RequestMapping("/users/{username}")
@ResponseBody
public String userProfile(@PathVariable String username){
// return String.format("user %s", username);
return "user" + username;
}
在上述例子中,当@Controller
处理HTTP请求时,userProfile
的参数username
会自动设置为URL中对应变量username
(同名赋值)的值,例如当HTTP请求为/users/fpc
,URL变量username
的值fpc会被赋给函数参数username
,函数的返回值自然是userfpc
。
在默认的情况下,Spring会对@PathVariable
注解的变量进行自动赋值,当然你也可以指定@PathVariable
使用哪一个URL中的变量:
@RequestMapping("/users/{username}")
@ResponseBody
public String userProfile(@PathVariable("username") String username){
// return String.format("user %s", username);
return "user" + username;
}
运行结果:
可以定义URL路由,其中包含多个URL变量:
@RequestMapping("/user/{username}/blog/{blogId}")
@ResponseBody
public String getUerBlog(@PathVariable String username , @PathVariable int blogId) {
return "user: " + username + "blog->" + blogId;
}
这种情况下,Spring能够根据名字自动赋值对应的函数参数值,当然也可以在@PathVariable中显示地表明具体的URL变量值。
在默认情况下,@PathVariable注解的参数可以是一些基本的简单类型:int,long,Date,String等,Spring能根据URL变量的具体值以及函数参数的类型来进行转换,例如/user/fpc/blog/1,会将“fpc”的值赋给username,而1赋值给int变量blogId。
运行结果:
很多时候,需要对URL变量进行更加精确的定义,例如-用户名只可能包含小写字母,数字,下划线,我们希望:
/user/fpc
是一个合法的URL/user/#$$$
则不是一个合法的URL除了简单地定义{username}
变量,还可以定义正则表达式进行更精确的控制,定义语法是{变量名:正则表达式}[a-zA-Z0-9_]+
是一个正则表达式,表示只能包含小写字母,大写字母,数字,下划线。如此设置URL变量规则后,不合法的URL则不会被处理,直接由SpringMVC框架返回404Not Found
。
@RequestMapping("/user/{username:[a-zA-Z0-9_]+}/blog/{blogId}")
@RequestMapping
注解中定义URL变量规则@RequestMapping
注解方法中获取URL变量-@PathVariable
@PathVariable
指定URL变量名url变量替换
HTML扩展方式navTab, dialog, ajaxTodo 的url支持变量替换。例如:__URL__/edit/id/{xxx}
大括号内的xxx就是变量名,主要功能是结合table组件一起使用,下面是dwz_thinkphp中用户列表的示例:
下图中的删除、编辑、修改密码都是用了url变量替换:
删除、编辑、修改密码使用了变量{sid_user}
<tbody>中<tr target="sid_user" rel="{$vo['id']}">
当选中一行时,tr上的rel值会自动替换到url变量中.
注意url变量名{sid_user}和tr的target="sid_user"保持一致.
代码示例:
<a class="delete" href="__URL__/foreverdelete/id/{sid_user}/navTabId/__MODULE__" target="ajaxTodo" title="你确定要删除吗?" warn="请选择用户"><span>删除</span></a>
<a class="edit" href="__URL__/edit/id/{sid_user}" target="dialog" mask="true" warn="请选择用户"><span>编辑</span></a>
<a class="icon" href="__URL__/password/id/{sid_user}" target="dialog" mask="true" warn="请选择用户"><span>修改密码</span></a>
<table class="list" width="100%" layoutH="116">
<thead>
<tr>
<th width="60">编号</th>
<th width="100">用户名</th>
<th>昵称</th>
<th>Email</th>
<th width="100">添加时间</th>
<th width="120">上次登录</th>
<th width="80">登录次数</th>
<th width="80">状态</th>
</tr>
</thead>
<tbody>
<volist id="vo" name="list">
<tr target="sid_user" rel="{$vo['id']}">
<td>{$vo['id']}</td>
<td>{$vo['account']}</td>
<td>{$vo['nickname']}</td>
<td>{$vo['email']}</td>
<td>{$vo['create_time']|date="Y-m-d",###}</td>
<td>{$vo['last_login_time']|date="Y-m-d H:i:s",###}</td>
<td>{$vo['login_count']}</td>
<td>{$vo['status']|showStatus=$vo['id']}</td>
</tr>
</volist>
</tbody>
</table>
使用 @PathVariable 获取URL变量
controller 代码
jsp页面
转载于:https://www.cnblogs.com/hy162050430/p/9451906.html
在DWZ文档中,对URL的变量替换说明如下:
HTML扩展方式navTab, dialog, ajaxTodo的url支持变量替换。例如:__URL__/edit/id/{xxx},大括号内的xxx就是变量名,主要功能是结合table组件一起使用,利用URL变量替换,对于解决数据的删除、编辑是非常方便的。
比如:删除、编辑使用了变量{sid_user}
<tbody>中<tr target="sid_user"rel="{$vo['id']}">
当选中一行时,tr上的rel值会自动替换到url变量中.
注意url变量名{sid_user}和tr的target="sid_user"保持一致.
具体的URL变量替换使用如下:
继续之前DWZ笔记二的例子,客户信息管理的界面如下:
界面代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <base href="<%=basePath%>">
<%@taglib uri="/struts-tags" prefix="s" %>
<!--分页的form-->
<form id="pagerForm" action="<%=basePath%>/admin/customer_main.action" method="post"> <input type="hidden" name="pageNum" value="1" /><!--【必须】value=1可以写死--> <input type="hidden" name="numPerPage" value="${param.numPerPage}" />--> </form>
<div class="pageHeader">
<!--查询的form--> <form rel="pageForm" οnsubmit="return navTabSearch(this);" action="<%=basePath%>/admin/customer_main.action" method="post"> <div class="searchBar"> <ul class="searchContent"> <li> <label>我的客户:</label> <input name="keywords" type="text" size="25" value="${param.keywords}" alt="请输入客户名"/> </li> </ul> <div class="subBar"> <ul> <li><div class="buttonActive"><div class="buttonContent"><button type="submit">检索</button></div></div></li> <li><a class="button" href="demo_page6.html" target="dialog" mask="true" title="查询框"><span>高级检索</span></a></li> </ul> </div> </div> </form> </div>
<div class="pageContent"> <div class="panelBar"> <ul class="toolBar"> <li><input type="checkbox" class="checkboxCtrl" group="ids" />全选</li> <li><a title="确实要删除这些记录吗?" target="selectedTodo" rel="ids" postType="string" href="<%=basePath%>/admin/customer_deleteAll.action" class="delete"><span>批量删除</span></a></li> <li><a class="add" href="<%=basePath%>/admin/customer_addInput.jsp" target="navTab"><span>添加</span></a></li> <li><a class="delete" href="customer_delete.action?customer.id={cid}" target="ajaxTodo" title="确定要删除吗?"><span>删除</span></a></li> <li><a class="edit" href="customer_updateInput.action?id={cid}" target="navTab"><span>修改</span></a></li> <li class="line">line</li> <li><a class="icon" href="demo/common/dwz-team.xls" target="dwzExport" targetType="navTab" title="实要导出这些记录吗?"><span>导出EXCEL</span></a></li> </ul> </div> <table class="table" width="100%" layoutH="150"> <thead> <tr> <th width="50">选择</th> <th width="120">序号</th> <th>客户昵称</th> <th width="100">客户名</th> <th width="150">客户类型</th> <th width="80" align="center">客户状态</th> <th width="80">客户邮箱</th> <th width="80">创建时间</th> <th width="80">上次登录时间</th> <th width="80">上次登录IP</th> </tr> </thead> <tbody> <s:iterator value="customers" var="c"> <tr target="cid" rel="${c.id}"> <td><input type="checkbox" name="ids" value="${c.id}" /></td> <td>${c.id}</td> <td>${c.cus_id}</td> <td>${c.cus_name}</td> <td><s:if test='#c.cus_type=="2"'>普通客户</s:if><s:else>管理员</s:else></td> <td><s:if test='#c.cus_isLock=="0"'>正常使用</s:if><s:else>用户锁定</s:else></td> <td>${c.cus_email}</td> <td>${c.cus_createtime}</td> <td>${c.last_logintime}</td> <td>${c.last_loginip}</td> </tr> </s:iterator> </tbody> </table> <div class="panelBar"> <div class="pages"> <span>显示</span> <select class="combox" name="numPerPage" onchange="navTabPageBreak({numPerPage:this.value})"> <option value="20">20</option> <option value="10">10</option> <option value="30">30</option> <option value="500">50</option> <option value="100">100</option> </select> <span>条,共${pager.totalCount}条</span> </div> <!--分页组件--> <div class="pagination" targetType="navTab" totalCount="${pager.totalCount}" numPerPage="${pager.everyPage}" pageNumShown="10" currentPage="${pager.currentPage}"></div>
</div> </div>
在上述代码中,对于删除和编辑的按钮设置如下:<li>
<a class="delete" href="customer_delete.action?customer.id={cid}" target="ajaxTodo"
title="确定要删除吗?"><span>删除</span></a>
</li> <li><a class="edit" href="customer_updateInput.action?id={cid}" target="navTab"><span>修改</span></a></li>
其中定义的XXX?id{cid}中的cid即为需要替换的变量,其具体定义在table中:<table class="table" width="100%" layoutH="150"> <thead> <tr> <th width="50">选择</th> <th width="120">序号</th> <th>客户昵称</th> <th width="100">客户名</th> <th width="150">客户类型</th> <th width="80" align="center">客户状态</th> <th width="80">客户邮箱</th> <th width="80">创建时间</th>
<th width="80">上次登录时间</th> <th width="80">上次登录IP</th> </tr> </thead> <tbody> <s:iterator value="customers" var="c"> <tr target="cid" rel="${c.id}"> <td><input type="checkbox" name="ids" value="${c.id}" /></td> <td>${c.id}</td> <td>${c.cus_id}</td> <td>${c.cus_name}</td> <td><s:if test='#c.cus_type=="2"'>普通客户</s:if><s:else>管理员</s:else></td> <td><s:if test='#c.cus_isLock=="0"'>正常使用</s:if><s:else>用户锁定</s:else></td> <td>${c.cus_email}</td> <td>${c.cus_createtime}</td> <td>${c.last_logintime}</td> <td>${c.last_loginip}</td> </tr> </s:iterator> </tbody> </table>
在这个table中,通过<s:iterator value="customers" var="c">,利用strut的标签,将后台的customer信息打印在页面中,同时进行关键设置<tr target="cid" rel="${c.id}">,当选中某一行时,rel="${c.id}"中的rel的值将自动替换到cid中,即通过target="cid"定义,也就是替换进删除和编辑的变量中。