Struts经典案例.docx
- 文档编号:5403631
- 上传时间:2022-12-16
- 格式:DOCX
- 页数:19
- 大小:219.19KB
Struts经典案例.docx
《Struts经典案例.docx》由会员分享,可在线阅读,更多相关《Struts经典案例.docx(19页珍藏版)》请在冰豆网上搜索。
Struts经典案例
Struts(增删改查)
Struts.xml
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http:
//struts.apache.org/dtds/struts-2.0.dtd">
method="execute"> --验证的信息--> method="addAdmin"> --验证的信息--> method="selectAllPerson"> method="delPerson"> method="updatePerson"> method="addPerson"> --验证的信息--> method="delPersons"> 国际化: lang_en.properties main.title=MainPage user.id=UserID user.name=UserName user.age=UserAge user.address=Address user.caozuo=CaoZuo tianjia=AddUser shanchu=DeleteUsers update=Update del=Delete m.title=AddressList Lang_zh_CN.properties main.title=\u4E3B\u754C\u9762\u8BB0\u5F55 user.id=\u7528\u6237ID user.name=\u7528\u6237\u540D user.age=\u5E74\u9F84 user.address=\u4F4F\u5740 user.caozuo=\u64CD\u4F5C tianjia=\u6DFB\u52A0\u8054\u7CFB\u4EBA shanchu=\u6279\u91CF\u5220\u9664 update=\u4FEE\u6539 del=\u5220\u9664 m.title=\u4E2A\u4EBA\u901A\u8BAF\u5F55 LoginAction.java packagecom.struts.login.ck; importjava.io.IOException; importjava.io.PrintWriter; importjavax.servlet.Servlet; importjavax.servlet.ServletConfig; importjavax.servlet.ServletException; importjavax.servlet.ServletRequest; importjavax.servlet.ServletResponse; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.apache.struts2.interceptor.ServletRequestAware; importcom.bean.maneage.Admin; importcom.hnsfxy.dao.IAdminDao; importcom.hnsfxy.dao.impl.AdminDaoImpl; importcom.opensymphony.xwork2.ActionSupport; publicclassLoginActionextendsActionSupport{ /** * */ privatestaticfinallongserialVersionUID=1L; privateAdminadmin; publicAdmingetAdmin(){ returnadmin; } publicvoidsetAdmin(Adminadmin){ this.admin=admin; } publicStringaddAdmin()throwsException{ //进行判断,如果用户已经存在,则不让他加进去 IAdminDaoadmindao=newAdminDaoImpl(); Stringname=admin.getName(); if(true==admindao.selAdminByName(name)){ //有此用户则不让其加入进去 return"fail"; }else{ //无此用户加入进去 admindao.addAdmin(admin); return"success"; } } publicStringexecute()throwsException{ /* *StringSUCCESS="";if("admin".equals(admin.getName())&& *"123".equals(admin.getPassword())){SUCCESS="success";return *SUCCESS;}else{SUCCESS="fail";returnSUCCESS;} */ Stringflag=""; IAdminDaoa=newAdminDaoImpl(); inti=a.checkLogin(admin.getName(),admin.getPassword()); System.out.println(i); if(i==0){//成功登陆了 flag="success"; returnflag; }elseif(i==1){//密码有误 flag="fail"; returnflag; }else{//不存在用户 flag="warn"; returnflag; } } @Override publicvoidvalidate(){ //验证有问题 if(null==admin.getName()||"".equals(admin.getName())){ this.addFieldError("admin.name","用户名字不能为空! ! ! "); } if(null==admin.getPassword()||"".equals(admin.getPassword())){ this.addFieldError("admin.password","密码不能为空! ! ! "); } }} PersonAction.java packagecom.action.struts.person; importjava.io.IOException; importjava.io.PrintWriter; importjava.util.ArrayList; importjava.util.List; importjava.util.Locale; importjavax.servlet.ServletOutputStream; importjavax.servlet.ServletRequest; importjavax.servlet.http.Cookie; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.apache.struts2.ServletActionContext; importorg.apache.struts2.dispatcher.ServletActionRedirectResult; importorg.apache.struts2.interceptor.ServletRequestAware; importcom.bean.maneage.Person; importcom.hnsfxy.dao.IPersonDao; importcom.hnsfxy.dao.impl.PersonDaoImpl; importcom.opensymphony.xwork2.ActionSupport; publicclassPersonActionextendsActionSupportimplementsServletRequestAware{ /** * */ privatestaticfinallongserialVersionUID=1L; privatePersonperson;//查询全表的时候没有使用Person对象 publicPersongetPerson(){ returnperson; } publicvoidsetPerson(Personperson){ this.person=person; } privateHttpServletRequestrequest; privateHttpServletResponseresponse; publicvoidsetServletRequest(HttpServletRequestrequest){ this.request=request; } publicvoidsetServletResponse(HttpServletResponseresponse){ this.response=response; } IPersonDaopdao=newPersonDaoImpl(); publicStringselectAllPerson()throwsException{ List list=pdao.selAllPerson(); request.setAttribute("list",list);//传值的方法,jsp中使用request获取值 return"listall"; } publicStringdelPerson()throwsException{ //System.out.println(person.getId()); //pdao.delPersonById(person.getId());删除的id不能由person获得 Stringsid=request.getParameter("id").toString(); intid=Integer.parseInt(sid); pdao.delPersonById(id); return"listall"; } publicStringupdatePerson()throwsException{ Stringsid=request.getParameter("id").toString(); intid=Integer.parseInt(sid); Stringname=request.getParameter("name").toString(); Stringsage=request.getParameter("age").toString(); intage=Integer.parseInt(sage); Stringaddress=request.getParameter("address").toString(); Personp=newPerson(); p.setId(id); p.setName(name); p.setAge(age); p.setAddress(address); pdao.updatePerson(p); return"listall"; } //id不是自增长的可以自动设置的最好加一个 AJAX验证 publicStringaddPerson()throwsException{ pdao.addPerson(person); return"listall"; } //添加的时候要验证的 publicStringdelPersons()throwsException{ //使用request获取的 //需要完善 String[]s=request.getParameterValues("like"); if(null==s){ return"listall"; }else{ for(inti=0;i //System.out.println(s[i]); intid=Integer.parseInt(s[i]); pdao.delPersonById(id); } return"listall"; } } } WebRoot文件夹 Login.jsp <%@pagelanguage="java"contentType="text/html;charset=GBK" pageEncoding="GBK"%> <%@taglibprefix="s"uri="/struts-tags"%> DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http: //www.w3.org/TR/html4/loose.dtd"> formaction="check"> textfieldname="admin.name"label="用户名"/> passwordname="admin.password"label="密码"/> submitvalue="登陆"/> form> <%//request.setAttribute("name","必须登录");%> Main.jsp <%@pageimport="com.bean.maneage.Person"%> <%@pageimport="java.util.List"%> <%@pageimport="com.sun.jndi.url.ldaps.ldapsURLContextFactory"%> <%@pagelanguage="java"contentType="text/html;charset=GBK" pageEncoding="GBK"%> <%@taglibprefix="s"uri="/struts-tags"%> DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http: //www.w3.org/TR/html4/loose.dtd"> textname="m.title"/> functionselAll(like,ck){ varelems=document.getElementsByName("like"); for(vari=0;i elems[i].checked=ck.checked; } } textname="main.title"/> -- onclick="selAll('like',ck)"> textname="user.id"/> textname="user.name"/> textname="user.age"/> textname="user.address"/> textname="user.caozuo"/> --对指定的List对象尽行迭代输出--> iteratorvalue="#request.list"status="st"id="l"> -- propertyvalue="#st.index"/> propertyvalue="#l.id"/> propertyvalue="#l.name"/> propertyvalue="#l.age"/> propertyvalue="#l.address"/> href="update.jsp? id=${l.id}&&name=${l.name}&&age=${l.age}&&address=${l.address}"> i18n name="lang"> 如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。 copyright@ 2008-2022 冰点文档网站版权所有 经营许可证编号:鄂ICP备2022015515号-1登陆页面
记录编号 --> -->
冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。