gpt4 book ai didi

Spring MVC Java why are extra request params being added to my url?(Spring MVC Java为什么要向我的url添加额外的请求参数?)

转载 作者:bug小助手 更新时间:2023-10-25 10:36:28 26 4
gpt4 key购买 nike



I have a website, called website.com. I have a form which looks like this:

我有一个网站,叫WebSite.com。我有一个表格,看起来是这样的:


<form:form method="get" id="profileForm" modelAttribute="profile" onsubmit="updateFormAction()">
<form:label path="username">Name</form:label>
<form:input path="username" id="usernameInput"/>
<input type="submit" value="Submit"/>
</form:form>

<script>
function updateFormAction() {
var username = document.getElementById("usernameInput").value;
var form = document.getElementById("profileForm");
form.action = "/profile/" + username;
}
</script>



What the form url should look like is this: website.com/profile/username123

形式的url应该是这样的:website.com/profile/username123


However, it is processing it as website.com/profile/username123?username=username123

但是,它将其作为website.com/profile/username123?username=username123处理


This is my web controller mapping method:

这是我的Web控制器映射方法:


@GetMapping ("/profile/{username}")
public String profile(@PathVariable("username") String name, Model model, RestTemplate restTemplate) throws JsonProcessingException {

When I set <form method="post" it works. However, I do not want this behaviour, as I can only post that url through that form. I want to be able to search it via a link instead.

当我设置


I have tried @ModelAttribute("username") String username, however the url looks like website.com/profile/?username=username123

我尝试了@ModelAttribute(“Username”)字符串USERNAME,但是url看起来像Website.com/Profile/?Username=Username123


Any ideas how I can fix this? Should I not be using a form, but a button onsubmit parameter instead?

你知道我怎么才能解决这个问题吗?我应该不使用表单,而是使用按钮onSubmit参数吗?


更多回答

You should send the request through JS not modify the form action. Because the GET will always add the parameters to the URL. So instead of rewriting the URL in the form you should be using fetch to send the request (and return false from your JS function to prevent the default from happening).

您应该通过JS发送请求,而不是修改表单操作。因为GET总是将参数添加到URL。因此,您应该使用FETCH发送请求(并从JS函数返回FALSE以防止默认情况发生),而不是在表单中重写URL。

优秀答案推荐

If you don't pass any parameter in the url then you should not submit the form. Just return false from the onsubmit function. This will prevent form submission or use a button type="button" with onclick event handler. Then use redirect directly to the url specified.

如果您没有在URL中传递任何参数,那么您就不应该提交表单。只需从onmit函数返回False即可。这将阻止表单提交或使用带有onClick事件处理程序的按钮type=“Button”。然后使用重定向直接指向指定的URL。


<script>
function updateFormAction() {
var username = document.getElementById("usernameInput").value;
window.location.href = "/profile/" + username;
return false;
}
</script>


Why are you adding username in path variable ?
I think you can remove onSubmit attribute and for action add /profile and make your http verb to post in form and controller .
You can create an object called profile that has a field called username;
In your controller in inputs add

为什么要在PATH变量中添加用户名?我认为您可以删除onSubmit属性和for action添加/配置文件,并使您的http动词在表单和控制器中发布。您可以创建一个名为Profile的对象,该对象有一个名为UserName的字段;在您的控制器中的Inputs中添加


@ModelAttribute("profile") Profile profile;

then you can get username in profile sth like this :

然后,您可以在配置文件中获取用户名,如下所示:


@PostMapping ("/profile")
public String profile(@ModelAttribute("profile") Profile profile) {

更多回答

If this answer helped you then you should mark it as accepted. The checkbox I'd on the left of the answer. Also it's worth to vote up the answer that helped you the same to help others to choose the correct answer.

如果这个答案对您有帮助,那么您应该将其标记为已接受。我在答案左边的那个复选框。此外,投票支持同样帮助你的答案也是值得的,以帮助其他人选择正确的答案。

I have tried this already, however with POST requests I can only access a resource from the form itself, not from a link.

我已经尝试过了,但是对于POST请求,我只能从表单本身访问资源,而不能从链接访问。

With a post it doesn't matter if it is a request parameter or a form parameter it will bind either way.

对于POST,无论它是请求参数还是表单参数,它都将以任何方式绑定。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com