// 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 java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;  
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TestController {
    // options
    private List<String> getOptions() {
        List<String> res = new ArrayList<String>();
        res.add("");
        res.add(T1.VER);
        res.add(T1.NOTVER);
        return res;
    }
    @RequestMapping(value="/", method=RequestMethod.GET)
    public String get(Model m) {
        // get all data and save in request for use by JSP
        m.addAttribute("data", DB.getAll());
        // get options and save in request for JSP
        m.addAttribute("options", getOptions());
        // forward to test.jsp
        return "test";
    }
    @RequestMapping(value="/", method=RequestMethod.POST)
    public String post(@RequestParam("f1") String f1, @RequestParam("f2") String f2, @RequestParam("f3") String f3, Model m) {
        // store in database
        DB.saveOne(new T1(Integer.parseInt(f1), f2, f3));
        // get all data and save in request for use by JSP
        m.addAttribute("data", DB.getAll());
        // get options and save in request for JSP
        m.addAttribute("options", getOptions());
        // forward to test.jsp
        return "test";
    }
}
