Spring MVC REST PUT is not working - rest

I have the following problem: Whenever I try to edit the following data, I get an exception saying that HTTP PUT is not supported by this URL. This is the JSP form side of things where a button is used to edit that row:
<c:url var="formAction" value="/circuits/${circuit.circuitId}" />
<form:form method="PUT" action="${formAction}">
<input type="hidden" name="circuitId" value="${circuit.circuitId}" />
<input type="submit" value="Edit" class="btn btn-primary" />
</form:form>
And the following is my controller method that retrieves the circuitId:
#RequestMapping(value = "/{circuitId}", method = RequestMethod.PUT)
public String showEditCircuitForm(#PathVariable Integer circuitId, ModelMap model) throws NoSuchRequestHandlingMethodException
However, when I use the normal GET method everything works fine, it's just this PUT method that is causing the problem. I have got all the dependencies that I need.

Only getand post are officialy supported with forms. I'm not aware of any browser that supports put with forms. It is supported with AJAX, though.
Apart from that the support of the Java Servlet API for PUT is not ideal. You might need to include a filter in your web.xml:
<filter>
<filter-name>httpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
Before I forget: You can send a post request and use another filter to fake a put request:
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
To get this working you have to include a hidden field with the name _method and the value PUT in your form.

Related

WSO2 IS 5.10 Self sign-up in only one form

I'm working on customizing the WSO2IS Self registration.
The Self registration has two parts :
Ask the username and verify the validity and unicity.
Ask others claims in a form
What I want is only a form with the username and the claims input.
I manage to add the username input on the second page but I want to add the username verification and to remove the first page.
When I change the web.xml it returns me that error :
ERROR {org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/accountrecoveryendpoint].[register.do]} - Servlet.service() for servlet [register.do] threw exception java.lang.StackOverflowError
Can anyone help me? Thanks
Edit -My changes that works:
I added the username field in the second part of form
<div class="username field">
<img class="mail icon-form" src="../accountrecoveryendpoint/images/mail-fill.svg">
<div class = "username required field">
<label for="username">
<%=IdentityManagementEndpointUtil.i18n(recoveryResourceBundle,"Username")%>
</label>
<input id="username" name="username" type="text" class="username form-control" required>
</div>
</div>
It overrides the username used for the first part and if it is not valid or unique WSO2 won't create it but the form is sent.
In web.xml I tried to change the jsp file of the register.do servlet.
<servlet>
<servlet-name>register.do</servlet-name>
<jsp-file>/self-registration.jsp</jsp-file>
</servlet>
But it sent error

How to hide Url values in Play Scala?

How to hide the values in the URL? In routes:
GET /admin/:userId com.example.sample.getUser(userId: Int)
When getUser action is requested, URL returns in the format as shown below:
http://localhost:9000/admin/0
I don't want to view the userId in the URL.
This is common GET vs POST difference. In GET request params are sent within the URL, in POST are not.
If you need to avoid params in the URL just use a HTML form with hidden field instead of a tag to send the requests and in your controller fetch the POST arguments, dummy HTML sample:
<form action="/admin/" method="POST">
<input type="hidden" name="userId" value="#userId" >
<input type="submit" value="#userName" >
</form>
It will give you submit button with name of the user as a label, you can use CSS to give it feel and look of common link.
Note that's invalid REST pattern, as POST shouldn't be used to showing data, you don't mention what is the purpose of this, so maybe you should rethink your needs.
Maybe you can also just make usage of parameters with default or fixed values:
check the docs for more options

How to attach a listener like oncomplete to p:button

I have the following code :
<h:form id="facebookForm">
<p:button value="Facebook Connect" href="#{loginPageCode.facebookUrlAuth}">
<p:ajax event="click" listener="#{loginPageCode.getUserFromSession}" update="growl confirmDialog" ></p:ajax>
</p:button>
<p:confirmDialog id="confirmDialog" message="Welcome"
header="Welcome to our website!" severity="alert" widgetVar="confirmation">
<h:panelGrid columns="1" cellpadding="10">
<h:outputText id="msg" value="Salutation : #{user.salutation}" />
<h:outputText id="msg1" value="Name and surname: #{user.name} #{user.surname}" />
<h:outputText id="msg2" value="Birthday : #{user.birthDate}" />
<h:outputText id="msg3" value="Email : #{user.email}" />
<h:outputText id="msg4" value="Postal code : #{user.postalCode}" />
<h:outputText id="msg5" value="Phone nr : #{user.phoneNumber}" />
</h:panelGrid>
<p:commandButton id="confirmation" value="Ok" onclick="confirmation.hide()" type="button" />
</p:confirmDialog>
<p:growl id="growl" life="3000" showDetail="true" />
</h:form>
I am trying to connect to facebook from my webpage, through a custom servlet which handles all the stuff regarding validation, parsing response, etc. The href inside above form contains the url to facebook authentification, which in the end redirects everything to the current page, which is index.xhtml.
What I want to do is to call a method after everything was done, something like onComplete attribute from <p:commandButton> component, because I want to display a dialog with updated managed bean, <p:confirmDialog>, and to welcome. So I need to first go to the first URL, wait until autenthification is finished, then call this method #{loginPageCode.getUserFromSession} which dinamically updates the managed bean under the hood by parsing JSON from facebook http response, and then to open the dialog which contains updated values.
I have tried also to use <p:commandButton> with type="button" to use action / actionListener / update capabilities, but unsuccessful.
Can you please advise?
Thank you.
You're overthinking/misgrasping how HTTP and ajax in general work. It's not possible to have an ajax oncomplete hook on a synchronous GET request.
Just do the job in the (post)constructor of the request/view scoped associated with index.xhtml which FB authentication is ultimately redirecting to, if necessary based on some specific request parameters which you've specified in index.xhtml callback URL.
Basically, you need to do most of your processing in the back. Try this
Define a preRenderView event in which you'll do most of the postprocessing of the facebook login. This event will be defined in index.xhtml. Your code will look something like this
<f:metadata>
<f:event type="preRenderView" listener="#{loginPageCode.postFBProcessing}" />
</f:metadata>
In postFBProcessing, you'll carry out whatever processing you need to do after return from FB (processing the JSON response, getting user from session etc).
Define a boolean dialogVisible in your backing bean and bind the <p:dialog/>'s visible property to the variable OR use RequestContext to update the dialog.
<p:confirmDialog id="confirmDialog" message="Welcome" visible="#{loginPageCode.isDialogVisible}" header="Welcome to our website!" severity="alert" widgetVar="confirmation">

generate querystring

I have created one jsp form which contains the username textfield. On the click of submit button it pass on to servlet and read parameter and display on the screen.
I want to generate a querystring of username with the url.
Can anyone tell me how can i do that??
The following example will add the username field in the querystring of the request URL.
<form action="servletURL">
<input type="text" name="username" />
<input type="submit" />
</form>
Note that there's no method. It defaults to GET already, which means that all form data is passed by URL.
If you still don't see the querystring in the request URL, then it means that your servlet is performing a redirect after submit.
response.sendRedirect("result.jsp");
The enduser will then see the redirected URL in browser address bar instead. If you don't include the querystring in the redirect URL, then the enduser will indeed not see it at all.
You should either be doing a forward() instead,
request.getRequestDispatcher("/WEB-INF/result.jsp").forward(request, response);
or append the query string yourself:
response.sendRedirect("result.jsp?" + request.getQueryString());

grails form redirect gives 404, but url mapping works

I feel like I've entered the Twilight Zone. I have a Grails form that redirects to a URL, but gives a 404. If I go directly to that exact URL, everything works fine (that is, the URL mappings are correct).
This is an example generated form tag:
<form action="/alm/contactRefresh/itemRefreshProcess/7070" method="post">
On submit, it redirects to:
http://localhost:8080/alm/contactRefresh/itemRefreshProcess/7070
But gives this error:
HTTP ERROR 404
Problem accessing /alm/contactRefresh/itemRefreshProcess/7070. Reason:
NOT_FOUND
Powered by Jetty://
But then if I just go directly to that same URL (by focusing the browser Location bar and pressing enter), the page renders just fine, though the form params are lost because it's just a GET now. I've also tried changing the form method to GET, and that doesn't work either (throws a 404).
I've done similar forms a zillion times before with no problems. I'm sure this is some stupid user error, but I seriously can't figure out what's wrong.
Thanks for any ideas!
So, I finally started ripping parts out of the form and found out that for some reason you can’t name a Grails checkbox starting with the word "action". It must be something related to the default params["action"] entry. Though my checkbox names were a concatenation of "action_" + an id.
Anyway, there was some kind pre-processing of the checkbox form params that was blowing up before making it to the controller, and somehow that translated to a 404 instead of an actual Exception.
Originally I had this:
<g:checkBox name="action_${serviceRefreshAction.id}" value="${true}" />
Which renders this:
<input type="hidden" name="_action_7196" /><input type="checkbox" name="action_7196" checked="checked" id="action_7196" />
I changed "action" to "myAction", like this:
<g:checkBox name="myAction_${serviceRefreshAction.id}" value="${true}" />
Which renders this:
<input type="hidden" name="_myAction_7206" /><input type="checkbox" name="myAction_7206" checked="checked" id="myAction_7206" />
And now everything works fine.
Five hours of my life down the drain.
But I guess I have to forgive Grails, for the all time it saves me on a daily basis normally. :o)