部分代码删了,黄色背景的为主题关键测试,经过测试,通过找资料忙了一下午,终于将其弄成功
struts2通过checkbox 删除多条记录(解释就不说了),看不懂的私聊
jsp页面
<form name="delForm" id="delForm" method="post" action="<%=basePath%>delSelectNews">
<table id="mainpage" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table id="subtree1" style="DISPLAY: " width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="20"><span class="newfont07">选择:<a href="#" class="right-font08" onclick="selectAll();">全选</a>-<a href="#" class="right-font08" onclick="unselectAll();">反选</a></span>
<input name="delselect" type="submit" class="right-button08" value="删除所选新闻" onclick="deleSelect()"/>
<input name="Submit2" type="button" class="right-button08" value="新建新闻" onclick="link();"/></td>
</tr>
<tr>
<td height="40" class="font42"><table width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#464646" class="newfont03">
<tr>
<td height="20" colspan="14" align="center" bgcolor="#EEEEEE"class="tablestyle_title"> 新闻列表 </td>
</tr>
<tr>
<td width="5%" align="center" bgcolor="#EEEEEE">选择</td>
<td width="15%" height="20" align="center" bgcolor="#EEEEEE">新闻标题</td>
<td width="10%" align="center" bgcolor="#EEEEEE">发布人</td>
<td width="15%" align="center" bgcolor="#EEEEEE">发布时间</td>
<td width="10%" align="center" bgcolor="#EEEEEE">新闻是否置顶</td>
<td width="10%" align="center" bgcolor="#EEEEEE">新闻是否已发布</td>
<td width="26%" align="center" bgcolor="#EEEEEE">操作</td>
</tr>
<s:iterator value="InfoList" var="c">
<tr align="center"><td bgcolor="#FFFFFF">
<input type="checkbox" name="delid" value="${infoId}" /></td>
<td height="20" bgcolor="#FFFFFF"><s:property value="#c.infoTitle"/></td>
<td bgcolor="#FFFFFF"><s:property value="#c.userPuber"/></td>
<td bgcolor="#FFFFFF"><s:property value="#c.infoTime"/></td>
<td bgcolor="#FFFFFF">
<s:if test="#c.isTop==1">置顶</s:if>
<s:if test="#c.isTop==0">否</s:if>
</td>
<td bgcolor="#FFFFFF">
<s:if test="#c.infoIsshow==1">已经发布</s:if>
<s:if test="#c.infoIsshow==0">存草稿</s:if>
</td>
<td bgcolor="#FFFFFF"><a href="delOneNews?infoId=${infoId}" onclick="isdel()">删除</a>|<a href="showOneNews?infoId=${infoId}">编辑</a></td>
</tr>
</s:iterator>
</table></td>
</table>
</form>
js代码
function deleSelect()
{
var delValue="";
var flag=false;
var de= document.getElementsByName("delid");
for(i=0;i<de.length;i++)
{
if(de[i].checked){
delValue+=","+de[i].value;
flag = true;
}
}
if(flag==false)
{
alert("至少你要选择一个待删除记录");
return false;
}
}
strust.xml配置
..............
<!-- 删除所选新闻 -->
<action name="delSelectNews" class="cn.swust.action.InfoPublishAction" method="delSelectNews">
<result type="redirect" name="success">showNews</result>
</action>
...................
Action处理
package cn.swust.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import cn.swust.bean.InfoPublishBean;
import cn.swust.service.InfoPublishService;
import com.opensymphony.xwork2.ActionSupport;
public class InfoPublishAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private InfoPublishService InfoService = new InfoPublishService();
private InfoPublishBean Info = new InfoPublishBean();
private int infoId;//新闻id;参数传递用
private List<InfoPublishBean> InfoList;
private String startTime;//用于按时间段查询
private String endTime;
private long[]delid=new long[]{};
public String execute(){
return "error";
}
/**
* 增加新闻
* @return
*/
public String addNews()
{
System.out.println("内容长度"+Info.getInfoContent().length());
Info.setInfoIsshow(1);
InfoService.addNews(Info);
return SUCCESS;
}
/**
* 显示所有新闻列表
* @return
*/
public String showAllNews()
{
InfoList = InfoService.queryNews();
System.out.println("新闻数量"+InfoList.size());
if(InfoList.size()>0)
return SUCCESS;
else
return "error";
}
/**
* 根据时间段查询
* @return
*/
public String showNewsByTime()
{
HttpServletRequest request = ServletActionContext.getRequest();
startTime=request.getParameter("startTime");
endTime = request.getParameter("endTime");
System.out.println("开始时间 "+startTime+" 结束时间 "+endTime);
InfoList = InfoService.queryNewsByTime(startTime, endTime);
System.out.println("新闻数量"+InfoList.size());
if(InfoList.size()>0)
return SUCCESS;
else
return "error";
}
/**
* 删除一条新闻
* @return
*/
public String delOneNews()
{
System.out.println(infoId);
InfoService.delNews(infoId);
return SUCCESS;
}
/**
* 删除所选新闻
* @return
*/
public String delSelectNews()
{
System.out.println("所选新闻");
for(int i=0;i<this.delid.length;i++)
InfoService.delNews((int)delid[i]);
return SUCCESS;
}
/**
* 显示一条新闻列表
* @return
*/
public String showOneNews()
{
Info = InfoService.queryOneNews(infoId);
return SUCCESS;
}
/**
* 修改一条新闻
* @return
*/
public String updateNews()
{
Info.setInfoId(infoId);
Info.setInfoIsshow(1);
InfoService.updateNews(Info);
return SUCCESS;
}
public InfoPublishService getInfoService() {
return InfoService;
}
public void setInfoService(InfoPublishService infoService) {
InfoService = infoService;
}
public InfoPublishBean getInfo() {
return Info;
}
public void setInfo(InfoPublishBean info) {
Info = info;
}
public List<InfoPublishBean> getInfoList() {
return InfoList;
}
public void setInfoList(List<InfoPublishBean> infoList) {
InfoList = infoList;
}
public int getInfoId() {
return infoId;
}
public void setInfoId(int infoId) {
this.infoId = infoId;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public long[] getDelid() {
return delid;
}
public void setDelid(long[] delid) {
this.delid = delid;
}
}