Struts2开发总结_第1页
Struts2开发总结_第2页
Struts2开发总结_第3页
Struts2开发总结_第4页
Struts2开发总结_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Struts2项目开发总结(注:Struts2版本:Struts,数据库:Oracle9i)所须架包:commons-logging-.jar、commons-fileupload-.jar 、freemarker-.jarognl-.jar、struts2-core-.jar 、xwork-.jar若在前台页面使用orgl表达式:必须定义: 使用:$requestScope.count来获取session中的值2、在jsp页面中显示action中的数据,不需要点击链接到action再返回页面中显示数据第一步,写一个action类第二步:配置struts.xml文件,如:/sort1.jsp/

2、error.jsp第三步:在sort1.jsp中遍历action方法为sort5list的结果集: a href=sortnlist.action?ct_id= 第四步:在index.jsp中调用action为sort5list的方法Struts2超链接传参方式:第一种:a href=newdetail2.action?nid=第二种:Newsquerynew最新文章第三种:直接使用即可 注意:Struts2超链接标签中的href属性是必选参数,否则在项目整合中可能会出现不明的标签异常。使用Struts2标签循环遍历结果集:a href=newdetail2.action?nid=其中内嵌ht

3、ml语言,除了value属性外,还有可选属性id= status=status,value属性是必选的格式化日期显示方法或者,只需要改变即可必须定义:Body体中引用:Struts2下拉菜单的使用属性注解:name是给Struts2标签取名字,这个也是提交到action并保存到对应的数据库中的字段名所必须得参数;list是下拉选项数值;listKey是选项列表所对应的id,listValue是选项列表每个字段的名称;theme是自定义下拉框格式,headerkey是首选项的主键id,headerValue是首选项的字段名Struts使用日期控件:需要导入包:struts2-dojo-plugi

4、n-.jar在head体中加入:在body体中引用:属性注解:name是给标签取名,type为date日期类型,displayFormat格式化显示日期,value是获取时间值,value=%today是获取当前系统时间8、Struts2中form提交表单格式在form中加入theme=simple属性,就不会受Struts2定义格式的影响9、使用属性驱动取值在action中将要输出的属性生成get和set方法,并将返回值定义为返回对象,如:private String ntitle;/标题private String ncontent;/内容private String ntime;/时间p

5、rivate 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;public void setNcontent(String ncontent) this.ncontent = ncontent;public String getNt

6、ime() 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中直接使用标签取出即可使用模型驱动:查改新建action查询文章类型:import javax.servlet.http.HttpServletRequest;import

7、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;import com.txxw.cms.news.model.CtypeService;/* * author fejd E- * version 创建时间:May 22, 200

8、9 1:43:21 PM * 功能实现:文章类型修改内容查询 */public class CtypeeditAction extends ActionSupport implements ModelDriven private Ctype ctype=new Ctype();/将封装数据的ctype实现get和set方法Overridepublic String execute() throws Exception / TODO Auto-generated method stubHttpServletRequest request=ServletActionContext.getReque

9、st();int ct_id=Integer.parseInt(request.getParameter(ct_id).replace(, );/通过选择复选框来实现单个文章类型修改CtypeService cs=new CtypeService();Ctype ctype=cs.editctypesql(ct_id);this.setCtype(ctype);return success;public Object getModel() / TODO Auto-generated method stubreturn null;public Ctype getCtype() return ct

10、ype;public void setCtype(Ctype ctype) this.ctype = ctype;Editctypesql()方法如下:public Ctype editctypesql(int ct_id) throws IOExceptiondbcon=new DBConnection();con=dbcon.getCon();Ctype cty=null;List ctylist=new ArrayList();try psta=con.prepareStatement(this.getEditCtypesql(ct_id);psta.setInt(1, ct_id);r

11、s=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转换String类型START/try oracle.sql.CLOB clob=(oracle.sql.CLOB)rs.getClob(ct_introduction);/数据库中存文本的CLOB型字段名 String ct_introduction=clob.getS

12、ubString(long)1,(int)clob.length();/subString是截取字符串(从1截到length)if(clob=null|ct_introduction=null|ct_introduction=)return null;cty.setCt_introduction(ct_introduction);System.out.println(ct_introduction); /try catch(Exception e)logger.debug(数据类型Clob转换String类型出现异常);(数据类型Clob转换String类型出现异常);e.printStack

13、Trace(); /数据类型Clob转换String类型END/cty.setCt_image(rs.getString(ct_image);ctylist.add(cty); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return cty;public String getEditCtypesql(int ct_id) this.editCtypesql=select *from system.ctype where ct_id=?;return editCtypesql;publi

14、c void setEditCtypesql(String editCtypesql) this.editCtypesql = editCtypesql;注明:上述方法中涉及到Oracle数据类型Clob转换成String类型问题在jsp中使用即可取出ctype中的数据。10、Struts2+ajax实现批量删除,批量更新操作全选: checkAll()不选: checkAllNo()反选: swichAll()批量删除: deletenews()批量更新:display1news() function checkAll() var obj=document.getElementsByName

15、(nid); for(i=0;iobj.length;i+) obji.checked=true; function checkAllNo() var obj=document.getElementsByName(nid); for(i=0;iobj.length;i+) obji.checked=false; function switchAll() var obj=document.getElementsByName(nid); for(i=0;iobj.length;i+) obji.checked =! obji.checked; function vals(input1,input2

16、) var temp=; var objForm = document.formsinput1; var objLen = objForm.length; for (var iCount = 0; iCount objLen; iCount+) if (objForm.elementsiCount.type = checkbox) if(objForm.elementsiCount.checked=true&objForm.elementsiCount.nid!=allSelected&objForm.elementsiCount.nid!=allConcled) temp=temp+objF

17、orm.elementsiCount.value+,; / objForm.elementsiCount.checked = true; return temp; function deletenews() var nid=vals(form1,this) if(nid=) alert(你没有选中内容); return false; else if(confirm(你确定要删除选中的内容吗+?+nid) var url=delete.action?nid=+nid; window.location.href=url; function display1news()var nid=vals(fo

18、rm1,this)if(nid=)alert(你没有选中内容);return false;elseif(confirm(你确定要显示选中的内容吗+?+nid)var url=display1news.action?nid=+nid;window.location.href=url;11、控制Struts2 提交表单样式 function sub() document.form.submit(); 12、ajax前台验证(一部分) function check() if(document.getElementById(ct_name).value=) alert(请输入栏目名称!); retur

19、n false; document.form.submit(); function check() if(document.getElementById(ntitle).value=) alert(请输入文章标题!); return false; if(document.getElementById(nprovider).value=) alert(请输入供稿人!); return false; if(document.getElementById(ntelephone).value=) alert(请输入联系电话!); return false; var partten1 = /13,5d9

20、$/; var partten2 = /0(1,2d)|(3-9d2)d8$/; if(partten1.test(document.getElementById(ntelephone).value)=false&partten2.test(document.getElementById(ntelephone).value)=false) alert(请检查您输入的联系电话格式); return false; if(document.getElementById(email).value=) alert(请输入Email!); return false; var zz=/w+(-w+)|(.w

21、+)*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(); 按标题:查询删除13

22、、Struts2+Oracle9i数据库分页第一步:新建分页的公共接口:FenYeDaopublic interface FenYeDao public int count(int ct_id);/带有参数第二步:实现接口方法(从数据库中获取数据的总行数):FenYeServicepublic int count(int ct_id) int intRowCount = 0;/总行数 String sql= null ; ResultSet rs = null ; dbcon=new DBConnection(); con=dbcon.getCon(); sql = select nid,nt

23、itle,ntime, ntype from system.news a where a.ntype =(select ct_id from system.ctype c where c.ct_id=?)order by nid asc;/Sql语句可自定义 try psta=con.prepareStatement(sql); psta.setInt(1, ct_id); rs =psta.executeQuery(); while( rs.next() /游标指向第一行 intRowCount+=1;/取得总行数 catch(Exception e) System.out.println(

24、e) ; return intRowCount; 第三步:新建action类:SortNewsListActionimport java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensym

25、phony.xwork2.ModelDriven;import com.txxw.cms.news.model.News;import com.txxw.cms.news.model.NewsService;public class SortNewsListAction extends ActionSupport implements ModelDriven private int ct_id;/分页需要的参数private String ntype;/文章类型private List sortclist;/文章类型列表private List sortnlist;/文章所有列表private

26、 News news;/文章对象private int pageNow = 1 ; /初始化为1,默认从第一页开始显示 private int pageSize = 8 ; /每页显示8条记录 private int k;/储存最大页面数 private int i;/从第i条查询数据 private int intRowCount;/总行数 private int intPageCount;/总页数 private NewsService ns=new NewsService();Overridepublic String execute() throws Exception / TODO

27、Auto-generated method stubHttpServletRequest request=ServletActionContext.getRequest();if(request.getParameter(pageNow)!=null)trypageNow=Integer.parseInt(request.getParameter(pageNow);catch (Exception ex)pageNow=1;if(request.getParameter(ct_id)!=null)ct_id=Integer.parseInt(request.getParameter(ct_id

28、);sortclist=ns.sortclist(ct_id); /文章类型列表intRowCount=ns.count(ct_id); /获取匹配条件文章的总条数k = (intRowCount+pageSize-1) / pageSize;/计算出总页数/翻页控制START/if(pageNow = k)pageNow = k;if(pageNow=0)pageNow=1;/翻页控制END/i=pageNow*pageSizeSize;/每页要显示文章的条数sortnlist=ns.sortnlist(i, pageSize,ct_id);/获取文章列表return success;pub

29、lic Object getModel() / TODO Auto-generated method stubreturn null;public List getSortnlist() return sortnlist;public void setSortnlist(List sortnlist) this.sortnlist = sortnlist;public int getCt_id() return ct_id;public void setCt_id(int ct_id) this.ct_id = ct_id;public String getNtype() return nty

30、pe;public void setNtype(String ntype) this.ntype = ntype;public News getNews() return news;public void setNews(News news) this.news = news;public List getSortclist() return sortclist;public void setSortclist(List sortclist) this.sortclist = sortclist;public int getPageNow() return pageNow;public voi

31、d setPageNow(int pageNow) this.pageNow = pageNow;public int getPageSize() return pageSize;public void setPageSize(int pageSize) this.pageSize = pageSize;public int getK() return k;public void setK(int k) this.k = k;public int getI() return i;public void setI(int i) this.i = i;public int getIntRowCou

32、nt() return intRowCount;public void setIntRowCount(int intRowCount) RowCount = intRowCount;public int getIntPageCount() return intPageCount;public void setIntPageCount(int intPageCount) PageCount = intPageCount;第四步:在model层中实现方法NewsService获取文章类型列表方法Sortclist()public List sortclist(int ct_id)dbcon=new

33、 DBConnection();con=dbcon.getCon();List sortclist=new ArrayList();try psta=con.prepareStatement(this.getSortclistSql();psta.setInt(1, ct_id);rs=psta.executeQuery();while(rs.next()Ctype ctype=new Ctype();ctype.setCt_id(rs.getInt(ct_id);ctype.setCt_name(rs.getString(ct_name);ctype.setCt_image(rs.getSt

34、ring(ct_image);sortclist.add(ctype); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return sortclist;分类文章列表显示方法Sortnlist()public List sortnlist(int i, int pageSize,int ct_id)dbcon=new DBConnection();con=dbcon.getCon();List sortnlist=new ArrayList();try pstac=con.prepareStatement(this.getQueryCctypesql();rsc=pstac.executeQuery();String ntype=new String1000;while(rsc.next()ntype(int)rsc.getInt(ct_id)=rsc.getString(ct_name);rsc.close();pstac.clo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论