I am working with a struts2 based application
Inside my JSP page for form submission is it mandatory to use s:form (Predefined struts component )
Because when i tried this way it worked (calling the Action class under struts.xml )
<s:form action="HelloWorld" >
<s:submit />
</s:form>
But When I tried to use normal form submission as shown
<form action="HelloWorld">
<input type="Submit"/>
</form>
It isn't working , it gave me 404 error .
please tell me is it mandatory to use and for data submission ??
A struts form action and an HTML tag form action are different. You can use a standard HTML form tag with struts if you create a struts specific URL for example (off the top of my head):
if using in multiple places, generate the url in and call like this -
<s:url id="myActionUrl" action="HelloWorld" />
<form action="<s:property value="%{myActionUrl}" />">
<input type="Submit"/>
</form>
or using in a single instance -
<form action="<s:url id="myActionUrl" action="HelloWorld" />">
<input type="Submit"/>
</form>
You can often look at the page source in your browser to see what Struts generates and recreate it manually like this. You will often end up using additional struts tags such as property to retrieve values from your value stack, but it is useful at times, for instance when generating JavaScript code dynamically.
No, it's not mandatory to use any of the S2 tags, but as Russell says, you need to duplicate the correct action URL.
You also need to be a little careful when mixing-and-matching S2 form tags with non-S2 HTML form tags, because the default S2 theme adds additional HTML markup to the page; they don't just create form tags--the default theme uses table tags to lay out the form.
You can use s:form for form and
<input type="Submit"/>
can be replaced by
<button type="submit"/>
It's not about which to use,it is what you want from it.Struts2 tags provides additional capabilities to the form.
Please go through below two links to get the diffrence
1] http://struts.apache.org/release/2.1.x/docs/form-tags.html
2] http://www.w3schools.com/tags/tag_form.asp
some facilities such as namespace,tooltip,tooltipIconPath and many are provided by struts2 tags.
Related
i am new in MVC and try to learning through googling & online tutorial.
i search Form binding with Model sample in MVC and got many sample code but i am not getting a sample code for a complete form where all kind of html controls are used like
checkbox
radiobutton
listbox control
drodownlist
textarea
hiddenfields
checkboxlist
radiobuttonlist etc
and all controls should be bind through model with server side and client side both kind of validation. form should have two textbox one dropdown, 3 checkboxes and 2 radio button, one listbox control and if possible provide guide line to work with checkboxlist & radiolist.
if anyone knows that kind of url then plzz inform me or if possible please give me a sample code of a complete form where all the above controls will be there with model binding and validation. thanks
in the Views there is a helper called Html you can use it in the view like #Html
there are all things that you wanted, for example
#Html.HiddenFor
#Html.RadioButtonFor
#Html.TextAreaFor
and so on
search for asp.net mvc Html helpers
another smaple
#Html.CheckBox("sameName")
#Html.CheckBox("sameName")
#Html.CheckBox("sameName")
#Html.CheckBox("sameName")
it will produce this html
<input type="checkbox" name="sameName" />
<input type="checkbox" name="sameName" />
<input type="checkbox" name="sameName" />
<input type="checkbox" name="sameName" />
take a look at this website
Have a look here:
It Explains,
Rendering a Form in ASP.NET MVC Using HTML Helpers
http://msdn.microsoft.com/en-us/library/dd410596%28v=vs.100%29.aspx
http://stephenwalther.com/archive/2009/03/03/chapter-6-understanding-html-helpers
Hy
What i want to do is to create a custom renderer for will_paginate which renders first, previous, next and last page and a input field where the user can type in the page number manually. I already have the links for first, last etc. but i stuck at the input field. I could create a form in the view but the input field has to be rendered between the previous and next links.
Does anyone know how to do this?
Thanks for your help
You can do this as a separate form (make sure it is a GET). All you
need is the one input element named page. Something as simple as this
should work (not all browsers may like the #). I dropped it into a
site I'm playing with now and it worked. Put it anywhere on your page.
You might have to make something more complicated if you need to
incorporate search terms.
<form action="#" method="get">
Go to page: <input type="text" name="page" value="" size="2"
maxlength="4" />
<input type="submit" name="btnSubmit" />
</form>
Can i use html form tags in struts 2 form?
Like
<input type='text' value='' />
<input type='submit' />
Will the values be posted through struts2?
It's not at all mandatory to use struts2 tags. You could go with regular HTML.
Of course.
This is one of those questions you can just try.
All the S2 form tags do is emit HTML, filling in various attributes as required. (It's slightly more complicated than that, but ultimately, they spit out an HTML form field.)
Flip your question on its head: why wouldn't a hand-crafted input tag be sent via the normal browser HTTP submission process? What mechanism could prevent it from working? How is the request body of from such a form submission different from one where the input tags are S2 custom tags?
These questions are all trivial to explore.
Yes.
You must give them a name; the name will be used to set properties (with correct type conversion) in the struts action.
If you call an input somename the setSomename() will be called on post.
If simple HTML used you wont be able to call struts tags inside it eg:
<s:submit cssStyle="submit_button" id='newrc%{#stat.index}.%{#questionIndex.index}' name="newrc%{#stat.index}.%{#questionIndex.index}" onclick="return newrcClick(this)" value="+" />
This works but below code does not provide values for id and name from values stack thus :name="newrc%{#stat.index}.%{#questionIndex.index}"
<input type="button" cssStyle="submit_button" id='newrc%{#stat.index}.%{#questionIndex.index}' name="newrc%{#stat.index}.%{#questionIndex.index}" onclick="return newrcClick(this)" value="+" />
I have the following mark up in an ASP.NET MVC view (this is a Twitter Bootstrap search box):
<form action="#Url.Action("Results", "Search")" method="post">
<input type="text" class="search-query" id="SearchTerm" name="SearchTerm" />
</form>
This code works as expected, but using a post here is causing problems.
How can I change this markup to pass the search query as a URL argument instead? I'm not really sure how to even approach this short of keeping the existing markup and then redirecting from the controller. I'm thinking there must be a more efficient way than that.
You should be able to change method="post" to method="get" and get the desired result. The form, with a get method setting, pushes the fields in the form to the querystring by its default behavior.
As a workaround, if the default behavior doesn't suit you, you could catch the submit event of the form, and do:
window.location = form.action + "?SearchTerm=" + document.getElementById("SearchTerm").value
Something like that, where form is a reference to the form element. You can build the link and redirect using javascript, which is a get request.
My project changed version of struts from struts-2.1.8.1 to struts-2.2.1.
We don't use suffix ".action" for naming, after migration it is appeared. For older version html code looks like:
<form id="Login" name="Login" action="/fm2/Login" method="post">
But new struts renders the same form:
<form id="Login" name="Login" action="/fm2/Login.action" method="post"
So difference that .action has been added. What's wrong with new release?
This is the default extension (and should be in 2.1.8.1 too).
You can change it in your struts.xml:
<constant name="struts.action.extension" value="whatever" />
I have a similiar problem when change to struts-2.2.1 from struts-2.1.8.1.
Struts-2.2.1 will add the ".action" extension automatically for redirectAction result.
This is very annoying.
This has not changed, AFAIK. Be sure to understand the difference between the "struts action" and the "action attribute of a HTML FORM" element
Typically, to render a FORM tag in Struts2 you'd use a (Struts2) form tag - its action attribute corresponds to the name of a Struts2 action, which corresponds to a url without the suffix (by default '.action', but you can change it)
So, the Struts2 tag
<s:form action="/fm2/Login">
would typically produce the HTML output
<form action="/fm2/Login.action">
I removed the struts2-convention-plugin-2.1.8.1 jar from my web-inf/lib and it started to work fine.
Hope this helps...
cheers...