How can I pass the value of the form input to Action (struts 1)

I am using struts.

I need to pass the value of the form to the action when I submit the form. I want to use the no html: text input tag .

How to do it?

This is my code:

in JSP:

<form class="form-horizontal" action="address.do" method="post">
    Name: <input type="text" class="form-control" name="name"><br>
    City <input type="text" class="form-control" name="city"><br>
    Country: <input type="text" class="form-control" name="country"><br>
    <button class="btn btn-success" type="submit">Submit</button>
</form>

spacers-config.xml:

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

<global-forwards>
    <forward name="pagAddress" path="/address.do"/>
</global-forwards>

<action-mappings>
    <action path="/address"
            type="com.action.MainAction"
            name="myForm"            
            scope="request" 
            input="/addressInput.jsp" 
            validate="true">
        <forward name="success" path="/addressInput.jsp"/>
    </action>
</action-mappings>

Actionform:

public class MyForm extends ActionForm{

private static final long serialVersionUID = -XXXXXXXXXXXXXXXXXXXX;
private String name = "";
private String city = "";
private String country = "";

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}

@Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.name = null;
        this.city = null;
        this.country = null;
        super.reset(mapping, request);
    }

}

Act:

public class MainAction extends Action {

   public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        if(getErrors(request) == null ||getErrors(request).size() == 0)
            return mapping.findForward("success");
        else
            return mapping.getInputForward();

   }
}
+4
source share
1 answer

In order to access the form values ​​in the Struts 1 action, you need to specify the ActionForm formtype of form the page uses, in your case MyForm. Then you can access your getters as usual. For instance:

public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    MyForm theForm = (MyForm) form;
    String name = theForm.getName();

    if (name == null || "".equals(name)) {
        // error case; name is required
        // do something
    }

    if(getErrors(request) == null ||getErrors(request).size() == 0)
        return mapping.findForward("success");
    else
        return mapping.getInputForward();

}

, MyForm JSP, taglibs *, :

<%
MyForm theForm = (MyForm) session.getAttribute("MyForm");
%>

:

<form class="form-horizontal" action="address.do" method="post">
    Name: <input type="text" value="<%= theForm.getName() %>" class="form-control" name="name"><br>
    City <input type="text" value="<%= theForm.getCity() %>" class="form-control" name="city"><br>
    Country: <input type="text" value="<%= theForm.getCountry() %>" class="form-control" name="country"><br>
    <button class="btn btn-success" type="submit">Submit</button>
</form>

, , taglib , css. taglibs styleClass class html. :

<html:form styleClass="form-horizontal" action="address.do" method="post">
    Name: <html:text styleClass="form-control" name="name" /><br>
    City <html:text styleClass="form-control" name="city" /><br>
    Country: <html:text styleClass="form-control" name="country" /><br>
    <button class="btn btn-success" type="submit">Submit</button>
</html:form>
+1

All Articles