现在的位置: 首页 > 综合 > 正文

struts1.x中ActionForm的作用

2013年10月14日 ⁄ 综合 ⁄ 共 979字 ⁄ 字号 评论关闭

一:通常是用来取网页中html标签的值,概念性的东西就不说了。通常就是ActionForm中设置变量属性和相应得getset方法。之后再action中调用actionFrom得到页面的值。我们只需写好actionForm,struts1会自动把页面的相应值匹配上。比如:页面上有2个标签,我们需要在action中得到这个2个标签的值新建一个包,包名叫com,TestForm和TestAction都建在com包下test.html:

<form action="xx.do">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" value="提交" />
</form>

 

二:在struts-config.xml中的配置:

<struts-config>
 <form-beans>
      <form-bean name="newsForm" type="com.NewsForm" />
      </form-beans>
<action path="/TestAction" name="newsForm" type="com.TestAction">

</action>

三:TestForm.java:

String username;
String password;
public static setUsername(String username){
this.username=username;
}
public String getUsername(){
return username;
}
public setPassword(String password){
this.password=password;
}
public String getPassword(){
return password;
}

四:TestAction.java:

public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
TestForm tf=(TestForm)form;
tf.getPassword();
tf.getUsername();
}

 

抱歉!评论已关闭.