Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML的最佳实践?如何通过提交删除表条目?_Html_Spring_Jsp - Fatal编程技术网

HTML的最佳实践?如何通过提交删除表条目?

HTML的最佳实践?如何通过提交删除表条目?,html,spring,jsp,Html,Spring,Jsp,我想实现一个表,每行中都有一个按钮来删除该行。 现在我在问自己什么是最好的方法 顺便说一下,我正在使用带有Spring标记的JSP,但我认为这是一个常见的html问题 在我看来,我有两种选择:使用链接或在表单中使用按钮 链接示例(删除用户): 删除按钮 用户Id 名称 ${user.userId} ${user.name} 是的,这应该可以,但是删除条目的URL直接在浏览器中公开。不是很节省 按钮(从示例中删除用户): 删除按钮 用户Id 名称 ${user.userId} ${user.

我想实现一个表,每行中都有一个按钮来删除该行。 现在我在问自己什么是最好的方法

顺便说一下,我正在使用带有Spring标记的JSP,但我认为这是一个常见的html问题

在我看来,我有两种选择:使用链接或在表单中使用按钮

链接示例(删除用户):


删除按钮
用户Id
名称
${user.userId}
${user.name}
是的,这应该可以,但是删除条目的URL直接在浏览器中公开。不是很节省

按钮(从示例中删除用户):


删除按钮
用户Id
名称
${user.userId}
${user.name}
这应该也可以,但是每个按钮都有一个表单标记。在浏览器URL栏中提交后,删除条目的URL可能会公开。 也不是最好的方法

那么,你认为什么是最好的方法?或者还有其他更好的方法吗?

切勿使用GET请求(即链接)删除服务器上的数据。我记得一个著名的故事(不确定这是否是一个骗局)一夜之间所有的数据都被删除了,原因是谷歌索引了这个网站并跟踪了所有的链接

如果希望使用表单方式,并且可能希望使用删除请求,那么在spring中,您可以配置
HiddenHttpMethodFilter
,然后在添加名为
\u method
的隐藏字段时,使用值
DELETE

接下来,您需要向表单中添加删除,以确保只有经过身份验证(和授权!)的用户才能使用删除。假设您正在使用(或将要添加)您的最终表单应该如下所示

<form:form action="/users/${user.userId}" method="POST">
    <input type="hidden" name="_method" value="DELETE" />
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
    <button type="submit"><icon:delete/></button>
</form:form>

公开URL是一种用途,因为?尽管如此,它还是公开的,即使您使用AJAX,检索URL也相当简单。您需要的是带有URL的表单,您可能希望在隐藏字段中使用DELETE(因此,您可以使用DELETE方法而不是POST)。最后,您希望添加一个CSRF令牌,以便只有经过授权和验证的用户才能使用删除功能。
<table>
    <thead>
        <tr>
            <th>Delete Button</th>
            <th>User Id</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach var="user" items="users">
            <tr>
                <td>
                    <form:form action="/users/${user.userId}/delete" method="POST">
                        <button type="submit"><icon:delete/></button>
                    </form:form>
                </td>
                <td>${user.userId}</td>
                <td>${user.name}</td>
            </tr>
        </c:forEach>
    </tbody>
</table>
<form:form action="/users/${user.userId}" method="POST">
    <input type="hidden" name="_method" value="DELETE" />
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
    <button type="submit"><icon:delete/></button>
</form:form>
@DeleteMapping("/users/{id}") 
public void remove(@PathVariable("id") Long uid) { ... }