SSI框架(Struts2+Spring+iBATIS)配置简单的添加,删除,显示。
1.Struts代码
<package name="portalInfoFlash" namespace="/pana/portal/IndexFlash" extends="struts-default"> <action name="Flash_*" class="iFlashAction" method="{1}"> <result name="view">/WEB-INF/pages/pana/portal/info/iFlashpage/iFlashShow.jsp</result> <result name="edit">/WEB-INF/pages/pana/portal/info/iFlashpage/iFlashEdit.jsp</result> <result name="add">/WEB-INF/pages/pana/portal/info/iFlashpage/iFlashAdd.jsp</result> <result name="index">/WEB-INF/pages/pana/system/indexFlash.jsp</result> <result name="ple">/WEB-INF/pages/pana/system/iFlashSimpleShow.jsp</result> <result name="upview">/WEB-INF/pages/pana/portal/info/iFlashpage/UploadFlashImage.jsp</result> <result name="upflash">/WEB-INF/pages/pana/portal/info/iFlashpage/UploadFlashImage.jsp</result> <result name="editupview">/WEB-INF/pages/pana/portal/info/iFlashpage/UploadEditFlashImage.jsp</result> <result name="upeditflash">/WEB-INF/pages/pana/portal/info/iFlashpage/UploadEditFlashImage.jsp</result> </action> </package>
显示画面Flash_ShowIndex.do
action代码
private List<Infoflash> iflist; public List<Infoflash> getIflist() { return iflist; } public void setIflist(List<Infoflash> iflist) { this.iflist = iflist; } public String ShowIndex() { iflist = iFlashService.getInfoThreeFourList(); return "index"; }
InfoFlashServiceImpl代码实现
public List<Infoflash> getInfoThreeFourList() { InfoflashExample infofla = new InfoflashExample(); try { return iFlashDAO.SelectTopThree(infofla); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
InfoflashDAOImpl代码实现
@SuppressWarnings("unchecked") public List<Infoflash> SelectTopThree(InfoflashExample example) throws SQLException { List<Infoflash> list = sqlMapClient.queryForList("InfoFlash.ibatorgenerated_selectByThree", example); return list; }
InfoFlash_SqlMap.xml代码实现
<select id="ibatorgenerated_selectByThree" resultMap="ibatorgenerated_BaseResultMap"
parameterClass="com.pana.portal.info.model.InfoflashExample" > select top 3 ID, Name, Img, Url, InputDate, ExpiredDate, ShowOrder, [Desc] from InfoFlash order by ShowOrder desc </select>
jsp页面显示
<div id="play"> <ul> <li id="playBg"></li> <li id="playText"></li> <li id="playNum"><a>1</a><a>2</a><a>3</a></li> <li id="playShow"> <s:iterator value="iflist"> <a href='javascript:void(0);'
onclick="parent.parent.add({id:'OtherInfoView',name:'查看信息',title:'查看信息',url:'<s:property value="url"/>'});"> <img src="http://www.cnblogs.com/../FileUpload/attachments/flasImage/<s:property value="img"/>" alt="<s:property value="name"/>"></a> </s:iterator> </li> <br/> </ul> </div>
添加画面
Flash_Add.do
action代码
public String Add()
{
return "add";
}
添加画面比较简单
jsp画面
View Code
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Flash 信息添加</title> <link type="text/css" href="http://www.cnblogs.com/../css/combo/css/main.css" rel="stylesheet" /> <link href="http://www.cnblogs.com/../css/common.css" rel="stylesheet" type="text/css" /> <link href="http://www.cnblogs.com/../css/basic.css" rel="stylesheet" type="text/css" /> <link href="http://www.cnblogs.com/../css/inchcms_style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="http://www.cnblogs.com/../js/combo/js/jquery-ui-1.8.2.custom/js/jquery-1.4.2.min.js"></script> <script type="text/javascript"> var findFrameWindow = function(contextWindow, frameId){ var frames = contextWindow.frames; for(i = 0, count = frames.length; i < count; i++) { if(frames[i].name == frameId) return frames[i]; } return null; } </script> <script type="text/javascript"> $(function(){ $("#save").click(function(){ var nameVal = $.trim($("#name").val()); if(nameVal=="") { alert("【信息名称】输入不能为空"); return; } var urlVal = $.trim($("#url").val()); if(urlVal=="") { alert("【链接路径】输入不能为空"); return; } var showorderVal = $.trim($("#showorder").val()); var testCheck=/^\d+$/; if(!testCheck.test(showorderVal)) { alert("【排序条件】必须为整数"); return; } // $("#addForm").submit(); var url = "Flash_AddFlash.do"; var params = $("#addForm").serialize(); $.post( url, params, function(data) { var infochViewWindow = findFrameWindow(parent, "FrameFlashInfoView"); if(infochViewWindow!= null) { infochViewWindow.location.reload(); } parent.tab.close(window.name.replace("Frame","")); } ); }); }); </script> </head> <body style="margin-left:20px; margin-top:20px;"> <form id="addForm" method="post" > <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tb_list"> <tr> <th width="10%"><b>添加Flash信息</b></th> <th width="90%"></th> </tr> <tr> <td align="right"> <label for="nameInput"> <span style="color: red">*</span>信息名称: </label> </td> <td align="left"> <input id="name" style="width: 35%" type="text" name="name" /> </td> </tr> <tr> <td align="right"> <span style="color: red">*</span>上传图片: </td> <td align="left"> <iframe src='Flash_Upload.do' scrolling="no" frameborder="0" width="100%"></iframe> </td> </tr> <tr> <td align="right"> <label for="nameInput"> <span style="color: red">*</span>链接路径: </label> </td> <td align="left"> <input id="url" style="width: 35%" type="text" name="url" /> </td> </tr> <tr> <td align="right"> <label for="nameInput"> <span style="color: red">*</span>排序条件: </label> </td> <td align="left"> <input id="showorder" style="width: 15%" type="text" name="showorder" /> </td> </tr> <tr> <td align="right"> <label for="nameInput"> 描述信息: </label> </td> <td align="left"> <textarea id="desc" style="width: 35%" name="desc" cols="45" rows="3"><s:property value="flash.desc"/></textarea> </td> </tr> <tr> <td></td> <td><input id="save" type="button" class="input02" value="保存" /> <input type="button" class="input02" value="取消" onclick="javascript:parent.tab.close(window.name.replace('Frame',''));" /></td> </tr> </table> </form> </body> </html>
值得注意的是页面采取的表单提交(非字符串拼接)
var url = "Flash_AddFlash.do";
var params = $("#addForm").serialize();
$.post(
url,
params,
function(data)
{
}
);
action代码
public String AddFlash() throws Exception { DateFormat format=new SimpleDateFormat("yyyy-MM-dd"); String inputdate=format.format(new Date()); flash=new Infoflash(); flash.setName(name); flash.setImg(tempnewname); flash.setUrl(url); flash.setInputdate(format.parse(inputdate)); flash.setExpireddate(format.parse(inputdate)); flash.setShoworder(Integer.parseInt(showorder)); flash.setDesc(desc); iFlashService.addInfoflash(flash); tempnewname=null; return "add"; }
InfoFlashServiceImpl类方法
public String addInfoflash(Infoflash flash) { try { iFlashDAO.insertSelective(flash); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "success"; }
InfoflashDAOImpl类方法
/** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoFlash * * @ibatorgenerated Fri Apr 15 11:54:14 CST 2011 */ public void insertSelective(Infoflash record) throws SQLException { sqlMapClient.insert("InfoFlash.ibatorgenerated_insertSelective", record); }
InfoFlash_SqlMap.xml代码
View Code
<insert id="ibatorgenerated_insertSelective" parameterClass="com.pana.portal.info.model.Infoflash" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Fri Apr 15 11:54:14 CST 2011. --> insert into InfoFlash <dynamic prepend="(" > <isNotNull prepend="," property="id" > ID </isNotNull> <isNotNull prepend="," property="name" > Name </isNotNull> <isNotNull prepend="," property="img" > Img </isNotNull> <isNotNull prepend="," property="url" > Url </isNotNull> <isNotNull prepend="," property="inputdate" > InputDate </isNotNull> <isNotNull prepend="," property="expireddate" > ExpiredDate </isNotNull> <isNotNull prepend="," property="showorder" > ShowOrder </isNotNull> <isNotNull prepend="," property="desc" > [Desc] </isNotNull> ) </dynamic> values <dynamic prepend="(" > <isNotNull prepend="," property="id" > #id:INTEGER# </isNotNull> <isNotNull prepend="," property="name" > #name:VARCHAR# </isNotNull> <isNotNull prepend="," property="img" > #img:VARCHAR# </isNotNull> <isNotNull prepend="," property="url" > #url:VARCHAR# </isNotNull> <isNotNull prepend="," property="inputdate" > #inputdate:TIMESTAMP# </isNotNull> <isNotNull prepend="," property="expireddate" > #expireddate:TIMESTAMP# </isNotNull> <isNotNull prepend="," property="showorder" > #showorder:INTEGER# </isNotNull> <isNotNull prepend="," property="desc" > #desc:VARCHAR# </isNotNull> ) </dynamic> </insert>
删除画面
jsp页面代码
<a href='Flash_Del.do?id=<s:property value="id"/>'> <img title='删除' src="http://www.cnblogs.com/../CMSImages/cross.png" width="16" height="16" onclick="javascript:return confirm('确定删除吗');" /> </a>
action代码
public String Del() { iFlashService.DeleteInfoFlash(id); if(name==null) { iflist = iFlashService.getInfoFlashList(); }else { iflist=iFlashService.getLikeInfoFlash(name); } sName = ""; for(Infoflash iFlash : iflist){ sName += iFlash.getName(); } return "view"; }
InfoFlashServiceImpl类实现
public void DeleteInfoFlash(int id) { try { iFlashDAO.deleteByPrimaryKey(id); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
InfoflashDAOImpl类方法,InfoFlash_SqlMap.xml代码 就不再说了
编辑画面
jsp页面
View Code
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Flash 信息编辑</title> <link type="text/css" href="http://www.cnblogs.com/../css/combo/css/main.css" rel="stylesheet" /> <link href="http://www.cnblogs.com/../css/common.css" rel="stylesheet" type="text/css" /> <link href="http://www.cnblogs.com/../css/basic.css" rel="stylesheet" type="text/css" /> <link href="http://www.cnblogs.com/../css/inchcms_style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="http://www.cnblogs.com/../js/combo/js/jquery-ui-1.8.2.custom/js/jquery-1.4.2.min.js"></script> <script type="text/javascript"> var findFrameWindow = function(contextWindow, frameId){ var frames = contextWindow.frames; for(i = 0, count = frames.length; i < count; i++) { if(frames[i].name == frameId) return frames[i]; } return null; } </script> <script type="text/javascript"> $(function(){ $("#save").click(function(){ var nameVal = $.trim($("#name").val()); if(nameVal=="") { alert("【信息名称】输入不能为空"); return; } var urlVal = $.trim($("#url").val()); if(urlVal=="") { alert("【链接路径】输入不能为空"); return; } var showorderVal = $.trim($("#showorder").val()); var testCheck=/^\d+$/; if(!testCheck.test(showorderVal)) { alert("【排序条件】必须为整数"); return; } var url = "Flash_EditFlash.do"; var params = $("#editform").serialize(); $.post( url, params, function(data) { var infochViewWindow = findFrameWindow(parent, "FrameFlashInfoView"); if(infochViewWindow!= null) { infochViewWindow.location.reload(); } parent.tab.close(window.name.replace("Frame","")); } ); }); }); </script> </head> <body style="margin-left:20px; margin-top:20px;"> <form id="editform" method="post" > <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tb_list"> <tr> <th width="10%"><b>编辑 Flash信息</b></th> <th width="90%"></th> </tr> <tr> <td align="right" > <label for="nameInput"> <span style="color: red">*</span>信息名称: </label> </td> <td align="left"> <input id="id" name="id" type="hidden" value="<s:property value="flash.id"/>"/> <input id="name" style="width: 35%" type="text" name="name" value="<s:property value="flash.name"/>" /> </td> </tr> <tr> <td align="right"> <span style="color: red">*</span>上传图片: </td> <td align="left"> <iframe src='Flash_EditUpload.do?img=<s:property value="flash.img"/>' scrolling="no" frameborder="0" width="100%"></iframe> </td> </tr> <tr> <td align="right"> <label for="nameInput"> <span style="color: red">*</span>链接路径: </label> </td> <td align="left"> <input id="url" style="width: 35%" type="text" name="url" value="<s:property value="flash.url"/>" /> </td> </tr> <tr> <td align="right"> <label for="nameInput"> <span style="color: red">*</span>排序条件: </label> </td> <td align="left"> <input id="showorder" style="width: 10%" type="text" name="showorder" value="<s:property value="flash.showorder"/>"/> </td> </tr> <tr> <td align="right"> <label for="nameInput"> 描述信息: </label> </td> <td align="left"> <textarea id="desc" style="width: 35%" name="desc" cols="45" rows="3"><s:property value="flash.desc"/></textarea> </td> </tr> <tr> <td></td> <td><input width="160" id="save" class="input02" type="button" value="保存"/> <input type="button" class="input02" value="取消" /></td> </tr> </table> </form> </body> </html>
action代码
View Code
public String EditFlash() throws ParseException { if(tempeditname!=null) { DateFormat format=new SimpleDateFormat("yyyy-MM-dd"); flash=new Infoflash(); flash.setId(id); flash.setName(name); flash.setImg(tempeditname); flash.setUrl(url); //flash.setInputdate(format.parse(inputdate)); //flash.setExpireddate(format.parse(expireddate)); flash.setShoworder(Integer.parseInt(showorder)); flash.setDesc(desc); iFlashService.UpdateInfoFlash(flash); }else { DateFormat format=new SimpleDateFormat("yyyy-MM-dd"); flash=new Infoflash(); flash.setId(id); flash.setName(name); flash.setUrl(url); //flash.setInputdate(format.parse(inputdate)); //flash.setExpireddate(format.parse(expireddate)); flash.setShoworder(Integer.parseInt(showorder)); flash.setDesc(desc); iFlashService.UpdateInfoFlash(flash); } tempeditname=null; return "edit"; }
InfoFlashServiceImpl类实现
public void UpdateInfoFlash(Infoflash flash) { try { iFlashDAO.updateByPrimaryKeySelective(flash); //iFlashDAO.updateByPrimaryKey(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
InfoflashDAOImpl类方法就不说了,需要注意的是做Update的操作主键必须有值
InfoFlash_SqlMap.xml代码
<update id="ibatorgenerated_updateByPrimaryKeySelective" parameterClass="com.pana.portal.info.model.Infoflash" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Fri Apr 15 11:54:14 CST 2011. --> update InfoFlash <dynamic prepend="set" > <isNotNull prepend="," property="name" > Name = #name:VARCHAR# </isNotNull> <isNotNull prepend="," property="img" > Img = #img:VARCHAR# </isNotNull> <isNotNull prepend="," property="url" > Url = #url:VARCHAR# </isNotNull> <isNotNull prepend="," property="inputdate" > InputDate = #inputdate:TIMESTAMP# </isNotNull> <isNotNull prepend="," property="expireddate" > ExpiredDate = #expireddate:TIMESTAMP# </isNotNull> <isNotNull prepend="," property="showorder" > ShowOrder = #showorder:INTEGER# </isNotNull> <isNotNull prepend="," property="desc" > [Desc] = #desc:VARCHAR# </isNotNull> </dynamic> where ID = #id:INTEGER# </update>
适合初学者练练手。。。