5 Types of tags supported by struts1:
HTML Tags: used to create HTML input forms that interact with the struts framework and other appropriate HTML tags
Bean Tags: used when accessing JavaBeans and its properties, and defining a new bean
<%@ taglib uri= "Http://struts.apache.org/tags-bean" prefix= "Bean"%>
Logic Tags: manage the output generated by conditions and the loops generated by the set of objects
<%@ taglib uri= "http://struts.apache.org/tags-logic" prefix= "logic"%>
Template Label: As the tiles framework package appears, this tag has started to reduce the use of
Nested Tags: enhanced ability to nest use of other struts tags
Set properties in action and remove properties from JSP
Loginaction
Packagecom.bjpowernode.struts;Importjava.util.ArrayList;Importjava.util.Date;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importorg.apache.struts.action.Action;ImportOrg.apache.struts.action.ActionForm;ImportOrg.apache.struts.action.ActionForward;Importorg.apache.struts.action.ActionMapping; Public classLoginactionextendsaction{@Override PublicActionforward Execute (actionmapping mapping, actionform form, httpservletrequest request, HTTPSERVLETRESPO NSE response)throwsException {loginactionform LAF=(loginactionform) Form; if("admin". Equals (Laf.getusername ()) && "Admin". Equals (Laf.getpassword ())) {Request.setattribute ("Message", "Test message!"); Request.setattribute ("User", CreateUser ("Zhang San", "123456", 29)); Request.setattribute ("UserList", Createuserlist ()); Request.setattribute ("UserMap", Createusermap ()); returnMapping.findforward ("Success"); } returnMapping.findforward ("Error"); } PrivateUser CreateUser (string Username, string password,intAge ) {User User=NewUser (); User.setusername (username); User.setpassword (password); User.setage (age); User.setcreatedate (NewDate ()); returnuser; } PrivateList<user>createuserlist () {List<User> userlist =NewArraylist<user>(); Userlist.add (CreateUser ("John Doe", "123123", 30)); Userlist.add (CreateUser ("Harry", "123999", 34)); Userlist.add (CreateUser ("Zhao Liu", "123123", 39)); returnuserlist; } PrivateMap<string, user>Createusermap () {Map<string, user> map =NewHashmap<string, user>(); Map.put ("K1", CreateUser ("Map1", "map123", 100)); Map.put ("K2", CreateUser ("Map2", "map12123", 110)); Map.put ("K3", CreateUser ("Map3", "Map123map", 130)); returnmap; }}
Post-Jump JSP
login_success.jsp
<%@ Page Language="Java"Import="java.util.*"pageencoding="GB18030"%><%@ taglib URI="Http://struts.apache.org/tags-bean"prefix="Bean" %><%@ taglib URI="Http://struts.apache.org/tags-logic"prefix="Logic" %> <%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <Basehref= "<%=basePath%>"> <title>My JSP ' login_success.jsp ' starting page</title> <Metahttp-equiv= "Pragma"content= "No-cache"> <Metahttp-equiv= "Cache-control"content= "No-cache"> <Metahttp-equiv= "Expires"content= "0"> <Metahttp-equiv= "keywords"content= "Keyword1,keyword2,keyword3"> <Metahttp-equiv= "description"content= "This is my page"> <!--<link rel= "stylesheet" type= "Text/css " href= "Styles.css" > - </Head> <Body>This is my JSP page. Success!!! <BR> <!--normal attribute removal - <Bean:writename= "message"/><BR> <!--property of object type out - <Bean:writename= "User" Property= "username"/><BR> <Bean:writename= "User" Property= "Password"/><BR> <Bean:writename= "User" Property= "CreateDate"format= "Yyyy-mm-dd HH:mm:ss"/><BR> <HR> <!--properties of list type out - <logic:iterateID= "User"name= "UserList"> <Bean:writename= "User" Property= "username"/><BR> <Bean:writename= "User" Property= "Password"/><BR> <Bean:writename= "User" Property= "CreateDate"format= "Yyyy-mm-dd HH:mm:ss"/><BR> </logic:iterate> <HR> <!--attributes of the map type are taken out - <logic:iterateID= "Useriteam"name= "UserMap"> <Bean:writename= "Useriteam" Property= "Key"/><BR> <Bean:writename= "Useriteam" Property= "Value.username"/><BR> <Bean:writename= "Useriteam" Property= "Value.password"/><BR> <Bean:writename= "Useriteam" Property= "Value.createdate"format= "Yyyy-mm-dd HH:mm:ss"/><BR> </logic:iterate> </Body></HTML>
STRUTS1 Four: Common labels