Search code examples
javajspservletsstrutsstruts-1

Struts 1: Entry ActionForm inputs from any Action class


suppose i want to create form with Struts, so my struts-config will like this:

<form-bean name="myForm" type="com.MyForm" />


<action path="/entryyourinputs"
    type="com.MyAction" name="myForm"
    input="/myJsp.jsp">

    <forward name="success" path="/regSuccess.jsp" />
</action>

it will show myJsp to assign the myForm.
my question is how to assign myForm in MyAction instead of the myJsp?.
or what is the method to assign some myForm's inputs from MyAction?

best regards
Thanks


Solution

  • In com.MyForm we need to create getters and setters for that inputs like the following:

    String input;
    public String getInput() {return this.input;}
    
    public void setInput(String input) {this.input = input;}
    

    In MyAction which extends struts Action class, we assign values to the MyForm inputs like the following:

    public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
      MyForm myForm = (MyForm) form;
    // assigning values to the inputs from action
      myForm.setInput("input value");
    
     .....
     .....
     .....
    }
    

    to get the values in jsp we have to use html tag lib:

    The Jsp code look like the below code:

    <html:form action="/entryyourinputs">
       <html:text name="myForm" property="input" />
    </html:form>
    

    Note: The html tags must be inside of the **<html:form>** tag.

    input fileds in jsp the values which are assigned in MyAction class automatically assigned to the jsp input fields, because of the controller.