Spring MVC - multiple submit buttons handled in single #requestmapping - java

I have a form with 2 submit type buttons(Yes/ No), i would like to handle this form with single #RequestMapping in my controller class. I certainly wish to handle multiple submit in single request mapping method only.
My first question is this possible. Can multiple submit buttons be handled with single request mapping of form action in the controller class ?
If yes, then below is the code I have written. Please suggest if this a correct way of implementing it or if it needs to be updated.
Currently, my code looks like this:
Form.jsp:
<form:form action="doAction">
<input type="submit" name="buttonClick" class="button" value="yes, do Someting" />
<input type="submit" name="buttonClick" class="button" value="no, do nothing" />
</form:form>
Controller.java:-
private String buttonClick;
#RequestMapping(value = "/doAction", method = RequestMethod.POST, params="buttonClick") {
if("yes, do Something".equalsIgnoreCase(buttonClick))
//
else if("no, do Nothing".equalsIgnoreCase(buttonClick))
//
}

You can change the form action on button click e.g. to
"doAction?buttonClick="+<some value from clicked button>.
Or introduce a hidden input in the form. On click change the input value to reflect clicked button. Then the input is available on server side.

You can use a hidden field and change its value on every button click using jQuery:
<form:form action="doAction">
<input type="hidden" name="buttonClick" id="buttonClick" />
<input type="submit" name="buttonClickYes" class="button" value="yes, do Someting" />
<input type="submit" name="buttonClickNo" class="button" value="no, do nothing" />
</form:form>
<javascript>
$(document).ready(function(){
$("input[type=submit]").click(function(){
$("#buttonClick").val($(this).val());
return true;
});
});
</javascript>

Related

Passing Data from JSP to Servlets from HTML page dynamically

I have a JSP page which reads data from HTML and has CSS,Jquery code in it .
Now my webpage in jsp has two text labels and a add button next to them.
User can enter any no of values in the text field.
Now my requirement is that every time user enters the alue in these fields and clicks on add then that data should be passed on to my servlet. Servlet will basically do some validation and return a boolean variable.
Based on the value of this boolean, I shall change the appearance of my text boxes.
This is required to be done for every time user clicks on Add button.
How can I achieve this ?
My HTML code :
<div id="id1" name="id1" style="display: none;">Add a node: </br>
<input type="text" name="ipaddress" id="ipaddress" placeholder="Enter Node IP"> <input type="text" name="port" id="port" placeholder="Enter Node Port">
<input type="button" value="Add" name="addnodebutton" id="addnodebutton"/>
</div>
The value in ipaddress and port shall be passed on to my servlet and depending on return parameter, their appearance should change.
Can anyone enlighten me how this is actually going to work ?
TIA :)
For passing data to and from a servlet, you have options.
Option 1- You can wrap your html in a form tag and set the action/method properties for your servlet/http method like below:
<form method="POST" action="servletname">
<input type="text" name="ipaddress" id="ipaddress" placeholder="Enter Node IP">
<input type="text" name="port" id="port" placeholder="Enter Node Port">
<input type="submit" value="Add" name="addnodebutton" id="addnodebutton"/>
</form>
The submit would send a request with the input to your servlet. You would then need to handle your request parameters in your servlet, set your values/flags in your response object and forward to the user or jsp/html page of your choice.
Option 2- You can make an ajax call from your jsp, process your input and return a response to your page asynchronously. Example below:
A Simple AJAX with JSP example

How to send label parameter from JSP to servlet?

I have a jQuery dialogue box which contains values as checkboxes. On selecting the checkboxes I am storing the selected values into label. Next I have to send these values from label as parameter through form to servlet but I don't know how to complete it.
Here is my code:
<form action="CallTimer" method="GET">
<label class="button2">Set Date: </label>
<input type="text" name="date" id="date" size="4">
<input type="Submit" name="Submit" value="Submit" id="Submit">
<br/>
Select Reporting Level
<label class="button2" style="display:none" id="depart"> Department</label>
</form>
I am retrieving these parameters in my Servlet as:
String reportname=request.getParameter("depart");
System.out.println(reportname);
But it is returning null values. Please help me.
Thanks in advance.
You have to use hidden input field:
<input type="hidden" name="depart" />
You need to understand what gets passed on form submission and what is not. In a nutshell, only values of the input fields get sent to the server. You have several ways to solve your problem:
Write value to a hidden input field
Modify the query string (what gets sent after ? in your GET request) during form submission (using java script):
?...&depart=xxx

Hidden input in JSP produces null when passing it to the servlet

In my JSP I do the following :
<!-- Bank manager's permissions -->
<!--more stuff goes here -->
<fieldset>
<legend>To open a new account</legend>
<form action="blablabla">
<input type="hidden" name="hdField" value="myValue" /> // note I pass a "myValue" as string
Press here to continue
</form>
</fieldset>
And in my Servlet I grab the hidden input :
#WebServlet("/employeeTransaction1")
public class Employee1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String getHiddenValue=request.getParameter("hdField");
System.out.println("Hidden field Value :"+getHiddenValue);
// forwards to the page employeeOpenNewAccount.jsp
request.getRequestDispatcher("/WEB-INF/results/employeeOpenNewAccount.jsp").forward(request, response);
}
}
And System.out.println produces : null at the Console
Why do I get a null of not the actual value is I pass ?
Regards
EDIT:
After changing to :
<fieldset>
<legend>To open a new account</legend>
<form action="/employeeTransaction1" method="GET">
<input type="hidden" name="hdField" value="myValue"/>
Press here to continue
</form>
</fieldset>
A null is still presented at the console .
What you are trying to do is to send a form to the server. But, in fact, you don't do that. You just issue a GET request (when the user clicks your link: Press here to continue)
If you want to send the form make sure you set the attributes of the form tag properly and add a submit button to the form:
<form action="/employeeTransaction1" method="GET">
...
<input type="submit" value="Submit" />
...
</form>
Depending on your preferred way of sending the form, you can change the method="GET" paramater to method="POST" and make sure that in the servlet you handle the form in the doPost() method
Alternatively, if your purpose is not to send the from to the server but just to pass the value of the hidden input, you should add its value as a prameter encoded in the GET request. Something like:
/employeeTransaction1?hdField=myValue
To achieve this, you need some client processing, i.e. when the user clicks the link, the hidden input should be added to the get and then the request should be issued.
Using an href tag does not submit your form, i.e. it does not pass the parameters defined in the form to the request. You should use input type="submit" or button tags instead. Also make sure the form action matches your #WebServlet definition.
<fieldset>
<legend>To open a new account</legend>
<form action="/employeeTransaction1">
<input type="hidden" name="hdField" value="myValue" /> // note I pass a "myValue" as string
<input type="submit" value="Submit" />
</form>
</fieldset>

request.getParameter("String") always returns null. Why?

// jsp snippet
<button name="TesterButton" value="TesterButton" onClick="location.href='TesterServlet';">
Servlet Snippet of class TesterServlet
writer.println( (String)request.getParameter("TesterButton") ); // always returns null
The above servlet always returns null. Why does it return null ?
It's working exactly as you told the code to do: changing the window location to the given URL. So, no surprises.
What you actually want should be coded as follows:
<form action="TesterServlet">
<input type="submit" name="TesterButton" value="TesterButton" />
</form>
Or if you really insist in using JS for this for some reason:
<button name="TesterButton" value="TesterButton" onclick="location.href='TesterServlet?TesterButton=TesterButton';">
Note that the use of onClick instead of onclick indicates that you're possibly reading outdated or poor HTML resources/examples.
See also:
HTMLdog - HTML beginner tutorial
W3 HTML forms specification
The button is not send in the request even if is inside a form.
UPDATE:
If you want to send the value of the clicked button (kinda strange) you need to use a hidden element and set the value before call the servlet:
<form id="myForm" method="post">
<input type="hidden" id="hidButtonValue" />
<button id="TesterButton" value="TesterButton" onclick="document.getElementById('hidButtonValue').value=this.value; location.href='TesterServlet';"
</form>
Now you can call the hidden in your servlet
writer.println( (String)request.getParameter("hidButtonValue") );
Also, it would be better to encapsulate the onclick code in a JavaScript function.
A quick and easy way would be using a form with a hidden field:
<form action="TesterServlet">
<input type="hidden" name="testField" id="test" value="SomeValue" />
<input type="submit" value="Submit" />
</form>
Then in your servlet:
writer.println( (String)request.getParameter("testField") ); // Should return "SomeValue"

How can I tell which submit button was clicked

I have several different submit buttons on my JSP in one form tag that all point to the same servlet. I need to know which submit button was clicked. How can I find out which button was clicked?
if request.getParameter("button-name") is not null then this is the button that was pressed
Each Submit button should have a different name:
<input type="submit" value="This is a submit button" name="submit1">
<input type="submit" value="Another submit button" name="submit2">
<input type="submit" value="Yet another submit button!" name="submit3">
Then, the name of the input should appear in the parameters sent to wherever the form is posting to, something like
post.jsp?key=value&submit3=&....
http://www.w3schools.com/tags/tag_input.asp
This is kind of similar to the DispatchAction in Struts. What they do is to have a hidden field, and when you submit the form, have onClick() set the value to specify which action is taken.
<input type="hidden" name="dispatchAction"/>
<input type="submit" value="Edit" onClick="setDispatchAction('edit')">
<input type="submit" value="Delete" onClick="setDispatchAction('delete')">
<button type="submit" name="somename" value="button1">some text</button>
<button type="submit" name="somename" value="button2">some other text</button>
you will have the post variable "somename" set to the according value, no matter the dispalyed value.

Categories