如何将表单输入值传递给 Action (struts 1)
How can I pass form input value to an Action (struts 1)
我是新手 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();
}
}
要在 Struts 1 操作中访问表单值,您需要将 ActionForm form
转换为页面使用的表单类型,在您的例子中为 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>
但是您可能可以使用标签库,只是不知道如何添加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。
我需要在提交表单时将表单值传递给操作。我想使用 输入标签 否 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();
}
}
要在 Struts 1 操作中访问表单值,您需要将 ActionForm form
转换为页面使用的表单类型,在您的例子中为 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>
但是您可能可以使用标签库,只是不知道如何添加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>