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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30426630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:06:20  来源:igfitidea点击:

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

javaformsjspstrutsstruts-1

提问by Oscar MV

I'm new using struts.

我是使用 struts 的新手。

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

当我提交表单时,我需要将表单值传递给一个操作。我想使用没有 html:text 的输入标签

How to do it?

怎么做?

This is my code:

这是我的代码:

form in JSP:

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:

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

   }
}

采纳答案by Martin Carney

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

要在 Struts 1 操作中访问表单值,您需要转换ActionForm form为页面使用的表单类型,在您的情况下为MyForm. 然后你可以像往常一样访问它的吸气剂。例如:

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 MyForminto the JSP page, and you truly cannot use taglibs*, then you canget the form like this:

如果您遇到的问题是越来越值出MyForm到JSP页面,你真的不能使用标签库*,那么你就可以得到这样的形式:

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

BUTyou probably canuse taglibs and just don't know how to add css classes to them. In the taglibs, styleClassrenders to classin the output html. Try this:

但是您可能可以使用 taglibs,只是不知道如何向它们添加 css 类。在标签库中,在输出 html 中styleClass呈现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>