07-06
25

<html:multibox>实验

<html:multibox>要比<html:checkbox>更灵活。它会把用户界面上面选择的复选框中的信息封装在一个数组里面,数组存在于Form中,并通过用户提交在Action中取得。
如下例所示:
JSP页面:
<body>
<html:messages id="message" message="true">
<bean:write name="message"/>
</html:messages>
<html:form action="box.do" method="POST">
<html:text property="name">
</html:text>
<br />
音乐:<html:multibox property="strArray" value="音乐"/>
<br>
电影:<html:multibox property="strArray" value="电影"/>
<br>
读书:<html:multibox property="strArray" value="读书"/>
<br>
<html:submit property="submit" value="Submit"/><br>
<html:reset value ="Reset"/>
</html:form>
</body>


Form.java
package struttag;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.*;

public class BoxForm extends ActionForm {
    private String[] strArray; //定义一个数组,数组长度待定
    private String name;
    public String[] getStrArray() {
        return strArray;
    }

    public String getName() {
        return name;
    }

    public void setStrArray(String[] strArray) {
        this.strArray = strArray;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ActionErrors validate(ActionMapping actionMapping,
                                 HttpServletRequest httpServletRequest) {
            /** @todo: finish this method, this is just the skeleton.*/
        return null;
    }

    public void reset(ActionMapping actionMapping,
                      HttpServletRequest servletRequest) {
    }
}


Actin.java
package struttag;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import java.io.UnsupportedEncodingException;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;

public class BoxAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws
            UnsupportedEncodingException {
        BoxForm boxForm = (BoxForm) form;
        String[] box=boxForm.getStrArray();
        if(box==null||box.equals("null")||box.length==0)
        {
            return new ActionForward("/Multibox.jsp");

        }
        System.out.println("box:"+box.toString());
        System.out.println("box[box.length]:"+box[box.length-1]);//因为multibox封装数组的长度比本身多出一位
        System.out.println("name:"+boxForm.getName());

        ActionMessages msg=new ActionMessages();
        msg.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("return.success"));
        this.saveMessages(request,msg);
        return new ActionForward("/Multibox.jsp");
    }
}


另外,这里因为传送的是中文,存在乱码问题,需要用request.setCharacterEncoding("GBK");来过虑下。

文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 846
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭