index.jsp
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html>
<head>
<title>ActionForm Samples</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>ActionForm Sample</h1>
<ul>
<LI><html:link page="/mapFormInput.jsp">Map-Backed Form</html:link></LI>
</ul>
</body>
</html>
mapFormInput.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html>
<head>
<title>Map-backed Form</title>
</head>
<body>
<h2>Map Form Test</h2>
<html:form action="/mapForm">
Java Skill:
<html:select property="skill(java)">
<html:options property="skillLevels"/>
</html:select><br />
JSP Skill:
<html:select property="skill(jsp)">
<html:options property="skillLevels"/>
</html:select><br />
Struts Skill:
<html:select property="skill(strut)">
<html:options property="skillLevels"/>
</html:select><br />
<html:submit/>
</html:form>
</body>
</html>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="mapForm" type="cn.hxex.actionform.struts.form.MapForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="mapForm"
input="/mapFormInput.jsp"
name="mapForm"
path="/mapForm"
scope="request"
type="cn.hxex.actionform.struts.action.MapFormAction">
<forward name="display" path="/mapForm.jsp" />
</action>
</action-mappings>
<message-resources parameter="cn.hxex.actionform.struts.ApplicationResources" />
</struts-config>
MapFormAction .java
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl
package cn.hxex.actionform.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 cn.hxex.actionform.struts.form.MapForm;
/**
* MyEclipse Struts
* Creation date: 06-04-2006
*
* XDoclet definition:
* @struts.action path="/mapForm" name="mapForm" input="/mapFormInput.jsp" scope="request" validate="true"
* @struts.action-forward name="display" path="/mapForm.jsp"
*/
public class MapFormAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
MapForm mapForm = (MapForm) form;
return mapping.findForward("display");
}
}
MapForm .java
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl
package cn.hxex.actionform.struts.form;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 06-04-2006
*
* XDoclet definition:
* @struts.form name="mapForm"
*/
public class MapForm extends ActionForm {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
private static String[] skillLevels =
new String[] {"Beginner","Intermediate","Advanced"};
private Map skills = new HashMap( );
public Object getSkill(String key) {
return skills.get(key);
}
public void setSkill(String key, Object value) {
skills.put(key, value);
}
public Map getSkills( ) {
return skills;
}
public String[] getSkillLevels( ) {
return skillLevels;
}
}
mapForm.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Map-backed Form</title>
</head>
<body>
<h2>Map Form Test</h2>
<c:if test="${not empty mapForm.skills}">
Java Skill: <c:out value="${mapForm.skills.java}"/><br />
JSP Skill: <c:out value="${mapForm.skills.jsp}"/><br />
Struts Skill: <c:out value="${mapForm.skills.struts}"/><br />
</c:if>
</body>
</html>