// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package demo;

import java.io.IOException;

import javax.servlet.ServletException;
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;

// add data action
public class AddAction extends Action {
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws IOException, ServletException {
        // get form data
        DynaActionForm daf = (DynaActionForm)form;
        int f1 = Integer.parseInt((String)daf.get("f1"));
        String f2 = (String)daf.get("f2");
        String f3 = (String)daf.get("f3");
        // store in database
        DB.saveOne(new T1(f1, f2, f3));
        // get data and store in request
        request.setAttribute("data", DB.getAll());;
        // get options and store in request
        request.setAttribute("options", Util.getOptions());;
        // forward to configured add.ok action
        return mapping.findForward("add.ok");
    }
}
