Ali
Ali

Reputation: 267077

Making Struts2 work with normal / hand-written HTML forms

Is it possible to make Struts 2 work with existing forms which have already been written, but which aren't using Struts 2 to generate the forms (e.g by using <s:textbox>, <s:radio> etc).

Primarily, I'm only interested in the validation and handling of these forms. The forms are already being rendered and displayed as needed. I just need to be able to get the values in the backend, validate them, and process them. Is that possible?

Upvotes: 2

Views: 637

Answers (1)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

This is how we can use simple HTML tags to submit values to Struts2 action.

JSP

<form action="" method="post">
  <input type="text" name="field1" value=""/>
  <input type="text" name="field2" value=""/>
  <input type="submit">
</form> 

Action

public class MyAction extends ActionSupport{

 private String field1;
 private String field2;
 public String getField1()
    {
        return field1;
    }

    public void setField1( String field1 )
    {
        this.field1 = field1;
    }

    public String getField2()
    {
        return field2;
    }

    public void setField2( String field2 )
    {
        this.field2 = field2;
    }

    public String execute(){
       System.out.println("******************************************** "+field1);
       System.out.println("******************************************** "+field2);
       return true;
    }

}

You will be able to receive values in your action class and free to use any logic you want, only drawback of this approach is that you will not be able to make direct use of some features being provided by Struts2 tags but there are other ways to do that.

Hope this will help you

Upvotes: 2

Related Questions