4

I'm new using struts.

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

How to do it?

This is my code:

form 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>

struts-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);
    }

}

Action:

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();

   }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Oscar MV
  • 63
  • 1
  • 1
  • 7
  • Are you having some kind of error? Can you describe better what is your problem and why this solution does not work? –  May 25 '15 at 09:37
  • When I enter the values in the inputs, I need to pass these values to action and after, when I submit the page I need to keep the values entered in the appropriate fields. – Oscar MV May 25 '15 at 10:07
  • you should write the code in the doPost menthod in action – Amar Agrawal May 26 '15 at 11:02
  • @AmarAgrawal What `doPost` method are you referring to? Actions have an `execute` method. – Dave Newton May 26 '15 at 12:36
  • More details are required; if the submission is happening, just get the form instance and the values will be there. I'd also consider most any basic Struts 1 tutorial which will cover what's necessary. Note that you'll give up some S1 functionality by using the plain HTML input tags, like filling on the values back on a validation error, etc. – Dave Newton May 26 '15 at 12:38
  • When I submit the page I need to get the values, that are using in other page, and keep it into the fields. I use the input tags because I use Bootstrap for the style of the form, and it's not available in Struts 1 tags. – Oscar MV May 27 '15 at 10:40

1 Answers1

2

To access form values in a Struts 1 action, you need to cast ActionForm form to the type of form that the page uses, in your case MyForm. Then you can access its getters like normal. For example:

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();

}

If the problem you're having is getting the values out of MyForm into the JSP page, and you truly cannot use taglibs*, then you can get the form like this:

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

Then insert it into the page like this:

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

BUT you probably can use taglibs and just don't know how to add css classes to them. In the taglibs, styleClass renders to class in the output html. Try this:

<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>
Mar
  • 7,765
  • 9
  • 48
  • 82
  • how do I do a post request using struts 1 without form data but rather a string that I want to send as url encoded? – Euridice01 Feb 09 '19 at 16:40