Struts :
在表单提交过程的经验:
在表单的<html:text property="managerName"></html:text>中property和FROM的对应字段名称相符合最好!!!以免出现不必要的错误++ Admin_Name->admin_Name 属性名小写 要不会出现javax.servlet.ServletException: No getter method for property Admin_Name of bean org.apache.struts.taglib.html.BEAN的错误
action 中config配置中FORM和ACTION 是一对多的对应!!
一下是截取http://blog.youkuaiyun.com/hotdust/archive/2007/04/26/1586343.aspx的内容 值得看
1. 在struts中,要得到一个最小化的struts是什么样的呢?
先说说这里最小化的含意。在这里,最小化就是指用struts实现一个JSP页面跳到另一个JSP页面就行。如果你想再小的话,那就只是一个strtus框架了(实现一个struts只要把各个JAR和TLD文件放到相应的LIB和WEB-INF目录中就行了)。下面说说struts这里所说的最小化结构。
最小化的struts只要实现一个Action就行,因为在struts中,ActionServlet是必须有的。ActionServlet是控制器,他要知道你要跳传的页面地址是什么,又因为在strtus架构中规范中,ActionServlet只接收的地址包装所要地址的ActionForward对像。在struts中,(据我现在所知)只有ACTION能返回ActionForward对象。所以,只要实现一个ACTION就可以了。
例:
2. 如果要实现一个在JSP页面中带有<html:form>的最小struts框架,那又是怎么样的呢?
(1) 在JSP页面中写一个<html:form></html:form>,在<html:form>的action属性设置为”/”
例:
<
html:form
action
=
"/"
>
</
html:form
>
(2) 在struts-config.xml中,写一个ActionMapping,内容如下:
<
action-mappings
>
<
action
path
=
"/"
></
action
>
</
action-mappings
>
(action和path属性最好写上你已经写好的Action类,这么写的原因是让些JSP可以通过编译)
(3)
接着写一个
ActionForm
,要写完整了,要不然会出错
<
form-beans
>
<
form-bean
name
=
"helloForm"
type
=
"hello.HelloForm"
>
</
form-bean
>
</
form-beans
>
这时,ActionMapping也要改成:
<
action-mappings
>
<
action
path
=
"/"
type
=
""
name
=
"helloForm"
>
</
action
>
</
action-mappings
>
要不然会出错
这样,一个有<html:form>的最小strtuts就完成了。
:)
通过上面的步骤说一说这个带<html:form>最小struts的工作流程吧。
(1)当你把带有<html:form>的JSP写完后,struts-config.xml只写上基本的头时,例:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
struts-config
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"
>
<
struts-config
></
struts-config
>
编译器会报错:“javax.servlet.ServletException: Cannot retrieve mapping for action /”。由此可以看出,当出现一个<html:form>时,编译器首先要找的是和这个form对应的ActionMapping的Action
(2)在把ActionMapping设置成
<
action-mappings
>
<
action
path
=
"/"
></
action
>
</
action-mappings
>
后,再进行编译JSP,编译器会报错:“
javax.servlet.ServletException: Cannot retrieve definition for form bean null”。这是因为编译器接下来想找与<html:form>对应的ActionForm,你的action中没有配置ActionForm的对象,自然就会报错。好,我们把他补上,变成:
<
action-mappings
>
<
action
path
=
"/"
type
=
""
name
=
"helloForm"
>
</
action
>
</
action-mappings
>
(4) 这下会怎么样呢?唉,还会报错,这是为什么?因为当你在action中指定了与form相对应的ActionForm的对象后,编译自动会实例化一个你编写的ActionForm的对象,它在struts-config.xml中找不到〈form-bean〉的配置清单,所以它不能根据〈form-bean〉配置清单创建一个ActionForm的实例,就报出刚才的错误。
(5) 然后我们创建一个ActionForm,这个form要写完整了,可以没有validate方法,和reset方法,但不可以少getXxx和setXxx方法,如果少了这两个方法的话,会报错的。