// 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;

// 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
        AddForm addfrm = (AddForm)form;
        int f1 = Integer.parseInt(addfrm.getF1());
        String f2 = addfrm.getF2();
        String f3 = addfrm.getF3();
        // 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("opt", Util.getOptions());
        // forward to configured add.ok action
        return mapping.findForward("add.ok");
    }
}
