使用多个对象的Spring MVC JSP Form需要下拉列表

时间:2015-10-28 19:41:53

标签: java spring jsp spring-mvc

My Spring JSP Form使用了2个类,我将它放在一个包装类中。员工姓名部分有效。但现在我需要创建一个可供选择的客户端下拉列表。我更新别人的代码,我相信" resultBoard.getFilteredResultsClients()"将返回客户列表。 Spring JSP Form的任何提示或示例代码,使用下拉列表从列表中进行选择?

包含员工和客户端类的Java包装器类:

public class EmployeeFormWrapper {

    private NewEmployeeVM employee = new NewEmployeeVM();
    private ClientVM client = new ClientVM();

    public NewEmployeeVM getEmployee(){
        return this.employee;
    }

    public ClientVM getClient(){
        return this.client;
    }
}

我的控制器类:

@Controller
@SessionAttributes({"resultBoard", "filterBoard"})
public class NewEmployeeController {
    @Autowired
    NewEmpDAO newEmpDAO;
    @RequestMapping(value = "NewEmpInput", method = RequestMethod.GET)
    public String promptEmployee(Model model)
    {
        model.addAttribute("myForm", new EmployeeFormWrapper());
        return "NewEmpInput";
    }

    @RequestMapping(value = "NewEmpInput", method = RequestMethod.POST)
    public String newEmployee(@ModelAttribute("myForm") EmployeeFormWrapper myForm)
    {

        NewEmployeeVM employee = myForm.getEmployee();
        ClientVM client = myForm.getClient();
        System.out.println("Employee = " + employee.getFirstName() + " " + employee.getLastName());
        System.out.println("Client = " + client.getText());

        try{newEmpDAO.writeNewEmployee(employee);}
        catch (Exception e){
            e.printStackTrace();
        }

        return "ConfirmNewEmp";
    }
}

我的JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<%@ page import="com.model.FilterBoard"%>
<!DOCTYPE html>
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> -->
<html>
<head>
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link href="assets/css/bootstrap.css" rel="stylesheet">
<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
<link rel="stylesheet"
    href="<c:url value='/resources/css/mainStyle.css'/>">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<style type="text/css">
     body {
       color:#000000;
       // background-color:#FFFFFF;
    background-color:tan;
       background-image:url('Background Image');
       background-repeat:no-repeat;
     }

     a  { color:#0000FF; }
     a:visited { color:#800080; }
     a:hover { color:#008000; }
     a:active { color:#FF0000; }
 </style>

<title>New Employee Proposed To Project</title>
</head>
<body class="NewEmpBody">

    <p>New Employee</p>

    <form:form commandName="myForm" modelAttribute="myForm">
        <table>
            <tr>
                <td>First Name:</td>
                <td><form:input path="employee.firstName" /></td>
                <td>Middle Initial:</td>
                <td><form:input path="employee.middleInitial" /></td>
            </tr>

            <tr>
                <td>Last Name:</td>
                <td><form:input path="employee.lastName" /></td>
            </tr>
            <tr>
                <td>Clients:</td>
                <td>
                    <ul>
                        <c:forEach items="${resultBoard.getFilteredResultsClients()}"
                            var="filteredResultsClient">
                            <li><form:button name="toggleClient" class="btn btn-link"
                                    value='${filteredResultsClient.toJSON()}'>
                                    ${filteredResultsClient.getText()}
                                </form:button></li>
                        </c:forEach>
                    </ul>
                </td>
            </tr>

            <tr>
                <td colspan="3"><input type="submit" value="Submit" /></td>
            </tr>

        </table>

    </form:form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

我明白了,我把我的JSP改成了这样,它给了我一个很好的下拉列表:

value