struts标签bean:cookie,bean:write,logic:page,logic:present,logic:iterate使用实例

jsp页面index.jsp:
<%@page contentType="text/html;charset=gb2312" language="java"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html>
<head><title>struts test</title></head>
<body>
  <bean:cookie id="jSession" name="userInfo" value="11111"/>
  <!-- 其它标签通过绑定到page作用域中的属性使用该值 -->
  这个cookie的名称是<bean:write name="jSession" property="name"/>,值为<bean:write name="jSession" property="value"/>。<br/>
  <!-- JSP脚本通过scripting变量使用该值 -->
  <%
    String name = jSession.getName();
    String value = jSession.getValue();
    out.println("这个cookie的名称是"+name+",值为"+value+"。<br/>"); 
  %>
<html:form action="login" method="post">
<html:text property="user" value="hello" /><html:errors property="user"/>
<html:text property="pwd" /><html:errors property="pwd" />
<br>
<html:errors />
<html:checkbox property="cb" value="1">fe</html:checkbox><html:errors property="cb" />
<html:submit value="提交" />
</html:form>
<bean:page id="te" property="request" />
<%=te.getContentType()%><br>
string attribute:
<logic:present name="tdstr">
<bean:write name="tdstr" /><br />
</logic:present>
other object attribute:
<logic:present name="tdobj">
<bean:write name="tdobj" property="user"/><br />
</logic:present>
iterate other Array object attribute:<br />
<logic:present name="sarr">
<logic:iterate id="sobj" name="sarr">
<bean:write name="sobj" /><br />
</logic:iterate>
</logic:present>
iterate other Hashtable object attribute:<br />
<logic:present name="htobj">
<logic:iterate id="hto" name="htobj">
<bean:write name="hto" property="key" />:<bean:write name="hto" property="value" /><br />
</logic:iterate>
</logic:present>
</body>
</html>

LoginForm.java:

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import javax.servlet.http.HttpServletRequest;
public class LoginForm extends ActionForm
{
 private String user = "";
 private String pwd = "";
 private String cb = "";
 
 
 public LoginForm()
 {
 }
 
 public String getCb()
 { 
  return this.cb;
 }
 
 public void setCb(String cb)
 {
  this.cb = cb;
 } 
 
 public String getUser()
 {
  return this.user;
 }
 
 public void setUser(String user)
 {
  this.user = user;
 }
 
 public String getPwd()
 {
  return this.pwd;
 }
 
 public void setPwd(String pwd)
 {
  this.pwd = pwd;
 }
// public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
// {
//  ActionErrors errors = new ActionErrors();
//  
//  if(this.user==null || this.user.length()<1)
//  {
//   ActionError ae = new ActionError("user");
//   errors.add("user",ae);
//  }
//  if(this.pwd==null || this.pwd.length()<1)
//  {
//   errors.add("pwd",new ActionError("login.error"));
//  }
//  
//  return errors;  
// }
 
 public void reset(ActionMapping mapping, HttpServletRequest request)
 {
  this.cb = "0";
 }
}


LoginAction.java:

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Hashtable;
public class LoginAction extends Action
{
 public LoginAction()
 {
  
 }
 
 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
 {
  Cookie c = new Cookie( "userInfo" ,"py8");   
  c.setComment( "A test cookie" );   
  c.setMaxAge(120);   
  response.addCookie(c);
  
  String s[] = new String[3];
  s[0]="this is s0";
  s[1]="this is s1";
  s[2]="this is s2";
  request.setAttribute("sarr",s);
  
  Hashtable ht = new Hashtable();
  ht.put("ht1","this is ht1");
  ht.put("ht2","this is ht2");
  ht.put("ht3","this is ht3");
  request.setAttribute("htobj",ht);
  
  request.setAttribute("tdobj",(LoginForm)form);
  request.setAttribute("tdstr","hao ren a");
  
  return this.getServlet().findForward("index");
 }
}


http://www.solol.org/technologic/java/j%2Dstruts/#d3e77
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值