2014-04-10 40 views
1

我想在页面加载的下拉菜单中显示选定的值。 我的编辑页面显示值,国家。现在用户属于美国,因此在页面加载时应默认选择美国。我可以使用JavaScript或jQuery。显示在页面加载编辑jsp页面上的下拉列表中选定的值(Spring MVC)

我在互联网上找到了一些帮助,但他们与scriplets(<%%>),我不想使用它们。

我传递一个具有Id和Value的List。另外我还有另一个对象。 我可以通过在jsp中引入for循环并显示值来编写代码,但可能是我在那里犯了一些错误。也想知道是否有更好的方法来做到这一点。

我正在使用Spring MVC。

谢谢。

回答

5

“现在用户属于美国,因此在页面加载时应默认选择美国。”你知道手边的用户属于某个国家。

假设你有一个POJO国家为:

public class Country{ 
     String countryName; 
     String countryId; 
     //setters and getters 
    } 

    public class YourForm{ 
     List<Country> countryList; 
     String selectedCountryId; 
     ... 
     //setters and getters 
    } 

在你的控制器的方法委托给JSP:

... 
    YourForm form = new YourForm(); 
    //set your countrylist to form 
    //set country user belongs to - selectedCountryId - since you know before hand user belongs to some country. 
    model.addAttribute("yourForm", form): 
    ... 

现在访问它在JSP为:

<%@ 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> 

由于selectedCountryId已经在控制器中设置,您将在jsp中看到该国家为自动选择的国家。

+0

谢谢。工作得很好。我正在使用<选择标记.....用 romhail

相关问题