您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 工程监理 > struts2 从头到尾
一:Struts2入门:视图:jsp(login.jsp)控制器:StrutsPrepareAndExecuteFilter,在web.xml(过滤器)中。模型:action(LoginAction)那个FilterDispatcher是struts1的,struts2的是StrutsPrepareAndExecuteFilter在这里,我不知道的是如何实例化的,怎么就将com.test.action.Login.action给实例化的?Index.jsp%@pagelanguage=javaimport=java.util.*pageEncoding=UTF-8%%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;%!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//ENhtmlheadbasehref=%=basePath%titleMyJSP'index.jsp'startingpage/title/headbodyformaction=loginaction.actioninputtype=textname=usernameinputtype=submitname=submit/form/body/htmlWeb.xml?xmlversion=1.0encoding=UTF-8?web-appversion=2.5xmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/javaee/web-app_2_5.xsdfilterfilter-namestruts2/filter-namefilter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class/filterfilter-mappingfilter-namestruts2/filter-nameurl-pattern/*/url-pattern/filter-mappingwelcome-file-listwelcome-fileindex.jsp/welcome-file/welcome-file-list/web-appStruts.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEstrutsPUBLIC-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN=struts2extends=struts-defaultactionname=loginactionclass=com.test.action.LoginActionresultname=success/login.jsp/result/action/package/strutsLoginActionpackagecom.test.action;importcom.opensymphony.xwork2.ActionSupport;publicclassLoginActionextendsActionSupport{privateStringusername;publicStringpassword;publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}@OverridepublicStringexecute()throwsException{//TODOAuto-generatedmethodstubreturnSUCCESS;}}Login.jsp%@pagelanguage=javaimport=java.util.*pageEncoding=UTF-8%%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;%!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//ENhtmlheadbasehref=%=basePath%titleMyJSP'login.jsp'startingpage/title/headbodyusername:${requestScope.username}brpassword:${requestScope.password}br/body/html二Struts2的类型转换(局部和全局转换),我认为比如页面输入年龄(int),我在后台不红转换,我可以在前天用js判断,如果不是int就不让他过来,不就可以了吗?另一种简单方法自动实现类型转换:Point.jsp%@pagelanguage=javaimport=java.util.*pageEncoding=UTF-8%%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;%!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//ENhtmlheadbasehref=%=basePath%titleMyJSP'login.jsp'startingpage/title/headbodyusername:${requestScope.username}brpassword:${requestScope.password}br/body/htmlWeb.xml?xmlversion=1.0encoding=UTF-8?web-appversion=2.5xmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/javaee/web-app_2_5.xsdfilterfilter-namestruts2/filter-namefilter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class/filterfilter-mappingfilter-namestruts2/filter-nameurl-pattern/*/url-pattern/filter-mappingwelcome-file-listwelcome-fileindex.jsp/welcome-file/welcome-file-list/web-appStruts.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEstrutsPUBLIC-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN=struts2extends=struts-defaultactionname=pointclass=com.test.action.PointActionresultname=success/pointsuccess.jsp/result/action/package/strutspointActionpackagecom.test.action;importcom.opensymphony.xwork2.ActionSupport;importcom.test.bean.Point;publicclassPointActionextendsActionSupport{privatePointpoint;publicPointgetPoint(){returnpoint;}publicvoidsetPoint(Pointpoint){this.point=point;}@OverridepublicStringexecute()throwsException{//TODOAuto-generatedmethodstubreturnSUCCESS;}}pointpackagecom.test.bean;publicclassPoint{privateintx;privateinty;publicintgetX(){returnx;}publicvoidsetX(intx){this.x=x;}publicintgetY(){returny;}publicvoidsetY(inty){this.y=y;}//重写toString不重写的话,页面是object类行publicStringtoString(){Stringresult=x:+x+,y:+y;returnresult;}}Pointsuccess.jsp%@pagelanguage=javaimport=java.util.*pageEncoding=UTF-8%%@tagliburi=/struts-tagsprefix=s%%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;%!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//ENhtmlheadbasehref=%=basePath%titleMyJSP'pointsuccess.jsp'startingpage/title/headbody!--这个不好使,不知道为什么x:${requestScope.x}bry:${requestScope.y}--s:propertyvalue=point/br/body/html三:输入验证(Action级别)(其实下面这个不是
本文标题:struts2 从头到尾
链接地址:https://www.777doc.com/doc-5946075 .html