<a>标签传递数据给controller
时间: 2023-08-17 13:07:59 浏览: 60
在 HTML 中,<a> 标签通常用于创建超链接。要将数据传递给控制器,可以使用以下两种方法之一:
1. 使用查询字符串传递数据:
在 <a> 标签的 href 属性中,添加查询字符串来传递数据。例如:
```
<a href="/controller/action?param1=value1¶m2=value2">Link</a>
```
这将在 URL 中传递参数 param1 和 param2,其值分别为 value1 和 value2。
2. 使用自定义属性传递数据:
在 <a> 标签中添加自定义属性,例如 data-param1 和 data-param2,然后使用 JavaScript 代码获取这些值,并将它们传递给控制器。例如:
```
<a href="#" data-param1="value1" data-param2="value2" onclick="sendData(event)">Link</a>
```
在 JavaScript 中,可以编写一个函数 sendData 来获取 data-param1 和 data-param2 的值,并将它们传递给控制器:
```
function sendData(event) {
event.preventDefault(); // 阻止默认行为
var param1 = event.target.getAttribute('data-param1');
var param2 = event.target.getAttribute('data-param2');
// 使用 Ajax 或其他方式将数据传递给控制器
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
@RequestMapping("/completed") public String Completed(Model model,HttpSession session) { User user = (User) session.getAttribute("user"); int userid=user.getUserId(); List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,4); model.addAttribute(formList); return "order4"; }我这样写传了List到A页面,那么我在A页面中,写标签进行页面跳转,并且要用到List中的数据作为参数也在中传递到下一个控制器,我该怎么写
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)