struts复选框的处理

来源:互联网 发布:股票经典书籍 知乎 编辑:程序博客网 时间:2024/06/07 09:48
程序代码 程序代码


public class TestForm extends ActionForm {
    /*
     * Generated fields
     */

    /** sex property */
    private String sex;

    /** favorite property */
    private String favorite[];

    /** name property */
    private String name;


由于是复选,所以favorite设置成[]类型;


ACTION处理:

程序代码 程序代码

    public ActionForward saveTest(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        TestForm testForm = (TestForm) form;// TODO Auto-generated method stub
        System.out.println(testForm.getName());
        System.out.println(testForm.getSex());
        String favor[]=testForm.getFavorite();
        String result="";
        for(int i=0;i<favor.length;i++)
        {
            result=result+favor[i]+",";
        }
        result=result.substring(0, result.length()-1);
        System.out.println(result);
        WebApplicationContext appContext = this.getWebApplicationContext();

        TestDAO t = (TestDAO)appContext.getBean("TestDAO");
        Test te=new Test();
        te.setName(testForm.getName());
        te.setSex(testForm.getSex());
        te.setFavorite(result);
        t.save(te);
        return null;
    }
    
    public ActionForward findTest(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        TestForm testForm = (TestForm) form;// TODO Auto-generated method stub
        Test te=new Test();
        te.setName("男");
        te.setSex("女");
        te.setFavorite("天天,学习");
        String re[]=te.getFavorite().split(",");
                                testForm.setFavorite(re);
                                testForm.setName(te.getName());
                                testForm.setSex(te.getSex());

        return mapping.findForward("edit_test_Ok");
    }
}


各爱好中我们以,号相隔,并存入数据库,在编辑时取出来在还原成字符数组;
原创粉丝点击