jsp页面index.jsp:
http://www.solol.org/technologic/java/j%2Dstruts/#d3e77
<%@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>
<%@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>
<!-- 其它标签通过绑定到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>
<%=te.getContentType()%><br>
string attribute:
<logic:present name="tdstr">
<bean:write name="tdstr" /><br />
</logic:present>
<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>
<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>
<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>
<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>
</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;
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;
}
{
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";
}
}
// {
// 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 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.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
{
{
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");
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