1、Struts2开发总结Struts2项目开发总结(注:Struts2版本:Struts2.1.6,数据库:Oracle9i)所须架包:commons-logging-1.0.4.jar、commons-fileupload-1.2.1.jar 、freemarker-2.3.13.jarognl-2.6.11.jar、struts2-core-2.1.6.jar 、xwork-2.1.2.jar1、 若在前台页面使用orgl表达式:必须定义: 使用:$requestScope.count来获取session中的值2、在jsp页面中显示action中的数据,不需要点击链接到action再返回页面
2、中显示数据第一步,写一个action类第二步:配置struts.xml文件,如: /sort1.jsp /error.jsp 第三步:在sort1.jsp中遍历action方法为sort5list的结果集: a href=sortnlist.action?ct_id= 第四步:在index.jsp中调用action为sort5list的方法3、 Struts2超链接传参方式:第一种:a href=newdetail2.action?nid=第二种:Newsquerynew最新文章第三种:直接使用即可 注意:Struts2超链接标签中的href属性是必选参数,否则在项目整合中可能会出现不明的标签
3、异常。4、 使用Struts2标签循环遍历结果集:a href=newdetail2.action?nid=其中内嵌html语言,除了value属性外,还有可选属性id= status=status,value属性是必选的5、 格式化日期显示方法或者,只需要改变即可必须定义:Body体中引用:6、 Struts2下拉菜单的使用属性注解:name是给Struts2标签取名字,这个也是提交到action并保存到对应的数据库中的字段名所必须得参数;list是下拉选项数值;listKey是选项列表所对应的id,listValue是选项列表每个字段的名称;theme是自定义下拉框格式,headerkey
4、是首选项的主键id,headerValue是首选项的字段名7、 Struts2.1.6使用日期控件:需要导入包:struts2-dojo-plugin-2.1.6.jar在head体中加入:在body体中引用:属性注解:name是给标签取名,type为date日期类型,displayFormat格式化显示日期,value是获取时间值,value=%today是获取当前系统时间8、Struts2中form提交表单格式在form中加入theme=simple属性,就不会受Struts2定义格式的影响9、使用属性驱动取值在action中将要输出的属性生成get和set方法,并将返回值定义为返回对象,
5、如:private String ntitle;/标题private String ncontent;/内容private String ntime;/时间private String username;/发布人News news=ns.queryNnewsdetailsql2(nid);/返回一个对象public String getNtitle() return ntitle; public void setNtitle(String ntitle) this.ntitle = ntitle; public String getNcontent() return ncontent; pub
6、lic void setNcontent(String ncontent) this.ncontent = ncontent; public String getNtime() return ntime; public void setNtime(String ntime) this.ntime = ntime; public String getUsername() return username; public void setUsername(String username) this.username = username; 由action直接跳转到要显示这些属性详细内容的页面在jsp
7、中直接使用标签取出即可使用模型驱动:查改新建action查询文章类型:import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.txxw.cms.news.model.Ctype;imp
8、ort com.txxw.cms.news.model.CtypeService;/* * author fejd E-mail:lzd_niit_java * version 创建时间:May 22, 2009 1:43:21 PM * 功能实现:文章类型修改内容查询 */public class CtypeeditAction extends ActionSupport implements ModelDriven private Ctype ctype=new Ctype();/将封装数据的ctype实现get和set方法 Override public String execute()
9、 throws Exception / TODO Auto-generated method stub HttpServletRequest request=ServletActionContext.getRequest(); int ct_id=Integer.parseInt(request.getParameter(ct_id).replace(, );/通过选择复选框来实现单个文章类型修改 CtypeService cs=new CtypeService(); Ctype ctype=cs.editctypesql(ct_id); this.setCtype(ctype); retur
10、n success; public Object getModel() / TODO Auto-generated method stub return null; public Ctype getCtype() return ctype; public void setCtype(Ctype ctype) this.ctype = ctype; Editctypesql()方法如下:public Ctype editctypesql(int ct_id) throws IOException dbcon=new DBConnection(); con=dbcon.getCon(); Ctyp
11、e cty=null; List ctylist=new ArrayList(); try psta=con.prepareStatement(this.getEditCtypesql(ct_id); psta.setInt(1, ct_id); rs=psta.executeQuery(); while(rs.next() cty=new Ctype(); cty.setCt_id(rs.getInt(ct_id); cty.setCt_name(rs.getString(ct_name); cty.setCt_sid(rs.getString(ct_sid);/数据类型Clob转换Stri
12、ng类型START/ try oracle.sql.CLOB clob=(oracle.sql.CLOB)rs.getClob(ct_introduction);/数据库中存文本的CLOB型字段名 String ct_introduction=clob.getSubString(long)1,(int)clob.length();/subString是截取字符串(从1截到length) if(clob=null|ct_introduction=null|ct_introduction=) return null; cty.setCt_introduction(ct_introduction);
13、 System.out.println(ct_introduction); /try catch(Exception e) logger.debug(数据类型Clob转换String类型出现异常); logger.info(数据类型Clob转换String类型出现异常); e.printStackTrace(); /数据类型Clob转换String类型END/ cty.setCt_image(rs.getString(ct_image); ctylist.add(cty); catch (SQLException e) / TODO Auto-generated catch block e.p
14、rintStackTrace(); return cty; public String getEditCtypesql(int ct_id) this.editCtypesql=select *from system.ctype where ct_id=?; return editCtypesql; public void setEditCtypesql(String editCtypesql) this.editCtypesql = editCtypesql; 注明:上述方法中涉及到Oracle数据类型Clob转换成String类型问题在jsp中使用即可取出ctype中的数据。10、Stru
15、ts2+ajax实现批量删除,批量更新操作全选: checkAll()不选: checkAllNo()反选: swichAll()批量删除: deletenews()批量更新:display1news() function checkAll() var obj=document.getElementsByName(nid); for(i=0;iobj.length;i+) obji.checked=true; function checkAllNo() var obj=document.getElementsByName(nid); for(i=0;iobj.length;i+) obji.c
16、hecked=false; function switchAll() var obj=document.getElementsByName(nid); for(i=0;iobj.length;i+) obji.checked =! obji.checked; function vals(input1,input2) var temp=; var objForm = document.formsinput1; var objLen = objForm.length; for (var iCount = 0; iCount objLen; iCount+) if (objForm.elements
17、iCount.type = checkbox) if(objForm.elementsiCount.checked=true&objForm.elementsiCount.nid!=allSelected&objForm.elementsiCount.nid!=allConcled) temp=temp+objForm.elementsiCount.value+,; / objForm.elementsiCount.checked = true; return temp; function deletenews() var nid=vals(form1,this) if(nid=) alert
18、(你没有选中内容); return false; else if(confirm(你确定要删除选中的内容吗+?+nid) var url=delete.action?nid=+nid; window.location.href=url; function display1news()var nid=vals(form1,this)if(nid=)alert(你没有选中内容);return false;elseif(confirm(你确定要显示选中的内容吗+?+nid)var url=display1news.action?nid=+nid;window.location.href=url;11
19、、控制Struts2 提交表单样式 function sub() document.form.submit(); 12、ajax前台验证(一部分) function check() if(document.getElementById(ct_name).value=) alert(请输入栏目名称!); return false; document.form.submit(); function check() if(document.getElementById(ntitle).value=) alert(请输入文章标题!); return false; if(document.getElem
20、entById(nprovider).value=) alert(请输入供稿人!); return false; if(document.getElementById(ntelephone).value=) alert(请输入联系电话!); return false; var partten1 = /13,5d9$/; var partten2 = /0(1,2d)|(3-9d2)d8$/; if(partten1.test(document.getElementById(ntelephone).value)=false&partten2.test(document.getElementByI
21、d(ntelephone).value)=false) alert(请检查您输入的联系电话格式); return false; if(document.getElementById(email).value=) alert(请输入Email!); return false; var zz=/w+(-w+)|(.w+)*A-Za-z0-9+(.|-)A-Za-z0-9+)*.A-Za-z0-9+$/; if(zz.test(document.getElementById(email).value)=false) alert(请输入正确格式的Email.); return false; document.form.submit(); function check() if(document.getElementById(nt).value=) alert(请输入查询关键字); return false; document.getElementById(form2).submit(); 按标题:s:hidden name=
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1