现在的位置: 首页 > 综合 > 正文

Struts1标签

2013年08月23日 ⁄ 综合 ⁄ 共 9196字 ⁄ 字号 评论关闭

  1. Struts标记分为(5组)   
  2. 二期的时候JSP里学习过标准标签库与自定义标签,它的原理是一个JAVA类代码实现的,通过页面的   
  3. <%@ taglib  uri="/WEB-INF/struts-html.tld" prefix="html" %>来导入相应的配置文件,使用别名prefix即可调用标签库设定的标记   
  4. Html  struts-html.tld   
  5. Bean   struts-bean.tld   
  6. Logic   struts-logic.tld   
  7. Tiles   struts-tiles.tld   
  8. Nested  struts-nested.tld   
  9. 红字为Struts中重要掌握的标签内容。一般JSP页面都导入红字相应的标签   
  10.   
  11. 接下来通过一个实例来说明HTML标记的使用(struts-HTML标签库)   
  12. 表单    
  13. Html代码   
  14.   
  15.    1. <html:form action="reg.do" method="post">      
  16.   
  17. <html:form action="reg.do" method="post">    
  18.   
  19.     
  20. 文本框    
  21. Html代码   
  22.   
  23.    1. 用户名 <html:text  property="username"/>      
  24.   
  25. 用户名 <html:text  property="username"/>    
  26.   
  27.     
  28. 密码框     
  29. Html代码   
  30.   
  31.    1. 密码   <html:password property="userpass"/>     
  32.   
  33. 密码   <html:password property="userpass"/>   
  34.   
  35.     
  36. Property属性值的名字,就是对应ActionForm类里的属性名字   
  37. 单选    性别   属性名字必须一致,否则视为多个单选组   
  38. Html代码   
  39.   
  40.    1. <html:radio property="gender" value="0"/>男      
  41.    2. <html:radio property="gender" value="1"/>女      
  42.   
  43. <html:radio property="gender" value="0"/>男    
  44. <html:radio property="gender" value="1"/>女    
  45.   
  46.  复选    爱好  这里返回的是一个数组,一般为String[]   
  47. Html代码   
  48.   
  49.    1. <html:multibox property="hobby" value="看书"/>看书      
  50.    2. <html:multibox property="hobby" value="睡觉"/>睡觉      
  51.    3. <html:multibox property="hobby" value="编程"/>编程      
  52.   
  53. <html:multibox property="hobby" value="看书"/>看书    
  54. <html:multibox property="hobby" value="睡觉"/>睡觉    
  55. <html:multibox property="hobby" value="编程"/>编程    
  56.   
  57. 下拉      
  58. Java代码   
  59.   
  60.    1. LabelValueBean lvbean=new LabelValueBean();      
  61.    2.  lvbean.setLabel(""+i);      
  62.    3.  lvbean.setValue(""+i);      
  63.    4.  list.add(lvbean);      
  64.   
  65.  LabelValueBean lvbean=new LabelValueBean();    
  66.   lvbean.setLabel(""+i);    
  67.   lvbean.setValue(""+i);    
  68.   list.add(lvbean);    
  69.   
  70.   年龄  (它比较特殊,必须用到集合对象才可以往标签里添加下拉数据,它是以LABLE与 VALUE存取)   
  71. 在ActionForm里获取的也是它设置的VALUE值。    
  72. Html代码   
  73.   
  74.    1. <html:select property="age">      
  75.    2. <html:options collection="list" labelProperty="label" property="value"/>      
  76.    3. </html:select>     
  77.   
  78. <html:select property="age">    
  79. <html:options collection="list" labelProperty="label" property="value"/>    
  80. </html:select>   
  81.   
  82.     
  83. 文本区    
  84. Html代码   
  85.   
  86.    1. 备注      
  87.    2. <html:textarea property="memo" rows="5" cols="60"/>      
  88.   
  89. 备注    
  90. <html:textarea property="memo" rows="5" cols="60"/>    
  91.   
  92.  隐藏   
  93. 提交 直接请求发送   
  94. 清除 清空表单的内容   
  95. 取消 也是和请求一样会向Action请求,但是方式不一样可以用这个方法来判断是否是取消请求   
  96. Html代码   
  97.   
  98.    1. <html:submit>提交</html:submit>  or  <html:submit value=”提交”/>      
  99.    2. <html:reset>清除</html:reset>      
  100.    3. <html:cancel>取消</html:cancel>      
  101.   
  102. <html:submit>提交</html:submit>  or  <html:submit value=”提交”/>    
  103. <html:reset>清除</html:reset>    
  104. <html:cancel>取消</html:cancel>    
  105.   
  106.     
  107. Java代码   
  108.   
  109.    1//判断是否点击了"取消"按钮      
  110.    2.         boolean flag=this.isCancelled(request);      
  111.    3.         if(flag){ //如果点击了"取消"按钮      
  112.    4.             return mapping.findForward("reg");      
  113.    5//重定向到注册页(想用重定向必须在struts-config.xml中配置属性      
  114.    6.   //<forward name="reg" path="/reg.jsp" redirect="true" />      
  115.    7.         }      
  116.   
  117. //判断是否点击了"取消"按钮    
  118.         boolean flag=this.isCancelled(request);    
  119.         if(flag){ //如果点击了"取消"按钮    
  120.             return mapping.findForward("reg");    
  121. //重定向到注册页(想用重定向必须在struts-config.xml中配置属性    
  122.   //<forward name="reg" path="/reg.jsp" redirect="true" />    
  123.         }    
  124.   
  125.     
  126.   
  127.   
  128.   
  129. 第二个知识点为:Struts中的文件上传   
  130. 记得以前Servlet时文件上传也是要在Form中指定请求数据类型吧,这里也一样(指定为二进制数据)   
  131. Html代码   
  132.   
  133.    1. <html:form action="upload.do" method="post" enctype="multipart/form-data">      
  134.   
  135. <html:form action="upload.do" method="post" enctype="multipart/form-data">    
  136.   
  137.     
  138. 用到的标签为[文件框]   
  139. Html代码   
  140.   
  141.    1. <html:file property="photo"/>      
  142.   
  143. <html:file property="photo"/>    
  144.   
  145.  它对应的ActionForm的类型为org.apache.struts.upload.FormFile;   
  146. 另一点注意的是,如果该表单里有其他的文本框要提交的话,需要注意的是,它也是以二进制数据提交,所以在ActionForm里无法直接通过请求处理配置转换成中文,必须使用自定义方法强制转换,并且要注意,它转发给页面时的一般属性也会丢失。如页面一有一属性与一文件上传,提交后直接转发到另一页面并显示属性的值报错   
  147. 转换中文方法为:    
  148.       
  149. Java代码   
  150.   
  151.    1public String changeGbk(String str){      
  152.    2.         String newstr = null;      
  153.    3.         try {      
  154.    4.             newstr = new String(str.getBytes("ISO8859-1"), "gb2312");      
  155.    5.         } catch (UnsupportedEncodingException ex) {      
  156.    6.         }      
  157.    7.         return newstr;      
  158.    8. }      
  159.   
  160. public String changeGbk(String str){    
  161.         String newstr = null;    
  162.         try {    
  163.             newstr = new String(str.getBytes("ISO8859-1"), "gb2312");    
  164.         } catch (UnsupportedEncodingException ex) {    
  165.         }    
  166.         return newstr;    
  167. }    
  168.   
  169.     
  170. 接下来再看Action是如何处理文件上传的,它有两种方法处理上传文件并保存到指定目录。   
  171. 方法一:   
  172. 输入流   
  173. Java代码   
  174.   
  175.    1. UploadActionForm uploadActionForm = (UploadActionForm) form;      
  176.    2.         //接收内容并且存盘(web/photos)      
  177.    3.         FormFile photo=uploadActionForm.getPhoto();//获得请求文件的内容FormFile      
  178.    4.          try {      
  179.    5.             InputStream in=photo.getInputStream();//通过上传文件对象获得输入流      
  180.    6.             String filename=photo.getFileName();//常用方法,获得上传文件的文件名      
  181.    7.             String path=servlet.getServletContext().getRealPath("photos");      
  182.    8//通过servlet获得服务器上下文对象获取指定目录的绝对路径      
  183.    9. 如:d:/regprj/Web/photos      
  184.   10.             String newfilename=path + File.separator + filename;      
  185.   11//拼装要创建的文件全路径及文件名File.separator方法会根据系统自动选择’/’or’/’      
  186.   12. 如:d:/regprj/Web/photos/updatefile.jpg      
  187.   13.             FileOutputStream out=new FileOutputStream(newfilename);//把路径给文件输入流对象      
  188.   14.      
  189.   15.             byte[] array1=new byte[1024];//设置缓冲大小,单位:字节      
  190.   16.             int len;      
  191.   17.             while(  (len=in.read(array1))>0  ){      
  192.   18.                 out.write(array1,0,len);      
  193.   19.             }      
  194.   20.             out.flush();      
  195.   21.             out.close();      
  196.   22.             in.close();      
  197.   23.             photo.destroy();//销毁文件;      
  198.   24.         } catch (Exception ex) {      
  199.   25.             ex.printStackTrace();      
  200.   26.         }      
  201.   
  202. UploadActionForm uploadActionForm = (UploadActionForm) form;    
  203.         //接收内容并且存盘(web/photos)    
  204.         FormFile photo=uploadActionForm.getPhoto();//获得请求文件的内容FormFile    
  205.          try {    
  206.             InputStream in=photo.getInputStream();//通过上传文件对象获得输入流    
  207.             String filename=photo.getFileName();//常用方法,获得上传文件的文件名    
  208.             String path=servlet.getServletContext().getRealPath("photos");    
  209. //通过servlet获得服务器上下文对象获取指定目录的绝对路径    
  210. 如:d:/regprj/Web/photos    
  211.             String newfilename=path + File.separator + filename;    
  212. //拼装要创建的文件全路径及文件名File.separator方法会根据系统自动选择’/’or’/’    
  213. 如:d:/regprj/Web/photos/updatefile.jpg    
  214.             FileOutputStream out=new FileOutputStream(newfilename);//把路径给文件输入流对象    
  215.   
  216.             byte[] array1=new byte[1024];//设置缓冲大小,单位:字节    
  217.             int len;    
  218.             while(  (len=in.read(array1))>0  ){    
  219.                 out.write(array1,0,len);    
  220.             }    
  221.             out.flush();    
  222.             out.close();    
  223.             in.close();    
  224.             photo.destroy();//销毁文件;    
  225.         } catch (Exception ex) {    
  226.             ex.printStackTrace();    
  227.         }    
  228.   
  229.     
  230. 方法二:   
  231. Java代码   
  232.   
  233.    1. UploadActionForm uploadActionForm = (UploadActionForm) form;      
  234.    2.         //接收内容并且存盘(web/photos)      
  235.    3.         FormFile photo=uploadActionForm.getPhoto();//获得请求文件的内容FormFile      
  236.    4try {      
  237.    5.            String filename=photo.getFileName();      
  238.    6.            String path=servlet.getServletContext().getRealPath("photos");      
  239.    7.            String newfilename=path + File.separator + filename;      
  240.    8.            FileOutputStream out=new FileOutputStream(newfilename);      
  241.    9.            out.write(photo.getFileData());      
  242.   10.            out.flush();      
  243.   11.            out.close();      
  244.   12.            photo.destroy();//销毁文件;      
  245.   13.        } catch (Exception ex) {      
  246.   14.            ex.printStackTrace();      
  247.   15.         }      
  248.   
  249. UploadActionForm uploadActionForm = (UploadActionForm) form;    
  250.         //接收内容并且存盘(web/photos)    
  251.         FormFile photo=uploadActionForm.getPhoto();//获得请求文件的内容FormFile    
  252. try {    
  253.            String filename=photo.getFileName();    
  254.            String path=servlet.getServletContext().getRealPath("photos");    
  255.            String newfilename=path + File.separator + filename;    
  256.            FileOutputStream out=new FileOutputStream(newfilename);    
  257.            out.write(photo.getFileData());    
  258.            out.flush();    
  259.            out.close();    
  260.            photo.destroy();//销毁文件;    
  261.        } catch (Exception ex) {    
  262.            ex.printStackTrace();    
  263.         }    
  264.   
  265.  区别就是两个方法使用的读取源二进制文件的方式不同。以上为黄色代码部份。   
  266.   
  267. 第三个知识点为<h1>测试超链接及图像标记</h1>    
  268. Html代码   
  269.   
  270.    1. <html:link href="index.jsp">回主页</html:link><br />      
  271.    2. <html:link forward="index">回主页</html:link>      
  272.   
  273. <html:link href="index.jsp">回主页</html:link><br />    
  274. <html:link forward="index">回主页</html:link>    
  275.   
  276.    在<form-beans>后添加以下配置    
  277.     
  278. Xml代码   
  279.   
  280.    1. <global-forwards>      
  281.    2.     <forward name="index" path="/index.jsp" />      
  282.    3.   </global-forwards>      
  283.   
  284. <global-forwards>    
  285.     <forward name="index" path="/index.jsp" />    
  286.   </global-forwards>    
  287.   
  288.  两个黄红代码中要配合一起才起使用   
  289.   
  290. 在一般的表单提交后,存放范围已经需要配置如request session,所以当请求交给了Action时,转发到JSP页面的时候,request所附带了请求的数据,可以直接用<bean: write>标签直接读取。但对象二进制数据的请求就不可以了。。   
  291.   
  292. 例:   
  293. Html代码   
  294.   
  295.    1. <bean:write scope="request" name="regActionForm" property="username"/>      
  296.   
  297. <bean:write scope="request" name="regActionForm" property="username"/>    
  298.   
  299.  Scope为请求数据存储在哪个范围,name就是请求的表单名,如:<form-bean name="regActionForm"。。   
  300. Property就是指请求时表单的属性名啦  

抱歉!评论已关闭.