【问题标题】:Spring MVC JSP Form using multiple objects needs drop-down listSpring MVC JSP Form 使用多个对象需要下拉列表
【发布时间】:2015-10-28 19:41:52
【问题描述】:

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

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>

【问题讨论】:

  • 当然他们是null,他们永远是null。您的 getter 始终创建员工和客户的新实例。将实例存储在包装类内的变量中并重用它。
  • 谢谢@M。 Deinum,我将包装类更改为此并修复了该部分。 '公共类 EmployeeFormWrapper { 私有 NewEmployeeVM 员工 = 新 NewEmployeeVM();私有 ClientVM 客户端 = new ClientVM(); public NewEmployeeVM getEmployee(){ return this.employee; } public ClientVM getClient(){ return this.client; }}'

标签: java spring jsp spring-mvc


【解决方案1】:

我明白了,我将我的 JSP 更改为如下所示,它给了我一个不错的下拉列表:

<body class="NewEmpBody">

    <p>New Employee Proposed To Project Before Entered Into OpenAir</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>Client :</td>
                <td><form:select path="client.text">
                      <form:option value="NONE" label="--- Select ---" />
                      <form:options items="${resultBoard.getFilteredResultsClients()}" />
                       </form:select>
                </td>
            </tr>
            <tr>
                <td colspan="3"><input type="submit" value="Submit" /></td>
            </tr>

        </table>

    </form:form>

</body>

【讨论】:

    猜你喜欢
    • 2011-02-02
    • 1970-01-01
    • 2018-02-07
    • 2018-02-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多