【发布时间】: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