`
IRH
  • 浏览: 1283 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Sping MVC中一对多的视图处理

阅读更多
@Entity
@Table(name = "employer")
public class Employer

    private Integer id;

    private String firstname;
    private String lastname;
    private String company;

    private List<Employee> employees; // one-to-many

    /* getters & setters */

}

@Entity
@Table(name = "employee")
public class Employee {

    private Integer id;

    @Transient // means "not a DB field"
    private Integer remove; // boolean flag

    private String firstname;
    private String lastname;

    private Employer employer; // many-to-one

    /* getters & setters */

}


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"
%><%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"
%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
%><!DOCTYPE HTML>
<html>
<head>

    <title>Edit</title>
    <style type="text/css">.hidden {display: none;}</style>
    <script type="text/javascript">
    $(function() {

        // Start indexing at the size of the current list
        var index = ${fn:length(employer.employees)};

        // Add a new Employee
        $("#add").off("click").on("click", function() {
            $(this).before(function() {
                var html = '<div id="employees' + index + '.wrapper" class="hidden">';                   
                html += '<input type="text" id="employees' + index + '.firstname" name="employees[' + index + '].firstname" />';
                html += '<input type="text" id="employees' + index + '.lastname" name="employees[' + index + '].lastname" />';
                html += '<input type="hidden" id="employees' + index + '.remove" name="employees[' + index + '].remove" value="0" />';
                html += '<a href="#" class="employees.remove" data-index="' + index + '">remove</a>';                   
                html += "</div>";
                return html;
            });
            $("#employees" + index + "\\.wrapper").show();
            index++;
            return false;
        });

        // Remove an Employee
        $("a.employees\\.remove").off("click").on("click", function() {
            var index2remove = $(this).data("index");
            $("#employees" + index2remove + "\\.wrapper").hide();
            $("#employees" + index2remove + "\\.remove").val("1");
            return false;
        });

    });
    </script>

</head>
<body>

    <c:choose>
        <c:when test="${type eq 'create'}"><c:set var="actionUrl" value="employer/create" /></c:when>
        <c:otherwise><c:set var="actionUrl" value="employer/update/${employer.id}" /></c:otherwise>
    </c:choose>

    <form:form action="${actionUrl}" modelAttribute="employer" method="POST" name="employer">
        <form:hidden path="id" />
        <table>
            <tr>
                <td><form:label path="firstname">Firstname</form:label></td>
                <td><form:input path="firstname" /><form:errors path="firstname" /></td>
            </tr>
            <tr>
                <td><form:label path="lastname">Lastname</form:label></td>
                <td><form:input path="lastname" /><form:errors path="lastname" /></td>
            </tr>
            <tr>
                <td><form:label path="company">company</form:label></td>
                <td><form:input path="company" /><form:errors path="company" /></td>
            </tr>
            <tr>
                <td>Employees</td>
                <td>
                    <c:forEach items="${employer.employees}" varStatus="loop">
                        <!-- Add a wrapping div -->
                        <c:choose>
                            <c:when test="${employer.employees[loop.index].remove eq 1}">
                                <div id="employees${loop.index}.wrapper" class="hidden">
                            </c:when>
                            <c:otherwise>
                                <div id="employees${loop.index}.wrapper">
                            </c:otherwise>
                        </c:choose>
                            <!-- Generate the fields -->
                            <form:input path="employees[${loop.index}].firstname" />
                            <form:input path="employees[${loop.index}].lastname" />
                            <!-- Add the remove flag -->
                            <c:choose>
                                <c:when test="${employees[loop.index].remove eq 1}"><c:set var="hiddenValue" value="1" /></c:when>
                                <c:otherwise><c:set var="hiddenValue" value="0" /></c:otherwise>
                            </c:choose>
                            <form:hidden path="employees[${loop.index}].remove" value="${hiddenValue}" />
                            <!-- Add a link to remove the Employee -->
                            <a href="#" class="employees.remove" data-index="${loop.index}">remove</a>
                        </div>
                    </c:forEach>
                    <button id="add" type="button">add</button>
                </td>
            </tr>
        </table>
        <button type="submit">OK</button>
    </form:form>

</body>
</html>

@Controller
@RequestMapping("employer")
public class EmployerController {

    // Manage dynamically added or removed employees
    private List<Employee> manageEmployees(Employer employer) {
        // Store the employees which shouldn't be persisted
        List<Employee> employees2remove = new ArrayList<Employee>();
        if (employer.getEmployees() != null) {
            for (Iterator<Employee> i = employer.getEmployees().iterator(); i.hasNext();) {
                Employee employee = i.next();
                // If the remove flag is true, remove the employee from the list
                if (employee.getRemove() == 1) {
                    employees2remove.add(employee);
                    i.remove();
                // Otherwise, perform the links
                } else {
                    employee.setEmployer(employer);
                }
            }
        }
        return employees2remove;
    }

    // -- Creating a new employer ----------

    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create(@ModelAttribute Employer employer, Model model) {
        // Should init the AutoPopulatingList
        return create(employer, model, true);
    }

    private String create(Employer employer, Model model, boolean init) {
        if (init) {
            // Init the AutoPopulatingList
            employer.setEmployees(new AutoPopulatingList<Employee>(Employee.class));
        }
        model.addAttribute("type", "create");
        return "employer/edit";
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(@Valid @ModelAttribute Employer employer, BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            // Should not re-init the AutoPopulatingList
            return create(employer, model, false);
        }
        // Call the private method
        manageEmployees(employer);
        // Persist the employer
        employerService.save(employer);
        return "redirect:employer/show/" + employer.getId();
    }

    // -- Updating an existing employer ----------

    @RequestMapping(value = "update/{pk}", method = RequestMethod.GET)
    public String update(@PathVariable Integer pk, @ModelAttribute Employer employer, Model model) {
        // Add your own getEmployerById(pk)
        model.addAttribute("type", "update");
        return "employer/edit";
    }

    @RequestMapping(value = "update/{pk}", method = RequestMethod.POST)
    public String update(@PathVariable Integer pk, @Valid @ModelAttribute Employer employer, BindingResult bindingResult, Model model) {
        // Add your own getEmployerById(pk)
        if (bindingResult.hasErrors()) {
            return update(pk, employer, model);
        }
        List<Employee> employees2remove = manageEmployees(employer);
        // First, save the employer
        employerService.update(employer);
        // Then, delete the previously linked employees which should be now removed
        for (Employee employee : employees2remove) {
            if (employee.getId() != null) {
                employeeService.delete(employee);
            }
        }
        return "redirect:employer/show/" + employer.getId();
    }

    // -- Show an existing employer ----------

    @RequestMapping(value = "show/{pk}", method = RequestMethod.GET)
    public String show(@PathVariable Integer pk, @ModelAttribute Employer employer) {
        // Add your own getEmployerById(pk)
        return "employer/show";
    }

}

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"
%><%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"
%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
%><!DOCTYPE HTML>
<html>
<head>

    <title>Edit</title>
    <style type="text/css">.hidden {display: none;}</style>
    <script type="text/javascript">
    $(function() {

        // Start indexing at the size of the current list
        var index = ${fn:length(employer.employees)};

        // Add a new Employee
        $("#add").off("click").on("click", function() {
            $(this).before(function() {
                var html = '<div id="employees' + index + '.wrapper" class="hidden">';                   
                html += '<input type="text" id="employees' + index + '.firstname" name="employees[' + index + '].firstname" />';
                html += '<input type="text" id="employees' + index + '.lastname" name="employees[' + index + '].lastname" />';
                html += '<input type="hidden" id="employees' + index + '.remove" name="employees[' + index + '].remove" value="0" />';
                html += '<a href="#" class="employees.remove" data-index="' + index + '">remove</a>';                   
                html += "</div>";
                return html;
            });
            $("#employees" + index + "\\.wrapper").show();
            index++;
            return false;
        });

        // Remove an Employee
        $("a.employees\\.remove").off("click").on("click", function() {
            var index2remove = $(this).data("index");
            $("#employees" + index2remove + "\\.wrapper").hide();
            $("#employees" + index2remove + "\\.remove").val("1");
            return false;
        });

    });
    </script>

</head>
<body>

    <c:choose>
        <c:when test="${type eq 'create'}"><c:set var="actionUrl" value="employer/create" /></c:when>
        <c:otherwise><c:set var="actionUrl" value="employer/update/${employer.id}" /></c:otherwise>
    </c:choose>

    <form:form action="${actionUrl}" modelAttribute="employer" method="POST" name="employer">
        <form:hidden path="id" />
        <table>
            <tr>
                <td><form:label path="firstname">Firstname</form:label></td>
                <td><form:input path="firstname" /><form:errors path="firstname" /></td>
            </tr>
            <tr>
                <td><form:label path="lastname">Lastname</form:label></td>
                <td><form:input path="lastname" /><form:errors path="lastname" /></td>
            </tr>
            <tr>
                <td><form:label path="company">company</form:label></td>
                <td><form:input path="company" /><form:errors path="company" /></td>
            </tr>
            <tr>
                <td>Employees</td>
                <td>
                    <c:forEach items="${employer.employees}" varStatus="loop">
                        <!-- Add a wrapping div -->
                        <c:choose>
                            <c:when test="${employer.employees[loop.index].remove eq 1}">
                                <div id="employees${loop.index}.wrapper" class="hidden">
                            </c:when>
                            <c:otherwise>
                                <div id="employees${loop.index}.wrapper">
                            </c:otherwise>
                        </c:choose>
                            <!-- Generate the fields -->
                            <form:input path="employees[${loop.index}].firstname" />
                            <form:input path="employees[${loop.index}].lastname" />
                            <!-- Add the remove flag -->
                            <c:choose>
                                <c:when test="${employees[loop.index].remove eq 1}"><c:set var="hiddenValue" value="1" /></c:when>
                                <c:otherwise><c:set var="hiddenValue" value="0" /></c:otherwise>
                            </c:choose>
                            <form:hidden path="employees[${loop.index}].remove" value="${hiddenValue}" />
                            <!-- Add a link to remove the Employee -->
                            <a href="#" class="employees.remove" data-index="${loop.index}">remove</a>
                        </div>
                    </c:forEach>
                    <button id="add" type="button">add</button>
                </td>
            </tr>
        </table>
        <button type="submit">OK</button>
    </form:form>

</body>
</html>

<form:form action="yourURL.htm" command="employeeDto">
    <form:input type="text" name="consult.consultTechnos[].techno.id" />
    <form:input type="text" name="consult.consultTechnos[].level" />
   <form:form>
分享到:
评论

相关推荐

    spring5mvc第二天【大纲笔记】.zip

    模型-视图-适配器(ModelAndView)是 Spring MVC 中一个常用的数据容器,用于传递模型数据到视图。 2. SpringMVC 文件上传:Spring MVC 支持文件上传功能,通过使用 `MultipartFile` 类型的参数,开发者可以在控制...

    spring mvc_02

    总结起来,`SimpleUrlHandlerMapping`是Spring MVC中一个基础且重要的组件,负责将URL映射到控制器,从而实现请求的路由。理解和熟练掌握其工作原理对于开发和维护Spring MVC应用至关重要。在"spring mvc_02"的学习...

    编写Spring MVC控制器的14个技巧(小结)

    ModelAndView对象是Spring MVC中一种重要的对象,用于传递数据和视图信息。控制器方法可以返回ModelAndView对象,以便将数据传递给视图,并将结果返回给客户端。 6. 使用redirect和forward redirect和forward是...

    spring-mvc-helloworld

    访问 `http://localhost:8010/spring-mvc-helloworld/hello/world` 显示了 Controller 中一个名为 `hello` 的方法处理的请求。这个 URL 可能是通过 `@RequestMapping` 注解在 Controller 类或方法上定义的。 7. **...

    【源码+配置教程】Maven3+Spring4+Spring MVC+mybatis3整合实例

    【源码+配置教程】Maven3+Spring4+Spring MVC+mybatis3整合实例是IT领域中一种常见的Web开发框架组合,这个压缩包提供了一套完整的应用实例,旨在帮助开发者快速理解和掌握这些技术的集成与应用。在这个项目中,...

    spring_MVC

    ### Spring MVC核心概念详解 #### 一、Spring MVC概述 ...综上所述,Spring MVC通过其先进的架构设计和功能特性,成为了现代Web开发中一个非常流行和强大的框架,极大地提高了开发效率和代码质量。

    Spring MVC复习.pdf

    Spring MVC是Java EE开发中一个流行的轻量级Web框架,它是Spring Framework的一部分,用于简化Web层的应用程序开发。Spring MVC是基于模型-视图-控制器(MVC)设计模式的实现,其中控制器负责处理用户请求,模型负责...

    SpringMVC中一个Controller多个方法

    在Spring MVC中,Controller类通常使用@Controller注解标识,并且可以包含多个处理请求的方法,这些方法通过@RequestMapping或其变体进行映射。 例如,假设我们有一个名为`UserController`的Controller类,我们可以...

    spring+MVC项目代码

    Spring是一个开源的应用框架,主要用于简化Java企业级应用的开发,而MVC模式则是软件架构中一种常用的设计模式,用于分离业务逻辑、数据和用户界面。 【描述】"TPSWebMonitorCorp项目代码,Spring+MVC+FreeMarker...

    spring管理servlet的利器--SimpleServletHandlerAdapter

    `SimpleServletHandlerAdapter`是Spring MVC中一个关键组件,它为非Spring MVC注解驱动的Servlet提供了一种简单的方式来与Spring容器进行集成。这篇文章将深入探讨`SimpleServletHandlerAdapter`的工作原理及其在...

    SSM英文文献翻译备课讲稿.docx

    9. **ModelAndView**:这是Spring MVC中一个特殊类,用于在控制器和视图之间传递数据。 10. **Conversion Service**和**Data Binding**:这两个特性简化了模型数据与HTTP请求参数之间的转换和绑定。 Spring MVC...

    javaweb框架学习

    8. **视图模型和ModelAndView**:ModelAndView是Spring MVC中一个非常重要的类,它用于封装模型数据和视图信息。开发者可以在Controller方法中创建ModelAndView对象,设置模型数据和视图名称。 9. **验证**:Spring...

    Spring In Action MVC 提交表单(2)-数据绑定和数据校验

    数据绑定是Spring MVC中一个强大的特性,它允许我们将HTTP请求参数自动映射到控制器方法的参数上。例如,当用户提交一个HTML表单时,表单字段的值可以被自动绑定到Java对象的属性上。Spring通过`@RequestParam`或`@...

    SSM框架应用开发与案例实战(Spring+SpringMVC+MyBatis)(慕课版)-PPT.rar

    "第11章 Spring Web MVC入门.ppt"和"第12章 注解式控制器开发.ppt"将带你了解SpringMVC的工作流程,包括模型-视图-控制器(MVC)设计模式,以及如何使用注解定义控制器、处理请求和返回视图。你还将学习到如何处理...

    SSH框架中SpringMVC

    拦截器是 Spring MVC 中一个强大的特性,允许在请求被处理前和处理后执行自定义逻辑。常见的应用场景包括日志记录、权限验证、性能监控等。 五、类型转换 Spring MVC 自动处理数据类型转换,例如将字符串转换为整数...

    spring reference中文版

    《Spring参考中文版》是Java开发领域中一本重要的技术文档,由知名开发者夏昕翻译,主要涵盖了Spring框架的核心概念、配置、使用方法以及最佳实践。Spring作为Java企业级应用开发的基石,其丰富的功能和强大的灵活性...

    java mvc设计模式

    在Java世界中,有多个流行的MVC框架,如Spring MVC、Struts、JSF等。以Spring MVC为例,它是Spring框架的一部分,提供了全面的MVC功能。 1. **Spring MVC的工作流程**: - 用户通过浏览器发送HTTP请求到服务器。 ...

    Spring技术内幕:深入解析Spring架构与设计原理(第2版)

    书中会深入介绍控制器、视图解析、表单处理等Spring MVC的核心概念和工作机制。 随着版本的演进,Spring不断加入新的功能和改进。本书的第二版也会针对Spring的新版本进行内容更新,以帮助读者跟上Spring的最新发展...

    SSH整合实例(SpringMvc+mybaties+Spring)

    SSH整合是Java开发中一种常见的框架集成方式,主要包括Spring、SpringMVC和MyBatis三个组件。这三种框架各自在应用程序开发中承担不同的职责,Spring作为基础架构框架,SpringMVC处理HTTP请求和响应,而MyBatis则...

    10-Spring&MVC企业级应用开发.pdf

    SpringMVC是Spring框架中用于Web应用开发的模块,它遵循MVC架构模式,使得Web应用可以更加模块化,分离了控制器、模型对象、视图对象等组件。SpringMVC的运行原理包括前端控制器的接收请求、视图解析、模型数据的...

Global site tag (gtag.js) - Google Analytics