【问题标题】:Re-assign value tag after first form submission?第一次提交表单后重新分配值标签?
【发布时间】:2015-02-02 16:20:46
【问题描述】:

假设我有以下课程

public class Account {

  private Long id;
  private String username;
  private String password;

  // getters + constructor

}

下面的动作类

public MyAction extends ActionSupport {

  private CoreService service;

  private long id;
  private String username;
  private String password;

  private Account account;

  public String loadAccount() {
    account = service.getAccountById(id);
    return SUCCESS;
  }

  public String updateAccount() {
    service.updateAccount(account);
    return SUCCESS;
  }
  // getters + setters
}

我使用 loadAccount() 方法填充表单的字段。

还有我的表格:

<form action="update_account" method="post">
      <label for="username">New username</label>
      <input type="text" name="username" value="${account.username}" id="username" placeholder="Enter name" required>

      <label for="password">New password</label>
      <input type="password" name="password" id="password" placeholder="Enter new password" required>
</form>

现在,假设用户将写下新用户名,但验证失败。 “用户名”文本字段中的值将被重置为帐户的用户名。我想更改我的程序并使其最初加载帐户的用户名,但如果验证失败,它将加载最后一个输入。

【问题讨论】:

  • 你只需要改变一个表达式,它就会保留最后一个值。
  • 最初是否仍会加载我帐户的用户名?
  • 是的,如果您将其设置为字段。
  • 我有点困惑。你能告诉我应该改变哪个表达式吗?
  • 表达式${account.username}

标签: java forms struts2 jstl struts


【解决方案1】:

更改显示字段的代码

<input type="text" name="username" value="${username}" id="username" placeholder="Enter name" required>

然后是加载后设置的方法

public String loadAccount() {
  account = service.getAccountById(id);
  setUsername(account.getUserName());
  return SUCCESS;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多