【问题标题】:Spring MVC Form static textSpring MVC 表单静态文本
【发布时间】:2012-10-28 12:03:32
【问题描述】:

我正在尝试使用 Spring MVC 制作表单以编辑某些对象。该对象的某些字段是在创建时设置的,然后无法编辑,因此它们必须像纯文本一样显示在编辑表单上,而不是像输入一样。 当我在 spring 表单标签库中没有找到像 spring:output 这样的东西时,我感到非常惊讶。怎么办?请帮忙 :) 这是我的表单代码:

    <?xml version="1.0" encoding="UTF-8" ?>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
    <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>

    <h1><spring:message code="Information" /></h1>

    <form:form commandName="shop" method="POST">
        <table>
            <tr>
                <td><spring:message code="ShopCompanyName"/>
                <td colspan="2">**HERE MUST BE A STATIC TEXT 
FILLED WITH THE VALUE OF SOME FIELD FROM OBJECT**</td>
            </tr>
            <tr>
                <td colspan="3"><h2><spring:message code="Contacts"/></h2></td>
            </tr>
            <tr>
                <td><spring:message code="ShopUserLN"/></td>
                <td><form:input path="user.lastName"/></td>
                <td><form:errors path="user.lastName" cssClass="error"/></td>
            </tr>
            <tr>
                <td><spring:message code="ShopUserFN"/></td>
                <td><form:input path="user.firstName"/></td>
                <td><form:errors path="user.firstName" cssClass="error"/></td>
            </tr>
            <tr>
                <td><spring:message code="ShopUserMN"/></td>
                <td><form:input path="user.middleName"/></td>
            </tr>
            <tr>
                <td><spring:message code="ShopUserPhone"/></td>
                <td><form:input path="user.phone"/></td>
                <td><form:errors path="user.phone" cssClass="error"/></td>
            </tr>
        </table>
        <input type="submit" value="Save">
    </form:form>

【问题讨论】:

    标签: java forms spring


    【解决方案1】:
    <c:out value="${someObject.someProperty}"/>
    

    甚至

    ${someObject.someProperty} 
    

    如果不需要 XML 转义。

    【讨论】:

    • 感谢 工作!但是当提交表单时,它会将该字段值提交给控制器吗?
    • 没有。正如您所要求的,这是静态文本。如果你想提交它(但为什么不简单地在服务器端重新生成它),那么你需要一个隐藏的表单字段。
    • 要在服务器端重新生成它,我将不得不再次通过 id 从数据库中获取此对象,使用提交的对象的值更新检索到的对象中的所有可编辑文件,然后保存更新的对象..这不好我认为..当整个对象被保存时,使用一个静态字段显示数据和一个隐藏字段将其子节点到服务器是否是一种好方法,所以它不会是空的?
    • 你是对的。但是如果表单的逻辑会被修改,例如,如果一些新的字段会被添加到对象中。我不仅要修改表单和对象本身,还要修改表单处理逻辑,即用提交的值更新旧对象的过程。 Spring MVC 是否正常且唯一的方法?
    • 您添加一个方法,该方法获取SomeObject 以按ID 进行编辑,并使用ModelAttribute 对其进行注释。这使得 Spring 自动调用该方法并将 SomeObject 实例添加到模型中进行编辑。然后将SomeObject 类型的参数添加到processSubmit() 方法,也使用ModelAttribute 进行注释。 Spring 将看到该对象已经在模型中,并将使用请求参数自动填充它。这一切都在我给你的链接中解释了。
    【解决方案2】:

    如果您已将输出分离到标记文件,则可以使用 spring 数据绑定和状态对象,以避免表单名称或对象“shop”的切换:

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%@ taglib prefix="my" tagdir="/path/to/tag"%>
    
    <form:form commandName="shop" method="POST">
        <my:formFieldFixed path="name"/>
    </form>
    

    并在 /path/to/tag/formFieldFixed.tag 文件中

    <%@ attribute name="path" required="true" type="java.lang.String" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    
    <spring:bind path="${path}">
        <span>${status.value}</span>
    </spring:bind>
    

    通过这样的绑定,您还可以将表单错误与表单容器分开,等等。在此处查看示例: https://docs.spring.io/spring/docs/1.1.5/taglib/tag/BindTag.html

    【讨论】:

      【解决方案3】:

      如果您想在 Spring 中在一行中创建多个字段,那么您可以使用以下代码:

      <tr>
          <td><form:label path="first_name">First Name:</form:label></td>
          <td><form:input path="first_name" /></td>
      
          <td><form:label path="last_name">Last Name:</form:label></td>
          <td><form:input path="last_name" /></td>
      </tr>
      

      这是在单行中创建表单字段的最简单方法...

      【讨论】:

      • 问题是“如何放置纯文本,而不是输入。”
      猜你喜欢
      • 2018-02-17
      • 2013-06-12
      • 2012-01-24
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 2023-04-01
      • 2011-11-26
      相关资源
      最近更新 更多