java – 如何将表单输入值传递给Action(struts 1)

我是新用的struts.

我需要在提交表单时将表单值传递给操作.我想使用输入标签no html:text.

怎么做?

这是我的代码:

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

}

行动:

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

   }
}

最佳答案 要在Struts 1操作中访问表单值,您需要将ActionForm表单转换为页面使用的表单类型,在您的情况下为MyForm.然后你可以正常访问它的getters.例如:

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>

但是你可能会使用taglibs而只是不知道如何向它们添加css类.在taglibs中,styleClass在输出html中呈现为class.试试这个:

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