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

时间:2015-05-24 18:04:14

标签: java forms jsp struts 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();

   }
}

1 个答案:

答案 0 :(得分:2)

要在Struts 1操作中访问表单值,您需要将$ find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '{}' \; 强制转换为页面使用的表单类型,例如ActionForm form。然后你可以正常访问它的getters。例如:

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(); } 中的值从JSP页面中获取,并且您真的无法使用taglibs *,那么可以获取表单这样:

MyForm

然后将其插入页面,如下所示:

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

但是你可能可以使用taglib并且不知道如何向它们添加css类。在taglib中,<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> 在输出html中呈现为styleClass。试试这个:

class