假设我想使用 Struts 创建表单,那么我的 struts-config 将如下所示:
<form-bean name="myForm" type="com.MyForm" />
<action path="/entryyourinputs"
type="com.MyAction" name="myForm"
input="/myJsp.jsp">
<forward name="success" path="/regSuccess.jsp" />
</action>
它将显示 myJsp 来分配 myForm。
我的问题是如何在 MyAction 而不是 myJsp 中分配 myForm?
或者从 MyAction 分配某些 myForm 输入的方法是什么?
最诚挚的问候
谢谢
最佳答案
在 com.MyForm 中,我们需要为该输入创建 getter 和 setter,如下所示:
String input;
public String getInput() {return this.input;}
public void setInput(String input) {this.input = input;}
在扩展了 struts Action 类的 MyAction 中,我们将值分配给 MyForm 输入,如下所示:
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
MyForm myForm = (MyForm) form;
// assigning values to the inputs from action
myForm.setInput("input value");
.....
.....
.....
}
要获取 jsp 中的值,我们必须使用 html 标签库:
Jsp 代码如下所示:
<html:form action="/entryyourinputs">
<html:text name="myForm" property="input" />
</html:form>
注意:html 标签必须位于 **<html:form>**
内标签。
jsp 中的输入字段 由于 Controller 的原因,MyAction 类中分配的值会自动分配给 jsp 输入字段。
关于java - 支柱 1 : Entry ActionForm inputs from any Action class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24501341/