2014-07-09 32 views
0

我有一個視圖模型,其中一個字段必須映射到下拉列表。我設法填充下拉列表,但我不知道如何指定選定的值。如何使用jsp標籤進行選擇?

edituser.jsp

<%@ include file="/WEB-INF/template/taglibs.jsp"%> 
<div class="container"> 
    <%@ include file="menu.jsp"%> 

    <form:form commandName="editForm" method="post" class="form-horizontal form-width"> 

     <fieldset> 
      <legend>Edit user</legend> 
      <div class="form-group"> 
       <input class="hidden" type="text" value="${id}" hidden="true" name="id"/> 
       <label for="firstName" class="col-lg-2 control-label">First 
        name</label> 
       <div class="col-lg-10"> 
        <input type="text" class="form-control" id="firstName" 
         placeholder="First name" name="firstName" value="${fname}"> 
       </div> 
      </div> 
      <div class="form-group"> 
       <label for="lastName" class="col-lg-2 control-label">Last 
        name</label> 
       <div class="col-lg-10"> 
        <input type="text" class="form-control" id="lastName" 
         placeholder="Last name" name="lastName" value="${lname}"> 
       </div> 
      </div> 

      <div class="form-group"> 
       <label for="select" class="col-lg-2 control-label">Role</label> 
       <div class="col-lg-10"> 
        <select name="role" class="form-control"> 
         <option value="NONE">Select role</option> 
         <c:forEach items="${roles}" var="role"> 
          <option value="${role}">${role}</option> 
         </c:forEach> 
        </select> 
       </div> 
      </div> 

      <div class="form-group"> 
       <label for="username" class="col-lg-2 control-label">Username</label> 
       <div class="col-lg-10"> 
        <input type="text" class="form-control" id="username" 
         placeholder="Username" name="username" value="${username}"> 
       </div> 
      </div>   

      <div class="form-group"> 
       <label for="inputPassword" class="col-lg-2 control-label">Password</label> 
       <div class="col-lg-10"> 
        <input type="password" class="form-control" id="inputPassword" 
         placeholder="Password" name="password" value="${guid}"> 
       </div> 
      </div> 

      <div class="form-group"> 
       <label for="inputPassword" class="col-lg-2 control-label">Confirm password</label> 
       <div class="col-lg-10"> 
        <input type="password" class="form-control" id="inputPassword" 
         placeholder="Password" name="confirmedpassword" value="${guid}"> 
       </div> 
      </div>  

      <div class="form-group"> 
       <div class="col-lg-10 col-lg-offset-2"> 
        <a class="btn btn-warning" href="http://localhost:8080/Catering/index/users">Cancel</a> 
        <button type="submit" class="btn btn-warning">Submit</button> 
       </div> 
      </div> 
     </fieldset> 
    </form:form> 
</div> 

UsersController

@Controller 
public class UsersController { 
    @RequestMapping(value = "/editUser", method = RequestMethod.GET) 
    public String getEditUserPage(@RequestParam int id, Model model, Authentication authentication) { 



     logger.debug("Received request to show edit user page(GET)"); 

     model.addAttribute("username", "You are logged in as " + authentication.getPrincipal()); 

     UserModel user = UserDataAccess.getUserById(id); 

     model.addAttribute("id", user.getId()); 
     model.addAttribute("fname", user.getFirstName()); 
     model.addAttribute("lname", user.getLastName()); 
     model.addAttribute("role", user.getRole()); 
     model.addAttribute("username", user.getUsername()); 
     model.addAttribute("guid", guid); 
     model.addAttribute("editForm", new UserViewModel()); 

     return "edituser"; 
    } 
} 

UserViewModel

public class UserViewModel { 
    private int id; 
    private String firstName; 
    private String lastName; 
    private String role; 
    private String username; 
    private String password; 
    private String confirmedpassword; 
    //getters and setters 
} 

回答

0

你可以這樣做(未測試)

<select name="role" class="form-control"> 
    <option value="NONE">Select role</option> 
    <c:forEach items="${roles}" var="curRole"> 
     <c:choose> 
      <c:when test="${curRole eq role}"> 
       <option selected="selected">${role}</option> 
      </c:when> 
      <c:otherwise> 
       <option>${role}</option> 
      </c:otherwise> 
     </c:choose> 
    </c:forEach> 
</select> 

您還必須在控制器中添加一個集合,表示所有角色的列表。我無法從您的代碼中看到它。

我建議你看一看spring-form.tld設施,你的具體情況:選擇標籤:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/spring-form.tld.html#spring-form.tld.select

在這裏你可以找到如何使用spring:form標籤一個簡單的例子:http://www.mkyong.com/spring-mvc/spring-mvc-dropdown-box-example/

相關問題