how to use drop down box in spring - spring-mvc

I have recently started working on spring MVC. In the JSPx web page I need to take the country-code from user and save it into the database. For this purpose, initially, I was using textfield for user input. The code is as below-
<fieldset class="fieldset" >
<div class="fields">
<field:input field="country" id="label_organisation_view_country" disabled="${!isAddOrganizationView}"/>
</div>
</fieldset>
I have made an enum (public enum Country) containing the list of country-codes. Now, I want to use drop-down for getting the country-code instead of textbox, using the created enum.
Can anyone please let me know how to get it or redirect me to the same page?

using the spring form tag, that requires this :
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
you can do something like:
<form:form name="myFRM" id="myFRM" action="${myURL}" method="POST" modelAttribute="myBackingObject">
<form:select path="myCountry" id="MyCountry" name='MyCountry'>
<form:options items="${countries}" itemLabel="yourMethodForDisplayNameInEnum"/>
</form:select>
and in controller :
model.addAttribute("countries", CountryEnum.values());
model.addAttribute("myBackingObject", yourPojoContaingFieldMyCountry);
Check out the docs :
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/
and specifically mvc
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html

Related

How do I receive the input to my form in my asp.net action?

this seems super simple, yet I'm having trouble figuring it out.
I have a asp.net core mvc application, and I need to use a form to send some input to a specific action.
I have this form:
<div class="panel-body">
<form action="#Url.Action("updateStatus", "Home")">
<textarea class="greyback" id="w3review" name="w3review" rows="4" cols="50"> Update your status...
</textarea>
<br><br>
<input class="input" type="submit" value="Submit">
</form>
</div>
The form invokes the action that I want it to, so far so good, but I of course also need the text that is put into the textfield to be sent along, and that I'm having a bit of trouble with.
I have tried looking at the special razor page stuff for making forms that look like so:
#using (Html.BeginForm(“action”, “controller”))
But I don't quite understand how that works, how I can hold onto my css tags, or how I could actually create a textarea tag with this syntax.
What would be the best way to send along this data to the action that I want?
how I could actually create a textarea tag with this syntax.
You can use the following code:
#Html.TextArea("w3review", "Update your status...", new { #class = "greyback" })
result:
What would be the best way to send along this data to the action that I want?
There is no difference to use html tags or htmlhelper.Htmlhelper will be rendered to html code which is the same with using html tags directly.

Asp.net Where i use Html.BeginForm()

I use Html.BeginForm() in some Asp.net M but i did not understand where its use becomes compulsory .Why i really needed to use it in my Views..? It may be a silly question but i am perplexed and confused to use this Html-helper in mvc...??
Here below i have code for search option but i did not know what is meaning of FormMethod.Get so please explain it briefly ?
using(Html.BeginForm("Index","Home",FormMethod.Get))
{
<b>Search Option</b>
#Html.TextBox("Search") <input type="submit" value="Search"/>
}
Html.BeginForm is a helper method which generates the HTML markup for a form tag to the page/view. When you use it will using , it properly creates a end form tag (<\form>) as well. So basically when razor executes the code in your question, it will generate markup like this
<form action="/Home/Index" method="get">
<!-- markup generated by other elements inside the form will goes here -->
</form>
One benefit of using the helper method is , it properly generates the correct markup that you /i write the markup of form tag and the attributes needed which is prone to human errors (typo). Also the helper method always generate the correct relative path to the action method for the value of the action attribute.
It is not necessary to use this method for your view. If you prefer to write clean-readable UI code, you would do that by replacing your helper method calls with something like
<form action="/Home/Index" method="get">
<b>Search Option</b>
<input id="Search" name="Search" type="text" value="">
<input type="submit" value="Search">
</form>
In ASP.NET Core, we have tag helpers which allows us to write code to generate form(or other form elements) as close to the above hand written html markup. So the code will basically look more like pure HTML than C# or VB.NET

How to prevent escape of html tags in Spring MVC?

I have some data inside my database with html tags like
<b>, <br>
But, when I try to store it into a model object and render it on a JSP, it is rendered with the tags. The tags are not evaluated.
Any idea on how to get my page to process those tags? Below is the code I use in my JSP.
<div class="col-xs-6 form-group">
<label>Comments</label>
${requestObject.comments}
</div>
Edit:
I tried the below code also, with no luck.
<c:out escapeXml="false" value="${requestObject.comments}" />
Database Content
<b>Oh Snap</b>
HTML Source
<b>Oh Snap</b>
I am expecting my text to be rendered bold
Oh Snap
Not sure whether this is what you are seeking, but this should allow HTML to be evaluted:
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:htmlEscape defaultHtmlEscape="false" />
If you have Spring form or message tags, you can do: htmlEscape="false".
UPDATE:
Sadly, the c:out syntax will take precedence over the Spring tag on this but you can also try:
<spring:escapeBody htmlEscape="false">
<%=requestObject.comments%>
</spring:escapeBody>

How can I create Simple aspx webpage which will send me parameters in the link

I am trying to do something from scratch in asp
I would like to get something like below. From basic hard coded values.
http://localhost:2478/Default.aspx?phone=905123456&order=123456
After that I want to use this parameter in my variables for SQLquery
I am new to asp.net and vb , I'm learning. Can you please explain me in details? Any helps will be appreciate.
Can you please provide any basic code from where I can start
Based on your latest comments:
<form method="get" action="default.aspx">
<input type="text" name="phone" />
<input type="text" name="order" />
<input type="submit" value="submit" />
</form>
Key points:
method=get (fields are in url instead of body)
note that the form above doesn't have runat=server (more below) - it's a plain HTML Form on plain HTML page
in the context of ASP.Net Web forms, you may run into issues particularly if you are using Master Pages (you can't nest forms, and a Master page is a container for your entire page).
ASP.Net forms do POSTbacks - re: method=post
You can use/add plain forms in an ASP.Net page as long as you don't nest them (outside of the server side ASP.Net form).
If you have no choice (e.g. Master Page), you'll have to construct the Querystring manually after a POSTback, then Response.Redirect to some other target (adding the querystring manually).
there are other non-ASP.Net-y ways of doing it - e.g. javascript, but that a different story

ASP.NET MVC: Working with ASPX view engine w/o using helpers?

I hate those razor helpers. (LabelFor, TextboxFor...) they try to help me but they teach me nothing.
I want to try with ASPX engine. when I open it there is even toolbox on the left with all old good html commands. why can't i use it ?
how come that when i try to buil app intellisense say runat="server" is required when I know MVC doesn't need that ?
In brief how do I write HTML w/o using helpers ?
Any constructive advice would be appreciated.
Sample:
<%# Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">Home Page</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:ListBox runat="server">
<asp:ListItem Text="text1" />
<asp:ListItem Text="text2" />
</asp:ListBox>
</asp:Content>
Error:
`Server Error in '/' Application.
Control 'MainContent_ctl00' of type 'ListBox' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Control 'MainContent_ctl00' of type 'ListBox' must be placed inside a form tag with runat=server.
Source Error:
Line 22: <ul id="menu">
Line 23: <li><%: Html.ActionLink("Home", "Index", "Home")%></li>
Line 24: <li><%: Html.ActionLink("About", "About", "Home")%></li>
Line 25: </ul>
Line 26: </div>`
You hate razor helpers, but you like to use predefined elements from the toolbox? Items in the toolbox are meant for webforms, but can be used for MVC as well. This is because MVC and webforms are still members of the asp.net family. However, I wouldn't really recommend to do that...
In brief how do I write HTML w/o using helpers ?
How do you write a simple text? Simply type the html tags as needed. You can use razor/aspx (doesn't really matter that much which one you choose) syntax to loop over your collections and construct the html based on your data. Nothing is forcing you to use razor helpers or webforms controls...
I think it may be the right time to learn a bit more about the technology you're using, because you seem to be confused about the really basic stuff here...
I hate those razor helpers. (LabelFor, TextboxFor...)
ASP.NET MVC is a very different pattern and concept than classic WebForms. If you don't like those new concepts this probably means that ASP.NET MVC is not for you. You could always go back to the classic WebForms development.
In brief how do I write HTML w/o using helpers ?
There's nothing in ASP.NET MVC that forces you to use helpers. You could perfectly fine write pure static HTML in your views:
<form action="/home/save" action="post">
<label for="first_name">First name</label>
<input type="text" name="first_name" id="first_name" />
<label for="item">Select an item</label>
<select id="item" name="item">
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
</select>
<button type="sybmit">OK</button>
</form>
Obviously now you can forget about things like automatic data binding from the model, validation, routing, ...
As far as the problem you have with the ListBox inside a form with runat="server" is concerned, it seems that you have used some classic WebForms server side control in an ASP.NET MVC application which is not supported. Server side controls don't work in MVC because they depend on things like ViewState and require you to place them inside forms with runat="server", ... things that no longer exist in MVC.
So I would recommend you to go ahead and read some getting started tutorials about MVC here: http://asp.net/mvc
A ListBox is a .Net-ified SELECT element. In a razor template you can probably do something like:
<select id="list1">
#for // some loop condition
{
<option value="#Data.someValue">Text1</option>
}
</select>
... where #Data is bound to a model, and Text1 could also be similarly declared.

Resources