Spring RequestMapping params attribute IE 405 - java

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.

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.

Use common JSP with a custom Servlet and Struts 2

(Oups.. Sorry for my english :) )
In my web application, Struts2 is used as the main Servlet dispatcher and filter. But For some reasons, i have a custom filter and a custom servlet used for a specific url "/book".
But I have some commons jsp... i had some issues when the custom Servlet should display my request attributes in the JSP because of the struts tags (implemented before). So i changed these tag by the jstl taglibs and it works now.
But... In one JSP, the main (lol)... I have a search form.. This JSP is included in several JSPs and could be called by Struts and the custom Servlet..
With only Struts the tag was "< s:form>.." and when the form was submitted, all sended values was kept in the input... But now, because of the custom Servlet i use a simple html form which is calling the struts action "search.do".
As source code is below:
<form method="post" action="<c:out value="${contextPath}"/>/search.do" name="search" id="search">
<input type="text" id="search_searchWord" value="" maxlength="200" size="100" name="searchWord">
<div align="right">
<input type="submit" value="Ok" name="searchButton" id="search">
</div>
<select id="search_searchCrit" name="searchCrit">
<option value="0">Crit1</option>
<option value="1">Crit2</option>
<option value="2">Crit3</option>
</select>
</form>
My problem is the search word and the selected option are refreshed after the submit. I need to keep them !
Is there a way to use the struts taglibs with a Standard Servlet ?
Or Do you have another solution to keep the submitted information ?
Thanks all !
take each field value from the input field and write js function to fill each field in jsp source code of your page.
function selectedValue(){
var value =<%=request.getParameter("searchCrit")%>;
if(value !=null)
{
document.getElementById('search_searchCrit').innerHTML=value;
}
}
I found a solution with the help of #java_seeker.
In my Struts action, i got the request through this way :
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("searchWord", this.getSearchWord());
There is two different way to do this, see: http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2/
The attribute is setted in each method (in the action) that could refresh the page.
Then, i just recovered and set the attribute from the request as a variable with a jstl tag and display it as the value of my html input:
<c:set var="searchWord" value='<%=request.getAttribute("searchWord") %>' />
<input type="text" id="search_searchWor" value='<c:out value="${searchWord}" />' name="searchWord">
For the , i just used an <c:choose><c:when test=""></c:when><c:otherwise><c:otherwise></c:choose> to set the selected choice.
Now all value are always displayed. Maybe it's not the very good way to display share the same JSP between a standard servlet and a Struts action, but it works. I'm open to try a better solution if you have one! Thanks all!

Hide <s:param> from the url

In my jsp page, there is link as follows.
<s:url var="editReqDetails" action="editReqDetails">
<s:param name="siteID" value="siteId"/>
</s:url>
when I click on that link, browser URL is
http://localhost:7101/legal/editReqDetails?siteID=99
like above.(The parameter shows in the URL.)
I want to know how to hide above highlighted part(the parameter) from the url.
If you can use javascript you could do this
<s:a href="#" onclick="window.location.href='%{editReqDetails}'">Edit Details</s:a>
This way you "hide" the url from the user. Though I'm not sure what the big problem is. If the user is malicious he can easily look in the source and get the values.
No, you can't use this. You pass parameter with http GET method that is default used in s:url tag and you want to get http POST method behavior. See the usage of struts url and choose one http GET or POST method.
You can do this:
<form id="edit-form" action="editReqDetails" method="POST">
<input type="hidden" name="siteID" value="siteId" />
</form>
Then:
<script type="text/javascript">
$(document).ready(function() {
$("#your-link").click(function(e) {
$("#edit-form").submit();
});
});
</script>

confusing actions in Struts2 submit button

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.

JSP / servlet / IE combination doesn't submit form detail on Enter

Using IE 7, JDK 1.6 and Sun Web server 7.
Inside the jsp form, we have:
<input type="text" name="id" maxlength="20" />
<input ... type="submit" name="send" value="Send"/>
i.e. a text box and a "Submit" button (called Send).
and the servlet has:
if (request.getParameter("send") != null && request.getParameter("send").trim().length() > 0) { ... }
Using Fiddler and IE, we can see that the following is sent when we populate the id text box and hit Enter:
id=123456
However, using Fiddler and IE, we can see that the following is sent when we populate the id text box and click the Send button:
userId=123456&send=Send
The end result is that hitting the Enter key effectively does nothing.
On other jsp pages, e.g. we have:
<input type="text" name="id" maxlength="20" />
<input ... type="submit" name="submitId" value="Submit"/>
and the servlet has:
if (request.getParameter("submitId") != null && request.getParameter("submitId").trim().length() > 0) { ... }
Using Fiddler and IE, we can see that the following is sent for both cases:
id=123456&submitId=Submit
So it seems to us that the behaviour is only exhibited on IE for forms where the "Submit" button is not called "Submit"?
Re-running the tests on Firefox 3.6 shows that the behaviour is correct and the same for both cases.
Any suggestions for getting IE to work correctly?
(Note: I have searched SO for a similar problem but the questions relating to this mainly all ASP related!).
This is indeed another IE anomaly in case of forms with only one input field. The only solid workaround for this is to add a second input field(!). You can hide it using CSS. No, type="hidden" ain't going to work.
<input type="text" name="id" maxlength="20" />
<input type="text" style="display: none;" />
<input type="submit" name="send" value="Send"/>
Why are you checking for request.getParameter("submitId") in your JSP when in fact submitId is the name of your submit button?
In my experience I never had to check the value for the submit button. I only used that button to trigger the form submit and would usually only be interested in retrieving the values for the other form parameters.
If you want to differentiate the submit methods by the name of the submit button, you might want to try adding a "hidden" property using input type="hidden".

Categories