Stripes - 绑定到集合的动态生成的输入字段无法设置为空

发布于 2024-09-14 10:39:44 字数 2224 浏览 3 评论 0原文

我正在一个项目中使用 Stripes,但遇到了我无法理解的情况。在我的操作 bean 中,我有一个对象列表(用于设置应用程序配置参数),在 jsp 中,我为每个对象动态创建输入字段。对于正常编辑,一切正常,但是如果我尝试将字段设置为空,则对象值仍保持以前的值。我已经广泛地查看了代码,并且确信我们编写的任何代码都没有这样做。当我调试时,我可以看到除了空白配置对象之外的所有配置对象都调用了我的对象的 setValue() 方法,而不是使用空字符串调用 setValue (这是我所期望的)。

有谁知道 Stripes 是否在幕后做一些影响此的事情?

谢谢

代码片段:

对象代码:

public class Configuration implements Serializable {
  @Id
  @Basic(optional = false)
  @Column(name = "id", nullable = false)    
  private Integer id;

  @Basic(optional = false)
  @Column(name = "name", nullable = false, length = 100, updatable = false)
  private String name;

  @Basic(optional = true)
  @Column(name = "value", nullable = true, length = 200)
  private String value;
  ...
  public void setValue(String value) {
    //This is never called when I empty my input field
    this.value = value;
  }
  ...
 }

Action Bean 代码:

public class ConfigActionBean extends BaseActionBean {
  private List<Configuration> allConfigurationEntries;

  @Before(stages = LifecycleStage.BindingAndValidation)
  public void rehydrate() {
    allConfigurationEntries = configurationService.getAllEntries();        
  }

  @DefaultHandler
  public Resolution view() {
    return new ForwardResolution(
            "/WEB-INF/jsp/admin/configuration.jsp");
  }

  public Resolution Save() {       
    configurationService.saveAllEntries(allConfigurationEntries,is);
    return new RedirectResolution(ConfigActionBean.class,"view");
  }

  public void setAllConfigurationEntries(
        List<Configuration> allConfigurationEntries) {
    this.allConfigurationEntries = allConfigurationEntries;
  }

  public List<Configuration> getAllConfigurationEntries() {
    return allConfigurationEntries;
  }
  ...
}

查看 JSP 代码:

...
<c:forEach items="${actionBean.allConfigurationEntries}" var="items" varStatus="loop">
   <div>
      <s:label for="allConfigurationEntries[${loop.index}].value">${items.name}</s:label>
      <s:text id="${items.name}" name="allConfigurationEntries[${loop.index}].value" value="${items.value}" />
   </div>
</c:forEach>
...

I am using Stripes for a project and have a situation I cannot understand. In my action bean I have a list of objects (for setting app config params) and in the jsp I am dynamically creating input fields for each object. For a normal edit everything works fine however if I try and set a field to empty the object value remains what it was previously. I have looked extensively through the code and am confident it is not being done by any of the code we have written. When I debug I can see that the setValue() method of my object is being called for all configuration objects except the one that is blank, rather than setValue being called with an empty string which is what I would expect.

Does anyone know if Stripes is doing something under the hood which is affecting this?

Thanks

Snippets of Code:

Object code:

public class Configuration implements Serializable {
  @Id
  @Basic(optional = false)
  @Column(name = "id", nullable = false)    
  private Integer id;

  @Basic(optional = false)
  @Column(name = "name", nullable = false, length = 100, updatable = false)
  private String name;

  @Basic(optional = true)
  @Column(name = "value", nullable = true, length = 200)
  private String value;
  ...
  public void setValue(String value) {
    //This is never called when I empty my input field
    this.value = value;
  }
  ...
 }

Action Bean Code:

public class ConfigActionBean extends BaseActionBean {
  private List<Configuration> allConfigurationEntries;

  @Before(stages = LifecycleStage.BindingAndValidation)
  public void rehydrate() {
    allConfigurationEntries = configurationService.getAllEntries();        
  }

  @DefaultHandler
  public Resolution view() {
    return new ForwardResolution(
            "/WEB-INF/jsp/admin/configuration.jsp");
  }

  public Resolution Save() {       
    configurationService.saveAllEntries(allConfigurationEntries,is);
    return new RedirectResolution(ConfigActionBean.class,"view");
  }

  public void setAllConfigurationEntries(
        List<Configuration> allConfigurationEntries) {
    this.allConfigurationEntries = allConfigurationEntries;
  }

  public List<Configuration> getAllConfigurationEntries() {
    return allConfigurationEntries;
  }
  ...
}

View JSP Code:

...
<c:forEach items="${actionBean.allConfigurationEntries}" var="items" varStatus="loop">
   <div>
      <s:label for="allConfigurationEntries[${loop.index}].value">${items.name}</s:label>
      <s:text id="${items.name}" name="allConfigurationEntries[${loop.index}].value" value="${items.value}" />
   </div>
</c:forEach>
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御守 2024-09-21 10:39:44

Web 浏览器不会向 http 请求添加空字段。因此,通过删除 @Before 方法并将此代码放入 view() 方法中,您的问题将得到解决。

Webbrowsers don't add empty fields to the http request. Thus by removing the @Before method and putting this code into the view() method you're issue will be resolved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文