当前位置: 代码迷 >> Java Web开发 >> Struts中调用ActionForm的getxxx()方法的疑问
  详细解决方案

Struts中调用ActionForm的getxxx()方法的疑问

热度:65   发布时间:2006-07-21 22:39:39.0
不是,一个Action只能有一个表单,但是一个表单可以对应到多个Action

但是你调用是调用Action,Action确定了Action所对应的表单也就确定了,所以在一个WEB提交(submit)中,Action和Form是一对一的关系。
----------------解决方案--------------------------------------------------------
一个表单可以对应到多个Action
-----------------
既然 在一个WEB提交(submit)中,Action和Form是一对一的关系。
那么,一个表单怎么对应到多个Action呢?

在写语句的时候,不是 <html:form action="/send.do" method="post">

这里 action="/send.do" 它就只能对应一个Action呀,你说的 一个表单可以对应到多个Action 是什么意思呢?

----------------解决方案--------------------------------------------------------
打个比方:
比如你要写个学生注册的系统,那么我们有学生类
public class Student {
    Integer id;
    String number;
    String name;
    String sex;
}

假设我们系统的功能有两个功能:
1.添加
2.修改

你会发现,这两个功能所提交的字段只有一个不一样,修改的要比添加的多提交一个id字段,而添加功能只要提交number,name,sex就好了。那么你打算怎么写ActionForm和Action呢?

如果你把两个功能各都写各自对应的Form --
public class UpdateStudentForm extends ActionForm{
    Integer id;
    String number;
    String name;
    String sex;
}

public class AddStudentForm extends ActionForm{
    String number;
    String name;
    String sex;
}

你会很无辜的发现,其实这两个根本就查不多一样。
这个时候,你就可以利用一个ActionForm可以对应到多个Action中去。

public class StudentForm extends ActionForm{
    Integer id;
    String number;
    String name;
    String sex;
}

&lt;action name="StudentForm" path="/UpdateStudentAction"/&gt;
&lt;action name="StudentForm" path="/AddStudentAction"/&gt;

看见了吧,我们就把StudentForm对应到多个Action中去了
----------------解决方案--------------------------------------------------------
<action name="StudentForm" path="/UpdateStudentAction"/>
<action name="StudentForm" path="/AddStudentAction"/>
---------------------------
这是在配置文件中要这么写,可是在jsp页面中要怎么写才能定义一个表单分别对应UpdateStudentAction和AddStudentAction 这两个Action呢?

就像这种:<html:form action="/send.do" method="post">


----------------解决方案--------------------------------------------------------
不可能
----------------解决方案--------------------------------------------------------

那ActionServlet它不就是根据 action="/xxx.do" 到配置文件中去找Action的parth的吗,既然 不可能,那ActionServlet是如何确定此时应该调用哪个Action呢?



----------------解决方案--------------------------------------------------------
通过配置文件呀

&lt;action name="StudentForm" path="/AddStudentAction"/&gt;
这样你就必须要调用 action = "AddStudentAction.do"

如果你的配置是
&lt;action name="StudentForm" path="/InsertStudent"/&gt;
那么你就要调用 action = "InsertStudent.do"

后面那.do是在你项目的web.xml中配置的,我就经常不用.do,用.asp来迷惑人家
----------------解决方案--------------------------------------------------------
那 action = "AddStudentAction.do"跟 action = "InsertStudent.do" 是写在哪里的,不是在jsp页面里吗?

----------------解决方案--------------------------------------------------------
我现在就琢磨不出来,这一个表单在jsp页面里要如何写代码才能实现到配置文件里去找相应的Action呢?
----------------解决方案--------------------------------------------------------
action = "AddStudentAction.do" 是写在HTML里面的

一个表单是不能自己找到想去的Action的,而是由Action自己寻找自己需要的表单。
----------------解决方案--------------------------------------------------------