*Struts之html标签的学习

本文深入探讨了Struts框架的应用,包括锚点链接、全局链接、参数传递等特性,并详细介绍了表单提交、文本输入、文件上传及错误处理等功能实现方式。


1。锚点链接
<html:link linkName="top">顶部</html:link>
在一篇文章的结尾处编写如下 代码。
<html:link href="" anchor="top">回到顶部</html:link>
href属性值为"",指定了链接的目标页面为当前页面
  全局链接
<global-forwards >
    <forward name="globalerror" path="/Errors.jsp" />
</global-forwards>
<html:link forward="globalerror">显示错误信息!</html:link>
 传参
<%
  String usersex="Man";
  Session.setAttribute("mysex",usersex);
%>
将该变量作为参数通过链接进行传递。
<html:link page="/welcome.jsp" paramId="sex" paramName="mysex">显示性别!</html:link>
__________________________
将userinfo中的username属性值作为参数通过链接进行传递。
假设userinfo中存在username和userpwd两个属性,并且username="yxq",userpwd="123",

session.setAttribute ("userBean",userinfo)。
<html:link page="/welcome.jsp" paramId="name" paramName="userinfo"

paramProperty="username"/>显示用户名!</html:link>
上述代码生成如下HTML元素。
<a href="/Logon/welcome.jsp?name=yxq">显示用户名!</html:link>
________________________________
<% Hashtable logonuser=new Hashtable();
  logonuser.put("username","yxq");
  logonusr.put("userpwd","123");
  session.setAttribute("userinfo",logonuser);
%>
<html:link page="/logon.do" name="userinfo" scope="session"/>登录</html:link>
上述代码生成如下HTML元素。<a href="/office/logon.do?username=yxq&amp;userpwd=123">
2.<html:form action="logon" method="post" focus="password">
没有name属性,因为在这里设置了,可直接用logonform
<action      path="/logon"  name="logonform"type="logon.action.LogonAction"/>
3<html:textarea>多行多列文本输入标签
4.文件上传
<html:file>
csdn空间4/struts之上传的学习.rar

5。errror
 errors.add("nameerror",new ActionMessage("reg.name.error"));
    errors.add("pwderror",new ActionMessage("reg.pwd.error"));
tr><td align="right">用户名:</td>
 <td><html:text property="username"/></td></tr>
  <tr><td colspan="2"><html:errors property="nameerror"/></td></tr>   //显示用户名的相关

信息  <tr><td align="right">密&nbsp;&nbsp;码:</td>
     <td><html:password property="userpwd" redisplay="false"/></td></tr>
    <tr><td colspan="2"><html:errors property="pwderror"/></td></tr>   //显示用户密码的相

关信息

 

 

 

 

 

 

______________________

Struts文件上传
<html:form action="upfile" method="post" focus="file" enctype="multipart/form-data" >
选择文件:<html:file property="file" size="30"/
-----------------------------
import org.apache.struts.upload.FormFile;
UpFileForm fileform = (UpFileForm) form;
FormFile upfile = fileform.getFile();
 String filename = upfile.getFileName();
  int filesize = upfile.getFileSize();
 String filepath = request.getRealPath("") + "/upfile";//上传目录
File file = new File(filepath, filename);
  byte a[] = new byte[filesize];
try {
   InputStream in = upfile.getInputStream();
   int read = 0;
   int allread = 0;
   while (allread < filesize) {
    read = in.read(a, allread, filesize);
    allread += read;
   }
   FileOutputStream out = new FileOutputStream(file);
   out.write(a);
   in.close();
   out.close();
   messages.add("success", new ActionMessage("file.up.file.success"));
   saveMessages(request, messages);
  } catch (Exception e) {
   e.printStackTrace();
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值