简体   繁体   中英

JSP dynamic form action attribute without JS?

I need to send http post request by clicking a form submit button. I have a form:

<form action="..." method="DELETE">
    <input type="text" name="id" id="id" />
    <input type="submit" />
</form>

Is it possible to dynamically set the action attribute of the form with the value that's user entered in the text field? Eg

#id=120 -----> action = "users/120"

It's not neccesary to set the action attribute, but is it possible to submit form in such a way without JS , using JSP 's JSTL only

JSP or JSTL - it is server side "template generator"... It cannot know about client side manipulations such as when the user fill the input in.

So there is no way to do it without any front end logic.

This behavior can only be simulated by additional controller method that is redirecting to URL you need.

No it's not. JSP is used to render HTML on server side, it doesn't work on client side, at least it is not developed for it. So you should either render your action on server once or use javascript if you need user interaction (you can also update state of page using AJAX and re-render the page on server).

您可以尝试创建一个Controller(也可以是servlet),该Controller捕获所有到“ users”的POST请求,读取id参数并重定向到“ users / {idReadFromRequestParameters}” URL

If you are absolutely against using javascript, the only solution that comes to my mind works as follows:

  1. submit the action to the server to a specific url, independent from the id
  2. retrieve id from request parameters
  3. redirect to users/id on the server side

The actual implementation depends on what framework you are using on the server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM