문제

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> 
도움이 되었습니까?

해결책

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

다른 팁

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top