【问题标题】:How can I pass form input value to an Action (struts 1)如何将表单输入值传递给动作(struts 1)
【发布时间】:2015-05-24 18:04:14
【问题描述】:

我是使用 struts 的新手。

我需要在提交表单时将表单值传递给操作。我想使用 输入标签 没有 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>

动作表单:

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

   }
}

【问题讨论】:

  • 你有什么错误吗?您能否更好地描述您的问题是什么以及为什么此解决方案不起作用?
  • 当我在输入中输入值时,我需要将这些值传递给操作,之后,当我提交页面时,我需要保留在相应字段中输入的值。
  • 你应该在 doPost 方法中编写代码
  • @AmarAgrawal 你指的是什么doPost 方法?操作有一个execute 方法。
  • 需要更多细节;如果提交正在发生,只需获取表单实例,值就会在那里。我也会考虑大多数基本的 Struts 1 教程,它会涵盖必要的内容。请注意,您将通过使用纯 HTML 输入标记放弃一些 S1 功能,例如在验证错误时填充值等。

标签: java forms jsp struts struts-1


【解决方案1】:

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

}

如果您遇到的问题是将 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>

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

【讨论】:

  • 如何在没有表单数据的情况下使用 struts 1 进行发布请求,而是使用我想要作为 url 编码发送的字符串?
猜你喜欢
  • 2011-09-07
  • 2012-10-03
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 2022-07-08
  • 1970-01-01
相关资源
最近更新 更多