一、动态ActionForm
1.配置struts环境
2.创建一个new.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:html lang="true">
<body>
<html:form action="news.do" method="post">
<html:text property="user"></html:text>
<html:password property="password"></html:password>
<html:submit value="提交"></html:submit>
</html:form>
</body>
</html:html>
3.在struts-config.xml中配置ActionForm的2个属性:user、password
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="newsForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="user" type="java.lang.String"></form-property>
<form-property name="password" type="java.lang.String"></form-property>
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings />
<message-resources parameter="com.learn.struts.ApplicationResources" />
</struts-config>
4.通过Eclipse,来创建NewsAction.java
点击window,找到customize perspective,进行设置
在src右键new,找到struts 1.3 action,进行设置
5.对struts-config.xml再次进行设置
在<action-mappings>之间添加如下代码
<action-mappings >
<action
path="/news"
scope="request"
type="com.learn.struts.action.NewsAction"
attribute="newsForm"
name="newsForm"
input="/new.jsp"
/>
</action-mappings>
6.在NewsAction.java类中添加代码后,运行
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.learn.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
/**
* MyEclipse Struts
* Creation date: 09-28-2014
*
* XDoclet definition:
* @struts.action scope="request" validate="true"
*/
public class NewsAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
DynaActionForm dynaForm=(DynaActionForm ) form;
String user=dynaForm.getString("user");
String password=dynaForm.getString("password");
System.out.println("user:"+user);
System.out.println("password:"+password);
return null;
}
}
运行结果:
http://localhost:8080/strutsProject/new.jsp