0

我想在 jsp 中显示一个预填充的表单。

测试动作.java

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {

    private String firstName;
    private String lastName;

    public String execute(){

        setFirstName("John");
        setLastName("Doe");

        return SUCCESS;
    }

    /** Getters & Setters  **/  
}

当我使用 html 标签时,它没有这样做,

测试.jsp

<!DOCTYPE html>
<html>
<head></head>
<body>

  <form>
    First Name <input type="text" name="firstName" > <br/>
    Last Name  <input type="text" name="lastName">
  </form>

</body>
</html>

相反,当我使用 struts2 标签时,它工作正常。

<s:form>
    <s:textfield name="firstName"></s:textfield>
    <s:textfield name="lastName"></s:textfield>
</s:form>

这可以使用非 struts2 标签来实现吗?

4

1 回答 1

1

您可以使用 JSP EL

<form>
  First Name <input type="text" name="firstName" value="${fn:escapeXml(firstName)}"><br/>
  Last Name  <input type="text" name="lastName" value="${fn:escapeXml(lastName)}">
</form>

这些值是字符串,因此为了安全起见最好将它们转义。

如果此 jsp 作为操作的结果返回,则变量以及标准范围也会搜索值堆栈。动作属性应该可以从值堆栈中获得。

于 2016-03-05T17:45:02.860 回答