登 录
Struts标记分为(5组) 二期的时候JSP里学习过标准标签库与自定义标签,它的原理是一个JAVA类代码实现的,通过页面的 <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>来导入相应的配置文件,使用别名prefix即可调用标签库设定的标记 Html struts-html.tld Bean struts-bean.tld Logic struts-logic.tld Tiles struts-tiles.tld Nested struts-nested.tld 红字为Struts中重要掌握的标签内容。一般JSP页面都导入红字相应的标签 接下来通过一个实例来说明HTML标记的使用(struts-HTML标签库) 表单 Html代码 1. <html:form action="reg.do" method="post"> <html:form action="reg.do" method="post"> 文本框 Html代码 1. 用户名 <html:text property="username"/> 用户名 <html:text property="username"/> 密码框 Html代码 1. 密码 <html:password property="userpass"/> 密码 <html:password property="userpass"/> Property属性值的名字,就是对应ActionForm类里的属性名字 单选 性别 属性名字必须一致,否则视为多个单选组 Html代码 1. <html:radio property="gender" value="0"/>男 2. <html:radio property="gender" value="1"/>女 <html:radio property="gender" value="0"/>男 <html:radio property="gender" value="1"/>女 复选 爱好 这里返回的是一个数组,一般为String[] Html代码 1. <html:multibox property="hobby" value="看书"/>看书 2. <html:multibox property="hobby" value="睡觉"/>睡觉 3. <html:multibox property="hobby" value="编程"/>编程 <html:multibox property="hobby" value="看书"/>看书 <html:multibox property="hobby" value="睡觉"/>睡觉 <html:multibox property="hobby" value="编程"/>编程 下拉 Java代码 1. LabelValueBean lvbean=new LabelValueBean(); 2. lvbean.setLabel(""+i); 3. lvbean.setValue(""+i); 4. list.add(lvbean); LabelValueBean lvbean=new LabelValueBean(); lvbean.setLabel(""+i); lvbean.setValue(""+i); list.add(lvbean); 年龄 (它比较特殊,必须用到集合对象才可以往标签里添加下拉数据,它是以LABLE与 VALUE存取) 在ActionForm里获取的也是它设置的VALUE值。 Html代码 1. <html:select property="age"> 2. <html:options collection="list" labelProperty="label" property="value"/> 3. </html:select> <html:select property="age"> <html:options collection="list" labelProperty="label" property="value"/> </html:select> 文本区 Html代码 1. 备注 2. <html:textarea property="memo" rows="5" cols="60"/> 备注 <html:textarea property="memo" rows="5" cols="60"/> 隐藏 提交 直接请求发送 清除 清空表单的内容 取消 也是和请求一样会向Action请求,但是方式不一样可以用这个方法来判断是否是取消请求 Html代码 1. <html:submit>提交</html:submit> or <html:submit value=”提交”/> 2. <html:reset>清除</html:reset> 3. <html:cancel>取消</html:cancel> <html:submit>提交</html:submit> or <html:submit value=”提交”/> <html:reset>清除</html:reset> <html:cancel>取消</html:cancel> Java代码 1. //判断是否点击了"取消"按钮 2. boolean flag=this.isCancelled(request); 3. if(flag){ //如果点击了"取消"按钮 4. return mapping.findForward("reg"); 5. //重定向到注册页(想用重定向必须在struts-config.xml中配置属性 6. //<forward name="reg" path="/reg.jsp" redirect="true" /> 7. } //判断是否点击了"取消"按钮 boolean flag=this.isCancelled(request); if(flag){ //如果点击了"取消"按钮 return mapping.findForward("reg"); //重定向到注册页(想用重定向必须在struts-config.xml中配置属性 //<forward name="reg" path="/reg.jsp" redirect="true" /> } 第二个知识点为:Struts中的文件上传 记得以前Servlet时文件上传也是要在Form中指定请求数据类型吧,这里也一样(指定为二进制数据) Html代码 1. <html:form action="upload.do" method="post" enctype="multipart/form-data"> <html:form action="upload.do" method="post" enctype="multipart/form-data"> 用到的标签为[文件框] Html代码 1. <html:file property="photo"/> <html:file property="photo"/> 它对应的ActionForm的类型为org.apache.struts.upload.FormFile; 另一点注意的是,如果该表单里有其他的文本框要提交的话,需要注意的是,它也是以二进制数据提交,所以在ActionForm里无法直接通过请求处理配置转换成中文,必须使用自定义方法强制转换,并且要注意,它转发给页面时的一般属性也会丢失。如页面一有一属性与一文件上传,提交后直接转发到另一页面并显示属性的值报错 转换中文方法为: Java代码 1. public String changeGbk(String str){ 2. String newstr = null; 3. try { 4. newstr = new String(str.getBytes("ISO8859-1"), "gb2312"); 5. } catch (UnsupportedEncodingException ex) { 6. } 7. return newstr; 8. } public String changeGbk(String str){ String newstr = null; try { newstr = new String(str.getBytes("ISO8859-1"), "gb2312"); } catch (UnsupportedEncodingException ex) { } return newstr; } 接下来再看Action是如何处理文件上传的,它有两种方法处理上传文件并保存到指定目录。 方法一: 输入流 Java代码 1. UploadActionForm uploadActionForm = (UploadActionForm) form; 2. //接收内容并且存盘(web/photos) 3. FormFile photo=uploadActionForm.getPhoto();//获得请求文件的内容FormFile 4. try { 5. InputStream in=photo.getInputStream();//通过上传文件对象获得输入流 6. String filename=photo.getFileName();//常用方法,获得上传文件的文件名 7. String path=servlet.getServletContext().getRealPath("photos"); 8. //通过servlet获得服务器上下文对象获取指定目录的绝对路径 9. 如:d:/regprj/Web/photos 10. String newfilename=path + File.separator + filename; 11. //拼装要创建的文件全路径及文件名File.separator方法会根据系统自动选择’/’or’/’ 12. 如:d:/regprj/Web/photos/updatefile.jpg 13. FileOutputStream out=new FileOutputStream(newfilename);//把路径给文件输入流对象 14. 15. byte[] array1=new byte[1024];//设置缓冲大小,单位:字节 16. int len; 17. while( (len=in.read(array1))>0 ){ 18. out.write(array1,0,len); 19. } 20. out.flush(); 21. out.close(); 22. in.close(); 23. photo.destroy();//销毁文件; 24. } catch (Exception ex) { 25. ex.printStackTrace(); 26. } UploadActionForm uploadActionForm = (UploadActionForm) form; //接收内容并且存盘(web/photos) FormFile photo=uploadActionForm.getPhoto();//获得请求文件的内容FormFile try { InputStream in=photo.getInputStream();//通过上传文件对象获得输入流 String filename=photo.getFileName();//常用方法,获得上传文件的文件名 String path=servlet.getServletContext().getRealPath("photos"); //通过servlet获得服务器上下文对象获取指定目录的绝对路径 如:d:/regprj/Web/photos String newfilename=path + File.separator + filename; //拼装要创建的文件全路径及文件名File.separator方法会根据系统自动选择’/’or’/’ 如:d:/regprj/Web/photos/updatefile.jpg FileOutputStream out=new FileOutputStream(newfilename);//把路径给文件输入流对象 byte[] array1=new byte[1024];//设置缓冲大小,单位:字节 int len; while( (len=in.read(array1))>0 ){ out.write(array1,0,len); } out.flush(); out.close(); in.close(); photo.destroy();//销毁文件; } catch (Exception ex) { ex.printStackTrace(); } 方法二: Java代码 1. UploadActionForm uploadActionForm = (UploadActionForm) form; 2. //接收内容并且存盘(web/photos) 3. FormFile photo=uploadActionForm.getPhoto();//获得请求文件的内容FormFile 4. try { 5. String filename=photo.getFileName(); 6. String path=servlet.getServletContext().getRealPath("photos"); 7. String newfilename=path + File.separator + filename; 8. FileOutputStream out=new FileOutputStream(newfilename); 9. out.write(photo.getFileData()); 10. out.flush(); 11. out.close(); 12. photo.destroy();//销毁文件; 13. } catch (Exception ex) { 14. ex.printStackTrace(); 15. } UploadActionForm uploadActionForm = (UploadActionForm) form; //接收内容并且存盘(web/photos) FormFile photo=uploadActionForm.getPhoto();//获得请求文件的内容FormFile try { String filename=photo.getFileName(); String path=servlet.getServletContext().getRealPath("photos"); String newfilename=path + File.separator + filename; FileOutputStream out=new FileOutputStream(newfilename); out.write(photo.getFileData()); out.flush(); out.close(); photo.destroy();//销毁文件; } catch (Exception ex) { ex.printStackTrace(); } 区别就是两个方法使用的读取源二进制文件的方式不同。以上为黄色代码部份。 第三个知识点为<h1>测试超链接及图像标记</h1> Html代码 1. <html:link href="index.jsp">回主页</html:link><br /> 2. <html:link forward="index">回主页</html:link> <html:link href="index.jsp">回主页</html:link><br /> <html:link forward="index">回主页</html:link> 在<form-beans>后添加以下配置 Xml代码 1. <global-forwards> 2. <forward name="index" path="/index.jsp" /> 3. </global-forwards> <global-forwards> <forward name="index" path="/index.jsp" /> </global-forwards> 两个黄红代码中要配合一起才起使用 在一般的表单提交后,存放范围已经需要配置如request session,所以当请求交给了Action时,转发到JSP页面的时候,request所附带了请求的数据,可以直接用<bean: write>标签直接读取。但对象二进制数据的请求就不可以了。。 例: Html代码 1. <bean:write scope="request" name="regActionForm" property="username"/> <bean:write scope="request" name="regActionForm" property="username"/> Scope为请求数据存储在哪个范围,name就是请求的表单名,如:<form-bean name="regActionForm"。。 Property就是指请求时表单的属性名啦
抱歉!评论已关闭.