如何在select标签中显示多个值?

时间:2014-03-25 13:32:09

标签: java spring jsp

我想在select标签中显示一些名称,但我无法做到这一点。我有类型字符串的setter和getter:

public void setFinalApprover(String finalApprover) {
        this.finalApprover = finalApprover;
    }

我在DTOService Class中的方法中从数据库中获取数据,如:

public List addApprover(EmpRegistrationForm leaveApplyForm){

            List list=new ArrayList();
            try {
                con=DBConnection.getConnection();
                String Approver="select emp_name from newemp_register where userType='employee'";
                PreparedStatement pstmt=con.prepareStatement(Approver);
                ResultSet rs=pstmt.executeQuery();

                if(rs.next())
                {

                    leaveApplyForm.setFirstApprover(rs.getString(1));
                    list.add(leaveApplyForm);

                }               

            } catch (SQLException e) {

                e.printStackTrace();
            }   

            return list;

        }

我的控制器类就像:

@RequestMapping(value = "/registerForm", method = RequestMethod.GET)
       public ModelAndView showForm(@ModelAttribute("registerForm")EmpRegistrationForm EmpRegistrationForm, BindingResult result , ModelMap model,EmpRegistrationForm empRegistrationForm , HttpServletRequest request) 
       {
         log.info("Inside Controller returning to loginform page....");
         CommonDTOBean dtoBean=new CommonDTOBean();

        EmpRegistrationForm registerForm =  new EmpRegistrationForm();
        model.put("registerForm", registerForm);    

        model.remove(registerForm);
        EmpRegisterWorker worker=new EmpRegisterWorker();
        List status=worker.addApprover(EmpRegistrationForm);

        if(status!=null){
            model.put("status", status);

        }
}

我在jsp页面上的列表显示值,如:

<tr><td><spring:message code="label.firstApprover"> </spring:message></td>
    <td><form:select path="firstApprover" name="firstApprover">
<form:option  value="">${status.firstApprover}</form:option>

现在问题在于,它只显示一个值,但是结果集正在重复多个值。请帮我解决这个问题。

有没有办法将多个值从DTOSerive类传递到控制器,从控制器传递回jsp页面?并且

String Approver="select emp_name from newemp_register where userType='employee'";if(rs.next())
                {

                    leaveApplyForm.setFirstApprover(rs.getString(1));
                    list.add(leaveApplyForm);

                }      getting error like wrriting aborted to setFirstApprover.

2 个答案:

答案 0 :(得分:0)

您的Select标签应如下所示。这应该有用。

<forms:select path="firstApprover" items="${status}" />

您希望select文件填充的内容是使用status中设置的ModelMap属性引用的。

答案 1 :(得分:0)

<select name="firstApprover" >
<c:forEach items="${status}" var="stat">
     <option value="${stat.your value}"> 
             ${country.your value}
     </option> 
</c:forEach>
</select>

选择选项标记值中,您可以使用 stat 变量设置实际预期数据。

即。使用stat附加您的密钥,例如我的模型密钥是名称,所以我只需将其添加为 stat.name