通常我都用以前旧有的方式写!
struts里好多标签使用起来都不方便
或许是我水平有限
struts里好多标签使用起来都不方便
或许是我水平有限
解决方案 »
- 就没有大侠帮帮忙嘛?
- 紧急!maven的eclipse找不到本地仓库的的jar包
- myeclipse中安装spket提示JQuery
- 尚学堂马士兵ooa&ood第4-10集
- org.hibernate.id.IdentifierGenerationException: attempted to assign id from null
- 直接赋值和new String创建的字符串有啥区别
- 空间和域名
- 程序出现如此错误....
- 我想用Java,但是不知道从什么地方下手,应该看什么书啊?应该如何做才好呢?请大家给我一点建议好吗?
- JBOSS启动报错
- 怎样实现从服务器向所有与它连接的客户端发通知?
- 导数据--高分求教
<INPUT TYPE="checkbox" NAME="name" value="0">
<INPUT TYPE="checkbox" NAME="name" value="1">
<INPUT TYPE="checkbox" NAME="name" value="2">
在ActionForm中,用int[] name(当然也可能是String[]、long[]),和相应的get、set方法,这样就能取到所有选中的值,然后遍历处理。
对于Struts的checkbox标签,确实有局限,我已经重写了checkbox的标记库,以适应自己的需要。
<html:checkbox property="check" value="1"/>
<html:checkbox property="check" value="2"/>
<html:checkbox property="check" value="3"/>Action Form(Dynamic Form):
<form-property name="check" type="java.lang.String[]"/>Session bean:
String[] x = (String[])map.get("check");是一个数组
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<head>
<title>view Info</title>
</head>
<body>
<p align="center">查看疑问信息</p>
<table width="600" border="1" align="center" cellpadding="2" cellspacing="0">
<tr align="center">
<td colspan="2"><bean:write name="row" property="title"/></td>
</tr>
<tr>
<td>网友:<bean:write name="row" property="nickname"/></td>
<td>添加时间:<bean:write name="row" property="addtime"/></td>
</tr>
<tr>
<td>问题类型:
<html:radio name="row" property="questiontype" value="1"><bean:message key="add.label.questiontype.option1"/></html:radio>
<html:radio name="row" property="questiontype" value="2"><bean:message key="add.label.questiontype.option2"/></html:radio>
<html:radio name="row" property="questiontype" value="3"><bean:message key="add.label.questiontype.option3"/></html:radio>
</td>
<td>回复类别:
<html:multibox name="row" property="replytype" value="1"/><bean:message key="add.label.replytype.option1"/>
<html:multibox name="row" property="replytype" value="2"/><bean:message key="add.label.replytype.option2"/>
</td>
</tr>
<tr>
<td width="88" valign="top"><bean:message key="add.label.city"/></td>
<td width="506">
<html:select name="row" property="city">
<html:option value="葫芦岛">葫芦岛1</html:option>
<html:option value="沈阳">沈阳1</html:option>
<html:option value="抚顺">抚顺1</html:option>
</html:select>
</td>
</tr>
<logic:notEmpty name="row" property="attach" >
<tr>
<td colspan="2"><bean:write name="row" property="attach" filter="false"/></td>
</tr>
</logic:notEmpty>
<tr>
<td colspan="2"><bean:write name="row" property="question"/></td>
</tr></table>
</body>
</html:html>
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import org.apache.struts.taglib.html.BaseHandlerTag;public class ViewAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
ResultSet rs = null;
DB db = null;
HashMap row = null;
String sql = "";
String tmp = "";
int id ; try {
id = Integer.parseInt(httpServletRequest.getParameter("id"));
}
catch(Exception e){
id = 0;
}
sql = "select title,nickname,addtime,replytype,questiontype,tele,email,attach,question,city from question where id =" + id;
db = new DB();
db.getConnect();
try {
rs = db.query(sql);
row = new HashMap(1);
if (rs.next()) {
row.put("title", rs.getString("title"));
row.put("nickname", rs.getString("nickname"));
row.put("tele", rs.getString("tele"));
row.put("email", rs.getString("email"));
row.put("questiontype", rs.getString("questiontype"));
row.put("replytype", rs.getString("replytype").split(","));
row.put("addtime", rs.getString("addtime"));
row.put("question", rs.getString("question"));
row.put("city", rs.getString("city")); if (rs.getString("attach").equals("")) {
row.put("attach","");
}
else {
row.put("attach",
"<a href='./upload/" + rs.getString("attach") +
"' target='_blank'>View Attach</a>");
}
} }
catch(Exception e) {
System.out.println("Fetch Info errors:" + e);
}
finally{
try{
rs.close();
db.close();
}
catch(Exception e) {
System.out.println("Close Connection Error:" + e);
}
}
httpServletRequest.setAttribute("row", row);
return actionMapping.findForward("view"); }
}
<bean:write name="document" property="documentId"/>
</html:multibox>然后你的bean属性应该是数组形式 like thisInteger ids[];{set ... get...}
1. 定义Tile模板. tiles-defs.xml:
<definition controllerClass="mytest.MasterDataControler" name="list" path="/control/checkboxlist.jsp"/>
2. 定义MasterDataControler.java:
package mytest;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import org.apache.struts.tiles.ComponentContext;
import javax.servlet.ServletContext;
public class MasterDataControler implements org.apache.struts.tiles.Controller{
public void perform(ComponentContext tileContext, HttpServletRequest request,
HttpServletResponse response,
ServletContext servletContext) {
request.setAttribute("master",new MasterDataFormBean()); //构造缓存主数据,或者根据用户参数动态构造.
}
}
3. MasterDataFormBean.java:
public class MasterDataFormBean {
public String[] getList(){
String[] list = new String[]{"a","b","c"};
return list;
}
}
4. checkboxlist.jsp:
<tiles:useAttribute name="paramName"/>
<logic:iterate id="item" name="master" property="list">
<html:multibox property='<%=(String)paramName%>'><bean:write name="item"/></html:multibox>
<bean:write name="item"/>
</logic:iterate>5. 在网页中调用控件:
<tiles:insert definition="list" flush="false">
<tiles:put name="paramName" value="list"/> 这里每个jsp页面控件的名称可能会不同.
</tiles:insert>
<html:options collection="assessyear" property="value" labelProperty="label" />
</html:select>
<html:options collection="BBB" property="code" labelProperty="label" />
</html:select>
《Struts单选框的实现》
《Struts下拉框的实现》