随笔-86  评论-33  文章-0  trackbacks-0
<html:link>
  
用于生成HTML<a>元素。用<html:link>创建超链接时,有两个优点:
  1、容许在url中以多种方式包含请求参数。
  2、当用户浏览器关闭Cookie时,会自动重写url,把SessionID作为请求参数包含在URL中,以跟踪用户会话。
 
 属性:
        forward :指定全局转发链接(使用struts-config.xml中的<global-forward>元素中的<forward>元素)
        href:指定完整的URL链接
        page:指定相对于当前网页的URL
1.创建全局转发链接
  <global-forwards>
    
<forward name="index" path="/index.jsp" />
  
</global-forwards>
<html:link forward="index">
  GLOBAL LINK
</html:link>
标签中forward属性与<global-forward>元素中的<forward>元素匹配,以上代码生成
<a href="/index.jsp">GLOBAL LINK</a>

2.创建完整的URL链接
<html:link href="http://www.blogjava.net/envoydada">
BLOGJAVA
</html:link>
生成代码 :
<a href="http://www.blogjava.net/envoydada">BLOGJAVA</a>
3.从当前网页创建相对URL
<html:link page="/login.do">
  LOGIN
</html:link>

<html:multibox>
  复选框,对应的ActionFormBean 中定义一个数组来接收
//formbean中定义数组
private String a[] = new String[0];
public String[] getA(){return a[];}
public void setA(String arr[]){this.a = arr;}
<html:multibox property = "a" value="dada" />
<html:multibox property = "a" value="derek"/>
被选中的复选框将值提交给ActionFormBean中的数组中,没选中的就不会包含

<html:file>
     实现文件上传功能
     1. jsp代码 
<html:form action="/fileAction.do" method="POST" enctype="multipart/form-data">
   
<html:file property="file" />
   
<html:submit property="submit" value="Submit"/><br>
   
<html:reset value ="Reset"/>
</html:form>
    2. ActionFormBean
formbean中必须定义一个与页面<html:file property="file" >属性对应的属性file,且这个属性的类型为
org.apache.struts.upload.Formfile类型
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;

public class FileActionForm extends ActionForm {
    
private FormFile file;
    
public ActionErrors validate(ActionMapping actionMapping,
                                 HttpServletRequest httpServletRequest) {          
        
return null;}
    
public void reset(ActionMapping actionMapping,HttpServletRequest servletRequest) {}
    
public void setFile(FormFile file) {this.file = file;}
    
public FormFile getFile() {return file;}
}
3.在action类中处理文件上传
public class FileAction extends Action {
    
public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest servletRequest,
                                 HttpServletResponse servletResponse) {
        FileActionForm fileActionForm 
= (FileActionForm) actionForm;
        FormFile file 
= fileActionForm.getFile();
        
try {
           InputStream in 
= file.getInputStream();
           OutputStream out 
= new FileOutputStream("d:\\"+new String(file.getFileName().getBytes("utf-8")));
           
int i=0;
           
byte[] buffer = new byte[4096];
           
while((i = in.read(buffer,0,4096))!=-1){
           out.write(buffer,
0,i);
           }
           in.close();
           out.close();
        } 
catch (FileNotFoundException ex) {
        } 
catch (IOException ex) {}
        
try {
            servletRequest.setAttribute(
"name",new String(file.getFileName().getBytes("UTF-8"),"UTF-8"));
        } 
catch (UnsupportedEncodingException ex1) {
        }
        servletRequest.setAttribute(
"size",Integer.toString(file.getFileSize()));
        
return actionMapping.getInputForward();
    }
}

<bean:page>
  检索JSP隐含对象,如request,session,response
<bean:page id="this_session" property="session"/>
<bean:write name="this_session" property="id"/>  //在页面显示session id号

<bean:message>
 
显示ResourceBundle中的消息
  显示邦定的、带变量的资源文件:
资源文件MyMessage.properties
hello 
= Hello,{0}

struts
-config.xml
  
<message-resources parameter="MyMessage" key="my" />

标签
<bean:message bundle="my" key="hello" arg0="Dada"/>
页面显示结果:Hello,Dada

<bean:resource>
检索Web资源的内容,装载到一个JAVABEAN中。
属性:id   定义一个代表web资源的变量
             name  web资源的路径
             input 如果没设置则 id 定义的变量为字符型,如果设置则id 定义的变量为java.io.InputStrean
<bean:resource id="scource" name="/file.jsp" />  //file.jsp中的内容装载到一个变量中,scource就是这个变量的引用
<bean:write name="scource"/> //写出这个对象的内容

<bean:write>
   format属性格式化输出
    输出Float型值 
    如request.setAtrribute("floatvalue",Float.valueOf("3.14159"));
   <bean:write format="#.####" name="floatvalue">  输出结果是 3.1416

     输出日期型
     request.setAtrribute("date",Calendar.getInstance());
    <bean:write format="MM-dd-yyyy hh:mm:ss" name="date" property="time"/> 输出结果是 06-15-2006 10:12:35
  
    filter属性
    默认为true,把输出内容中的特殊html符号作为普通字符串来显示,如果为false,则不会
posted on 2006-03-28 14:03 Derek.Guo 阅读(1455) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航:
 
MSN:envoydada@hotmail.com QQ:34935442