Ljava.lang.String is displayed when I accesed param in thymeleaf Spring - spring-mvc

I have a problem while accessing the page parameter in thymeleaf.
Please see my below code :
<input type="hidden" name="resetKey" th:value="${param.key}"/>
I am trying to access parameter key from query string and send it as hidden parameter to the server.
After page loads, it is displayed like below :
<input type="hidden" name="key" value="[Ljava.lang.String;#b1fd0f9">
I want to send the key back to the server when the form is submitted.

You are trying to print a multi-value, a String array. To print the first value:
<input type="hidden" name="resetKey" th:value="${param.key[0]}"/>
Or this should work too:
<input type="hidden" name="resetKey" th:value="${#request.getParameter('key')}"/>
Source: Spring MVC and Thymeleaf: how to access data from templates

Related

Form Submission on Enter Press

I have a form in my ASP .NET project that takes the users input, and appends it to a URL to search a wiki. The form works perfectly when you enter in a search term, and click the 'search' button, however when you type into the input box and hit enter, the page refreshes and the box clears.
my html
<form>
<label id="sideBarLabel"> Services
<input type="text" placeholder="Search Wiki: e.g. E911" name="queryString" id="query-string" />
</label>
Search Wiki
</form>
my js
function searchWiki(){
var siteQuery = $('#query-string').val();
window.location.href = "/dosearchsite.action?queryString=" + siteQuery;
}
Can anyone help ?
Your searchWiki() js method is only called when the evenement onclick is raised on your button, but not when your form is submitted.
There is several ways to achieve what you want:
Add a js method to catch the onsubmit event of your form:
$("form").on("submit", searchWiki());
Or add a tag on your form:
<form onsubmit="searchWiki()">
Or specify the action attribute on your form:
<form action="/dosearchsite.action">
Note that in ASP.NET, the ModelBinder will link your form inputs to your controller action parameters, using the name attribute of the inputs. That means that you should not specify them in the url yourself.
You should also declare your form using Html.BeginForm or Ajax.BeginForm if you want your form to be submitted by ajax.
#Html.BeginForm("ActionName", "ControllerName")
{
<label id="sideBarLabel">Services
<input type="text" placeholder="Search Wiki: e.g. E911" name="queryString" id="query-string" />
</label>
<input type="submit" class="button" value="Search Wiki"/>
}
This will call searchWiki when you press enter.
$('#query-string').keypress(function(event) {
if (event.keyCode == 13) {
searchWiki();
event.preventDefault();}});

Google Oauth-2.0 returns url with hash instead of question mark

I using this form to request Google Offline Token
<form action="https://accounts.google.com/o/oauth2/auth" methode="POST">
<input type="hidden" name="access_type" value="offline" />
<input type="hidden" name="client_id" value="XXX" />
<input type="hidden" name="scope" value="https://www.googleapis.com/auth/analytics https://www.googleapis.com/auth/analytics.edit https://www.googleapis.com/auth/analytics.manage.users https://www.googleapis.com/auth/analytics.manage.users.readonly https://www.googleapis.com/auth/analytics.readonly" />
<input type="hidden" name="response_type" value="code token gsession" />
<input type="hidden" name="redirect_uri" value="http://example.com/analytics" />
<input type="hidden" name="approval_prompt" value="force" />
<button>Get or Refresh token</button>
</form>
How come Google sends me back an URL like :
http://example.com/analytics#access_token=XXX&token_type=Bearer&expires_in=3600&code=YYY&authuser=0&num_sessions=1&prompt=consent&session_state=ZZZ
Please notice we have an hash code # instead of a question mark ? after main url part.
I was expecting to get above variables with PHP $_GET but I can't because that hash code.
My question is how can I get an url with question mark instead ?
You're using a response_type of code token gsession that triggers a so-called Implicit flow in which the access_token will be returned in the URL fragment. Use response_type=code to get a code value as a query parameter that you can use at the token endpoint to exchange it for an access_token as described in: https://developers.google.com/accounts/docs/OAuth2WebServer#handlingtheresponse

Retrieve non-form-element value through form post in Spring 3 MVC Controller Class

When I submit this:
<form:form action='/controller.do' method='POST'>
<input type='text' value='test' name='myName" />
<input type='submit'/>
</form:form>
How can I retrieve the value of 'myName' in Spring 3 MVC Controller without using form tag?
If I understand your question correctly, you put some plain HTML <input> tags inside Spring MVC tag library's <form:form> tag.
Though it may work, it probably would be a better idea to use (depending on your situation) either plain HTML:
<form action='/controller.do' method='POST'>
<input type='text' value='' name='myName" />
<input type='submit'/>
</form>
or Spring MVC tags only:
<form:form action='/controller.do' method='POST' modelAttribute='fooModelAttribute'>
<form:input path='myName' />
<input type='submit'/>
</form>
You can retrieve the myName value by declaring the following parameter in your controller method:
#RequestParam("myName") String myNameVal
Alternatively, you can declare a class which has a myName field with a getter and setter (say, FooClass). And then you can declare the following parameter in the controller:
FooClass foo
This parameter may have an optional #ModelAttribute("fooModelAttribute") annotation.
See the Spring Reference for more details on #RequestParam, #ModelAttribute, and <form:form>.

MVC - Receive Form with Viewbag Variable from database and passing to the view

i have a html form into my database.
In this form i have a value which contains a Viewbag,
for example value="#Viewbag.MyVariable"
In my view when i try to receive my form all works fine,
but my problem is that the Viewbag value are not converting
to the value which comes from my controller.
any ideia how to resolve this ?
my code:
View:
#Html.Raw(p.Form)
My form in my database looks like:
<form action="/MyController/MyAction" method="post">
<input type="hidden" name="num" value="#ViewBag.num" />
....
....
....
</form>
What i have try to do was with:
Stringbuilder a=new Stringbuilder;
#Html.Raw(a.To.String())
Your code should technically work, if you are using MVC you could try
#MvcHtmlString.Create(p.Form);
I found a solution and now i solved it.
what i have done:
in my form the only part which are different from the others
forms are the dropdownlists.
i only put the part from <select> to </select> in my
database. (only the dynamically part of my form)
in my view i have write my form and only insert the variable
which comes from my controller to insert the data from my
database on the right part in my form.
here is the code:
<form action="/MyController/MyAction" method="post">
<input type="hidden" name="num" value="#ViewBag.num" />
#MvcHtmlString.Create(p.Form);
<button type="submit" name="submit" id="submit">OK</button>
</form>
So i have never more the problem with the variables to be
converted, because the variables are still the same

How to pass values from one Contact Form 7 form to another in Wordpress?

I have a site that has 2 forms - a short form and a long form. If you look at http://dforbesinsuranceagency.com you'll see the short form next to the masthead photo. The long form is at http://dforbesinsuranceagency.com/request-free-insurance-quotes/
When the user hits Submit on the short form, it kicks them over to the long form page, so that part works fine. The part that gives me fits is that I need the values entered into the short form fields First Name, Last Name, Email Address and Telephone passed to their equivalent fields on the long form.
How do I do this?
This is how I am redirecting the short form to the long form (I added it to the Additional Settings section for the short form):
on_sent_ok: "location = 'http://dforbesinsuranceagency.com//request-free-insurance-quotes';"
Any help would be appreciated.
Hack, hack, hackety, hack hack hack... Without suggesting "not using a form-builder" I don't think there is an elegant solution - you can't use the other PHP method suggested without modifying the plugin itself (and that is a can of worms). I will propose a Javascript solution but there are some caveats (below):
jQuery(document).ready(function($){
$('#quick-quote form:first').submit(function(){
var foo = {};
$(this).find('input[type=text], select').each(function(){
foo[$(this).attr('name')] = $(this).val();
});
document.cookie = 'formData='+JSON.stringify(foo);
});
var ff = $('#container form:first');
if(ff.length){
var data = $.parseJSON(
document.cookie.match('(^|;) ?formData=([^;]*)(;|$)')[2]
);
if(data){
for(var name in data){
ff.find('input[name='+name+'], select[name='+name+']').val(data[name]);
}
}
}
});
What this will essentially do is: on submission, store your mini-form options in a cookie. On page load it will then look for a form in the main body of the page and apply any stored cookie data.
Notes
The jQuery selectors are deliberately ambiguous to avoid any future changes in your admin panel/plugin that will likely screw with the form IDs (thus breaking the script).
I'm not faffing about pairing field/option names - for example the select box in your mini-form is named insurance-type however the matching box in the main form is named ins-type - you will have to ensure they are of the same name.
This also applies to select box values - if there is no matching value, it will be ignored (eg. some of your values in the main form have » » characters in front (and so don't match).
try this.
set the action of our first form to a php file named xyz.php
<form method="post" action="xyz.php">
<input type="text" name="name">
<input type="text" name="email_address">
<input type="submit" value="Go To Step 2">
</form>
the file xyz.php will create a new form for you which in this case is your second form (the big one). Set the action of the form as required. the code of your xyz.php will look something like this.
<form method="post" action="form3.php">
<input type="text" name="name" value="<?php echo $_POST['name']; ?>">
<input type="text" name="email_address" value="<?php echo $_POST['email_address']; ?>">
<input type="radio" group="membership_type" value="Free">
<input type="radio" group="membership_type" value="Normal">
<input type="radio" group="membership_type" value="Deluxe">
<input type="checkbox" name="terms_and_conditions">
<input type="submit" value="Go To Step 3">
</form>
where the input fields of the first form will already be filled with the details given by the user in the first form.
You can create the first form by yourself and let the contact form create the second form for you providing the default values using the method above.
Hope this helps!

Resources