Question

I have a form which sends a request to the controller.

<form method="POST" action="/user/{id}">
    <input type="text" placeholder="Input Id">
    <button>Get User</button>
</form>

I need to add inputed value from to the action of form. How can I do it? For example, if user input '2', my action must be action="/user/2".

UPD:

Can I use somthing like this (see below)?

<form:form id="myForm" method="POST" action="">
    <input type="text" id="userId" placeholder="Input Id">
    <button onclick="setId()">Get User</button>
</form:form>

And script:

<script>
        function setId (){
            var id = document.getElementById('userId');
            document.getElementById('myForm').action  = '/user/'+id;
        }
</script> 
Was it helpful?

Solution

If you like to try this in Javascript as Sotirios Delimanolis mentioned, try this

document.getElementById('form_id').action  = '/user/{id}'

ie:

Do this on button submit or window.onload or on, any other event handling,

var id = document.getElementById('inputfieldid').value;
document.getElementById('form_id').action  = '/user/'+id;

here is another example in SO using struts: Change action attribute of Form for different action methods in Struts2

Note: In your case, you want the id from HTML input field, hence we have to go for javascript

OTHER TIPS

sample like this:

    <form method="post" action='UserController' >

        EMPLOYEE NAME: <input type="text" name="empName" 
            value="<c:out value="${user.empName}"/>"/><br/>

    <input type="submit" value="Submit"/>
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top