Struts tag Library
In JSP pages, using the tag library instead of the traditional Java code to implement the page display logic is the trend of the times. However, custom tags can easily lead to repeated and non-standard implementations, as a result, java Web-based JSTL, struts-based taglib, and other label libraries have emerged to solve these problems.
The implementation principles of these tag libraries are almost the same. You can learn one, and the other is very easy to use. Let's start with the Struts tag library.
Common strust tag libraries include , And , And , Four.
Usage:
1. Configure message-resources in the strus-config.xml, note that this label should be placed behind the action-mappings label. The configuration information is as follows:
2. Introduce the MessageResources. properties file that provides international resources under the src directory.
3. Introduce taglib on the JSP page
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
4. The following are (Obtain the field value, JavaBean attribute value ), And (Judge whether it is empty ), And (Determine whether or not ), (Iteratively retrieve the data in the Set) configuration on the Action and JSP pages respectively
(1) --> Action
Public class BeanWriteAction extends Action {@ Overridepublic ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {// common string request. setAttribute ("hello", "quwenzhe"); request. setAttribute ("today", new Date (); Group group = new Group (); group. setName ("Upgrading"); User user User = new user (); User. setUsername ("quwenzhe"); user. setAge ("24"); user. setGroup (group); request. setAttribute ("user", user); return mapping. findForward ("success ");}}
--> JSP
Name:">
Age:">
GROUP:">
(2) And , And --> Action
public class EmptyPresentAction extends Action {@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {request.setAttribute("attr1", null);request.setAttribute("attr2", "");request.setAttribute("attr3", new ArrayList());return mapping.findForward("success");}}
And , And --> JSP
Attr1 is empty
Attr1 is empty
Attr1 exists
Attr1 does not exist
Attr2 is empty
Attr2 is empty
Attr2 exists
Attr2 does not exist
Attr3 is empty
Attr3 is empty
Attr3 exists
Attr3 does not exist
(3) --> Action
Public class IteratorAction extends Action {@ Overridepublic ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {Group group = new Group (); group. setName ("Advanced Training"); List userList = new ArrayList (); for (int I = 0; I <10; I ++) {User user = new User (); user. setUsername ("quwenzhe -->" + I); user. setAge ("24 -->" + I); user. setGroup (group); userList. add (user);} request. setAttribute ("userList", userList); return mapping. findForward ("success ");}}
--> JSP
Name |
Age |
Group |
No matching data |
|
|
|
In this way, the value set by request in the background Action can be easily obtained through tag in the JSP page, greatly improving work efficiency.