【问题标题】:Too much logic in view for MVC pattern?MVC 模式的逻辑太多?
【发布时间】:2018-12-02 11:45:00
【问题描述】:

这是我添加新用户的 HTML 模板:

<div layout:fragment="content">
<div th:if="${#request.getParameter('successful') != null}" class="card m-3">
    <div th:if="${#request.getParameter('successful') == 'true'}" class="card-body text-white bg-success">
        <span th:text="#{employees.add.succesful}"></span>
    </div>
</div>
<div class="card m-3">
    <div class="card-header">
        <h4 th:text="#{employees.add.title}" class="card-title"></h4>
        <p th:text="#{employees.add.subtitle}" class="card-subtitle text-muted"></p>
    </div>
    <div class="card-body">
        <form action="#" th:action="@{/employees/add}" th:object="${user}" method="post">
            <div class="form-group">
                <label th:text="#{employees.add.first_name}" for="first_name"></label>
                <input th:placeholder="#{employees.add.first_name}" th:errorclass="is-invalid" th:field="*{firstName}" type="text" class="form-control" id="first_name" required>
                <div class="invalid-feedback" th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}"></div>
            </div>
            <div class="form-group">
                <label th:text="#{employees.add.last_name}" for="last_name"></label>
                <input th:placeholder="#{employees.add.last_name}" th:errorclass="is-invalid" th:field="*{lastName}" type="text" class="form-control" id="last_name" required>
                <div class="invalid-feedback" th:if="${#fields.hasErrors('lastName')}" th:errors="*{lastName}"></div>
            </div>
            <div class="form-group">
                <label th:text="#{employees.add.email}" for="email"></label>
                <input th:placeholder="#{employees.add.email}" th:field="*{email}" th:errorclass="is-invalid" type="email" class="form-control" id="email" required>
                <div class="invalid-feedback" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
            </div>
            <p><button th:text="#{button.submit}" type="submit" class="btn btn-primary"></button><button th:text="#{button.reset}" type="reset" class="btn btn-secondary"></button></p>
        </form>
    </div>
</div>
</div>

它是否包含太多逻辑?我们试图坚持 MVC 模式,这意味着,我们不能在视图中承受太多的逻辑。 如果你觉得里面有太多的逻辑,我们该如何改进呢?哪些部分可以移动到 Java 代码中?

【问题讨论】:

    标签: java spring spring-mvc spring-boot model-view-controller


    【解决方案1】:

    MVC 模式中您不想要的 逻辑业务逻辑,我在您的代码中看不到任何业务逻辑,因此您无法改进它那种感觉

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 1970-01-01
      • 2011-07-04
      • 2012-01-29
      • 2011-05-30
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多