javascript - show the selected value in dropdown on edit jsp page on page load (Spring MVC) -
i want show selected value in dropdown on page load. edit page shows value, country. user belongs usa, usa should selected default on page load. can use javascript or jquery.
i found few on internet scriplets (<% %>) , don't want use them.
i passing list has id , value. have id in object. can write code introducing loop in jsp , showing value, may doing mistake there. wanted know if there better way this.
i using spring mvc.
thanks.
"now user belongs usa, usa should selected default on page load.". know before hand user belongs country.
assuming have pojo country as:
public class country{ string countryname; string countryid; //setters , getters } public class yourform{ list<country> countrylist; string selectedcountryid; ... //setters , getters }
in controller method delegates jsp:
... yourform form = new yourform(); //set countrylist form //set country user belongs - selectedcountryid - since know before hand user belongs country. model.addattribute("yourform", form): ...
now access in jsp as:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1252"> </head> <body> <form:form id="yourform" modelattribute="yourform" method="post"> <tr> <td width="50%" class="label"> <form:select id="countryid" path="selectedcountryid" title='select country'> <option value="">please select</option> <form:options items="${countrylist}" itemvalue="countryid" itemlabel="countryname"/> </form:select> </td> </tr> </body>
since selectedcountryid set in controller see country auto selected in jsp.
Comments
Post a Comment