UrlEncodedFormResource

The UrlEncodedFormResource class provides examples of the following:

The class is shown below:

UrlEncodedFormResource.java

/** * Sample REST resource for loading URL-Encoded form posts into POJOs. */ @RestResource( path="/urlEncodedForm", messages="nls/UrlEncodedFormResource" title="URL-Encoded form example", htmldoc=@HtmlDoc( widgets={ StyleMenuItem.class }, navlinks={ "up: request:/..", "$W{StyleMenuItem}", "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/$R{servletClassSimple}.java" }, aside={ "<div style='min-width:200px' class='text'>", " <p>Shows how to process a FORM POST body into a bean using the <code>@Body</code> annotation.</p>", " <p>Submitting the form post will simply echo the bean back on the response.</p>", "</div>" } ) ) public class UrlEncodedFormResource extends BasicRestServlet { /** GET request handler */ @RestMethod( name=GET, path="/", htmldoc=@HtmlDoc( script={ "// Load results from IFrame into this document.", "function loadResults(buff) {", " var doc = buff.contentDocument || buff.contentWindow.document;", " var buffBody = doc.getElementById('data');", " document.getElementById('results').innerHTML = buffBody.innerHTML;", "}" } ) ) public Div doGet(RestRequest req) { return div( form().id("form").action("servlet:/").method(POST).target("buff").children( table( tr( th(req.getMessage("aString")), td(input().name("aString").type("text")) ), tr( th(req.getMessage("aNumber")), td(input().name("aNumber").type("number")) ), tr( th(req.getMessage("aDate")), td(input().name("aDate").type("datetime"), " (ISO8601, e.g. ", code("2001-07-04T15:30:45Z"), " )") ), tr( td().colspan(2).style("text-align:right").children( button("submit", req.getMessage("submit")) ) ) ) ), br(), div().id("results"), iframe().name("buff").style("display:none").onload("parent.loadResults(this)") ); } /** POST request handler */ @RestMethod(name=POST, path="/") public Object doPost(@Body FormInputBean input) throws Exception { // Just mirror back the request return input; } public static class FormInputBean { public String aString; public int aNumber; @Swap(CalendarSwap.ISO8601DT.class) public Calendar aDate; } }

The localized messages are pulled from the resource bundle:

UrlEncodedFormResource.properties

#-------------------------------------------------------------------------------- # UrlEncodedFormResource labels #-------------------------------------------------------------------------------- aString = A String: aNumber = A Number: aDate = A Date: submit = submit

The $R variables are request string variables.
In this case, $R{resourceTitle} and $R{resourceDescription} resolve to the values returned by {@link oajr.RestRequest#getResourceTitle()} and {@link oajr.RestRequest#getResourceDescription()}.

Pointing a browser to the resource shows the following:

http://localhost:10000/urlEncodedForm

Entering some values and clicking submit causes the form bean to be populated and returned back as a POJO response:

http://localhost:10000/urlEncodedForm

Additional Information