這次項目換了freemarker模板,我不熟。再加上我js真是一塌糊塗,解決一個按鈕跳轉問題,居然花費了一天多。。。
現在記錄一下吧。
問題描述:當前網址是A,視圖如下。我希望點擊“詳情”跳轉到網址A/details,展示這條記錄的詳細信息。
但是始終無法跳轉。debug發現前台向后台傳數據ok,后台查詢數據庫ok,但始終無法跳轉到A/details。直接輸入
A/details,發現ftl文件編寫和訪問正確。

踩到的坑:
1.@RestController理解錯誤 2. js跳轉理解錯誤 3. ajax理解錯誤
知識總結:
1.@RestController
這個注解相當於@ResponseBody 和 @Controller兩個注解的組合,不返回視圖,只返回數據。如果一個類上加了這個注解,
那么這個類的函數都是返回不了視圖的,return "redirect:/seq_tool/seq_order_details";也會只在頁面上顯示return的字符串。
解決方法是把類上的注解改為@Controller,然后給不返回視圖,只返回數據的函數加上注解@ResponseBody。
2.js跳轉問題

直接貼代碼:
我的表格是用js的bootstrapTable創建的,所以在另一個文件table.js寫了操作,這里隱去。
<script>
// 定義按鈕:當前角色支持的操作
function operateFormatter(code, row, index) {
var orderId = row.id;
orderId.value = row.id;
var operateBtn = [
'<@shiro.hasPermission name="seq_tool:getdetails"><button class="btn btn-xs btn-primary btn-get-details" type="button" order-id="' + orderId + '" ><i class="fa fa-info-circle"></i>詳情</button></@shiro.hasPermission>',
];
return operateBtn.join('');
}
$(function () {
var options = {
url: "/seq_tool/list/${user.id}",
getDetailsUrl: "/seq_tool/getdetails/{id}",
// title是展示在頁面上的名稱
columns: [
// 其他列
}, {
field: 'operate',
title: '操作',
editable: true,
formatter: operateFormatter //自定義方法,添加操作按鈕
}
],
modalName: "訂單"
};
//1.初始化Table
$.tableUtil.init(options);
//2.初始化Button的點擊事件
$.buttonUtil.init(options);
});
</script>
table.js綁定按鈕操作:
/* [pxy]查看訂單細節 */ $('#tablelist').on('click', '.btn-get-details', function () { var $this = $(this); var orderId = $this.attr("order-id"); var url = options.getDetailsUrl.replace("{id}", orderId); window.location.href=url+"?backurl="+window.location.href; });
后台:
@Controller @RequestMapping("/seq_tool") public class SeqOrderController { @RequiresPermissions("seq_tool:getdetails") @GetMapping(value="/getdetails/{orderid}") public String getDetails(@PathVariable("orderid")String orderid, RedirectAttributes ra) { // 獲取了一些數據 String oid = X.getOrderId(); // ...
// 為了重定向時攜帶數據 ra.addFlashAttribute("orderid", oid == null ? "NULL" : oid);return "redirect:/seq_tool/seq_order_details"; } }
// 涉及權限控制,我把另一塊寫在另一個類
@Controller
public class RenderController {
@RequiresPermissions("seq_tool")
@GetMapping("/seq_tool/seq_order_details")
public String getOrderDetails() {
return "tools/seq_order_details";
}
}
至此,順利跳轉。
