07-06
25
<html:multibox>实验
作者:Java伴侣 日期:2007-06-25
<html:multibox>要比<html:checkbox>更灵活。它会把用户界面上面选择的复选框中的信息封装在一个数组里面,数组存在于Form中,并通过用户提交在Action中取得。
如下例所示:
JSP页面:
Form.java
Actin.java
另外,这里因为传送的是中文,存在乱码问题,需要用request.setCharacterEncoding("GBK");来过虑下。
如下例所示:
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>
<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) {
}
}
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");
}
}
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");来过虑下。
评论: 0 | 引用: 0 | 查看次数: 848
发表评论