confusing actions in Struts2 submit button - java

I am trying to update the existing code of other developer. I am facing the problem of confusing actions.
Existing :
<s:form name="f2" action="delFood.action">
<input type="submit" value="Delete" class="button" onClick="javascript:get_check_value()"/>
My Code to Update:
<input type="file" class="button" id="foodItemFile" name="foodItemFile" value="Browse ..."/>
<input type="submit" class="button" value="AddFood" onClick="callAddFood();"/>
My Javascript:
In my script, I try to submit my action by following code.
document.f2.action = "AddFoodAction.action";
document.f2.submit();
It seems like when I click [AddFood] button, it always call the [delFood.action].
For adding food, I need to check something with javascripts before calling the [AddFoodAction.action] action.
Due to limitations, I can't change the existing code. I can only add new codes to the existing one.
So, Any way to call [AddFoodAction.action] from javascripts without confusing with other actions of the same form ?
Thanks ahead.

Looks like a javascript problem here. Make sure the code
document.f2.action = "AddFoodAction.action";
document.f2.submit();
is executing. Maybe document.f2 is not resolving correctly (maybe more than one form with this name?).
This fiddle shows that it should work. It changes the action of a form inside a onclick handler on a <input type="submit">.
And just a reccomendation, don't do document.f2.submit();. It's an <input type="submit"> so it will submit the form automatically when onclick ends.

Finally, I resolve it by doing like that.
1) There will no direct action in form tag.
<s:form name="f2" method="post" enctype="multipart/form-data">
2) for delete part,
<input type="submit" value="Delete" class="button" onClick="javascript:get_check_value()"/>
call the delete action from the javascript, not directly from form.
document.f2.action = "delFood.action";
3) for add part, like delete part. check necessary things in java scripts and call the add action. It works well.
Another Solution:
There maybe common action directly called from Form. For this approach, just name your button and give value and map those value from action class. And differentiate multiple methods by using those value from one action. I read this article at coderanch and javaSample. Thanks.

Related

HTMLUnit thinks Submit Button is a text input

I am attempting to process a form in Java with HtmlUnit. It works fine until it tries to find and click the submit button.
Here is what the form looks like,
<form method="get" action="result.php">
<p>Text: <input type="text" name="text"/></p>
<p>Agree: <input type="checkbox" name="doYouAgree" value="agree" /></p>
<p><input type="submit" name="Submit" value="Submit"/></p>
</form>
I've searched around a tried many different methods for retrieving the element but it consistently returns HtmlTextInput rather than HtmlSubmitInput.
form.getInputByName("Submit").click();
I've also tried processing a form with every type of input and no matter the type it always returns HtmlTextInput.
Has anyone seen this issue or know how to correct it? I'm concerned that this is the reason why HtmlUnit is not submitting any forms.
You can submit a form with a base object, as DomElement.
DomElement button = page.getFirstByXPath("//input[#name='Submit']");
HtmlPage new_page = button.click(); // or you can use the old page
should work.

How to set value with button to input text in wicket

I have input text:
<div><label wicket:for="name">Name</label><input type="text" wicket:id="name" /></div>
Now I need to add two buttons which just add to this input text some value. How can I implements this ?
with this buttons I dont want do send form just edit value in input rext
You don't have to send the whole form. You could use an AjaxButton with an onClick-method changing the model of your input field and adding the field to the AjaxRequestTarget. Anything not involving a server roundtrip at least by Ajax would be hard to do using wicket methods since wicket runs on the server. Of course you could do this by JavaScript but that wouldn't involve wicket.
Use <input type="button">Click Me</input>.
and at onclick event of these buttons call a javascript function which will edit the input text.
This has nothing to do with Wicket, this is just plain Javascript.
You can do something like this
<div>
<label wicket:for="name">Name</label>
<input type="text" wicket:id="name" id="markupId"/>
<input type="button" value="Do something" onclick="javascript:editField();"/>
</div>
With the following Javascript somewhere in your page (or in loaded script files):
function editField(){
document.getElementById("markupId").value = "My value";
}
If you use a Javascript framework like JQuery, you should bind the function to the onclick event using your framework (prevents polluting HTML with your javascript)

Spring RequestMapping params attribute IE 405

I have a RequestMapping in Controller:
#Controller
class aController{
...
#RequestMapping("/action", method=RequestMethod.POST, params="actionName"){
//some logic
}
}
In JSP:
<form action="/action" type="POST">
<input type="submit" name="actionName" value="actionName">
<input type="submit" name="xyz" value="XYZ">
</form>
The problem is the request from JSP gets mapped when Chrome or Firefox browsers are used. But in IE it fails to latch on to the Request Mapping, because the way IE handles button names. To resolve this issue I have to add following:
<input type="hidden" name="actionName" value="actionName"/>
The problem is there are several pages with multiple buttons mapped to different RequestMapping(params=""). I don't want to start adding hidden fields everywhere. Is there any better way to resolve this stupid IE issue?
According to me you should use javascript to handle onclick events of your submit buttons and then from that function you can change the action of your form tag. Try adding the required parameters to the action and declare the form method as GET.
I think this should resolve your problem.
Cheers.
I solved the IE issue by using a hidden field with the required actionName of button passed as the value :
<input type="hidden" name="actionName" value="actionName">
This solution solves the issue I face on IE browser.

Run script only on button click in JSP

Iam new to JSP and i can't seem to figure out how to run code only when the user clicks on the button..here's my code:
$
<form action="list_computers.jsp" method="post">
Search:
<input type="text" name="FromTextBox1"/>
<input type="submit" value="Search it!" >
<%
String TheSearch = (String)request.getParameter("FromTextBox1");
String GetIt = Searcher(TheSearch);
out.println(GetIt);
%>
</form>
The Searcher() is a function i declared above this. Any help would be really appreciated.
You need to do something like
if (request.getParameter("submit") != null) {
// do work here
}
You also need to give a name to your button
<input type="submit" value="Search it!" name="submit">
When user clicks (or presses enter), request['submit'] will be equal to "Search it!"
I highly recommend moving this logic to the top of the page or even better to a controller.
you need to use javascript to check for the onclick event here's a little example with JQuery
$("input[type='submit']").click(function(){
//do your thing
// use event.preventDefault(); to stop submission if need
});

Dynamic Forms in Spring

I am trying to make a dynamic form using Spring forms. Basically, the form gets a title of learning activity and then there's the a button below it that says, "Add one more Learning Activity". This makes it possible for the user to add one more learning activity. I want him to be able to add as much as he likes.
I haven't tried this before so obviously I encountered errors with the first solution I thought of. I really had a feeling doing what I did will generate an error but just do drive home what I am trying to do, here's the code:
<script language="javascript">
fields = 0;
function addInput() {
document.getElementById('text').innerHTML += "<form:input path='activity[fields++].activity'/><br />";
}
<div id="text">
<form:form commandName="course">
Learning Activity 1
<form:input path="activity[0].activity"/>
<input type="button" value="add activity" onclick="addInput()"/>
<br/><br/>
<input type="submit"/>
</form:form>
<br/><br/>
</div>
You can't use <form:input> within the javascript because is a jsp tag that runs on the server-side.
However, there's nothing magical about how an HTML input gets bound to a field in the Spring command object; it's just based on the name. So in your javascript, add a new
<input type="text" name="activity[1].activity">
(for example -- obviously you'll increment the index).
Another option I've used for more complicated controls is to grab the HTML of the existing control (which was created using the Spring form:input tag) and clone it, replacing the indexes with the incremented number. This gets a lot easier if you use jQuery.
EDITED TO ADD:
One issue that may cause you problems: you're appending your new input box to the end of your outer div ("text"), which means it's not inside the form tags. That won't work.
Is <form:input> a JSP tag? If so, then client-side Javascript to add <form:input> nodes to the DOM will have no effect - since the server-side JSP engine is not interpreting those.
Your Javascript needs to work with raw HTML and DOM, such as adding an <input> element.

Categories