-2

Below is the code used for simple html and it works:

<select id="name" class="state">
    <option value="Brad" selected="selected">
        Brad
    </option>

    <option value="Carol">
        Carol
    </option>

    <option value="Derrick">
        Derrick
    </option>
</select> 

Is there another method that i can use so that I can set a default value to my select option tag?

In my jsp file:

</html:select>
<html:option value="0">--Select--</html:option>
                    <html:option value="Brad" selected="selected">Brad</html:option>
                    <html:option value="Carol">Carol</html:option>
                </html:select>

The <option value="Brad" selected="selected"> Brad </option> works in simple html. Brad is shown before clicking the dropdown arrow. My problem is that when I used this in my jsp it says that "The attribute is not recognized <html:option value="Brad" selected="selected">Brad</html:option>"

Hans Kesting
  • 34,565
  • 7
  • 74
  • 97
AndI
  • 3
  • 2
  • 5
  • _Is this correct?_ What exactly are you asking? Does this do what you want? Is there some problem? If so, please update your question to indicate what is wrong. – EJK Nov 23 '17 at 05:52
  • @EJK. The works in simple html. Brad is shown before clicking the dropdown arrow. My problem is that when I used this in my jsp it says that "The attribute is not recognized Brad" – AndI Nov 23 '17 at 06:00

2 Answers2

0

If you are using JSP then there is a tag for html-select-dropdown. Using <form:select> tag you can do something like this -

<form:select path="country">
   <form:option value="NONE" label="--- Select ---"/>
   <form:options items="${yourList}" />
</form:select>

To know more about how to use jsp select tag dig deep into this link.

And for assiging default value take help from this answer.

Update
This is how you can include JSTL in you JSP

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<html>
    <head>
        <title></title>
    </head>
    <body>

    </body>
</html>

Here you see prefix="form", you can name the prefix as you want. You can find more detail here.

Erfan Ahmed
  • 1,404
  • 4
  • 20
  • 30
  • can I do something like this ? – AndI Nov 23 '17 at 08:34
  • nope. You must follow `JSTL` - jsp standard tag library. Find more about [JSTL](https://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm). Here `form` is not a static key word. You can of course change it as you like while including library – Erfan Ahmed Nov 23 '17 at 08:41
  • is there any way that I can use dropdown select option without form tag? – AndI Nov 23 '17 at 08:53
  • Of course. You can use plain `HTML` tag the way you are using now. But using `JSTL` tag will save a lot of works for you. – Erfan Ahmed Nov 23 '17 at 08:56
  • thanks for sharing these information. It helped me a lot! – AndI Nov 24 '17 at 01:48
0

The ="selected" part is not necessary. Just <option selected> will do fine.

<select id="name" class="state">
<option selected value="Brad">
    Brad
</option>

<option value="Carol">
    Carol
</option>

<option value="Derrick">
    Derrick
</option>
</select> 

The value, in this case "Brad", would be passed as a string unless another option is selected.

Brock
  • 95
  • 10