Search code examples
struts2elvaluestack

Can't prepopulate a form with html tags


I want to show a prepopulated form in jsp.

TestAction.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  **/  
}

When I use html tags, it fails to do so,

Test.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>

when instead I use struts2 tags, It works fine.

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

Can this be achieved using non struts2 tags ?


Solution

  • You can use 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>
    

    The values are strings, so better to escape them for safety.

    If this jsp returned as a result of the action the variables along with standard scopes also searched the value stack. The action properties should be available from the value stack.