@PathVariable
当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。
@Controller @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping("/pets/{petId}") public void findPet(@PathVariable String ownerId,@PathVariable String petId, Model model) { // implementation omitted } }
@Controller @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping("/pets/{petId}") public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { // implementation omitted } }
上面代码把URI template 中变量 ownerId的值和petId的值,绑定到方法的参数上。若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称。如果一样,则可以省略。
@RequestMapping(value = "/forwardEdit/{id}.do") public String forwardEdit(HttpServletRequest request, @PathVariable("id") int id, String pf) throws Exception { // 采购方编码 TJzny_plan fmodel = jzny_planService.find(id); AjaxMsg message = fmodel == null ? new AjaxMsg(false, "修改的计划单不存在!") : new AjaxMsg(true, ""); LogonInfo userInfo = this.getUserInfo(request); TDepartment department = departmentService.find(Integer.parseInt(fmodel.getBusinessDep())); String businessDepId_value = ""; if (department != null) { businessDepId_value = department.getDeptName(); } request.setAttribute("businessDepId_value", businessDepId_value); request.setAttribute("userInfo", userInfo); request.setAttribute("fmodel", fmodel);// 将结果返回给前台,前台可以通过EL表达式来获取数据 request.setAttribute("AjaxMsg", message); request.setAttribute("pf", pf); request.setAttribute("url", "planmanage/forwardGrid.do"); request.setAttribute("purchaseTypes", StringUtils.isNotBlank(userInfo.getUserInfo().getDvSpecifyPurchaseTypes()) ? userInfo.getUserInfo().getDvSpecifyPurchaseTypes() : "0"); // 判断是否需要判断物资编码 String ishasWzCode = "否"; List<TDictionarydata> dictionarydatas = DictorySearchService.find("WzCodeControl", userInfo.getPurchaserNo()); if (dictionarydatas != null && dictionarydatas.size() > 0) { ishasWzCode = dictionarydatas.get(0) != null ? dictionarydatas.get(0).getDicValue() : "否"; } request.setAttribute("ishasWzCode", ishasWzCode); return "jh/bid_planmanage/edit"; }