动态Formbean

DynaActionForm是ActionForm的子类,可以创建带有动态属性结合的form beans,不需要开发者创建form bean类

先回忆下静态ActionForm 

package demo;

import org.apache.struts.action.ActionForm;

public class StudentForm extends ActionForm {
	private String sname;
	private String major;
	private float score;
	private java.sql.Date birth;
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getMajor() {
		return major;
	}
	public void setMajor(String major) {
		this.major = major;
	}
	public float getScore() {
		return score;
	}
	public void setScore(float score) {
		this.score = score;
	}
	public java.sql.Date getBirth() {
		return birth;
	}
	public void setBirth(java.sql.Date birth) {
		this.birth = birth;
	}
	
}

package demo;

import java.io.PrintWriter;

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;

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		StudentForm loginForm=(StudentForm)form;
		PrintWriter out=response.getWriter();
		out.print(loginForm.getSname()+" "+loginForm.getMajor()+" "+loginForm.getBirth()+
				" "+loginForm.getScore());
//		
		return null;
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foudation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
	<form-beans>
		<form-bean name="studentForm" type="demo.StudentForm"></form-bean>
	</form-beans>
	<action-mappings>
		<action path="/login" type="demo.LoginAction" name="studentForm">			
		</action>
	</action-mappings>
</struts-config>

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>action</servlet-name>
	<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
	<init-param>
		<param-name>config</param-name>
		<param-value>/WEB-INF/struts-config.xml</param-value>
	</init-param>
	<load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>action</servlet-name>
	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="<%= request.getContextPath() %>/login.do" method="post">
    name:<input type="text" name="sname" /><br />
    major:<input type="text" name="major" /><br />
    birth:<input type="text" name="birth" /><br />
    score:<input type="text" name="score" /><br />
    <input type="submit" value="login" />
    </form>
  </body>
</html>


动态formbean

在<form-bean>标签下添加了<form-property>标签

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foudation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
	<form-beans>
		<form-bean name="studentForm" type="org.apache.struts.action.DynaActionForm">
			<form-property name="sname" type="java.lang.String"></form-property>
			<form-property name="major" type="java.lang.String"></form-property>
			<form-property name="birth" type="java.sql.Date"></form-property>
			<form-property name="score" type="java.lang.Float"></form-property>
		</form-bean>
	</form-beans>
	<action-mappings>
		<action path="/studentAction" type="demo.StudentAction" name="studentForm">
		</action>
	</action-mappings>
</struts-config>
web.xml没有变化,同上

package demo;

import java.io.PrintWriter;
import java.sql.Date;

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;

public class StudentAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		DynaActionForm studentForm=(DynaActionForm)form;
		String sname=(String)studentForm.get("sname");
		String major=(String)studentForm.get("major");
		Date birth=(Date)studentForm.get("birth");
		float score=(Float) studentForm.get("score");
		PrintWriter out=response.getWriter();
		out.print(sname+"\t"+major+"\t"+birth+"\t"+score);
		return null;
	}

}
StudentForm不再需要,已经在struts-config.xml进行了配置




你可能感兴趣的:(动态Formbean)