tailieunhanh - Core Servlets and JavaServer Pages phần 7

Để thực hiện nhiệm vụ này, bước đầu tiên là xác định một lớp IfTag để xử lý csajsp: nếu thẻ. Xử lý này nên có phương pháp để xác định và kiểm tra điều kiện là đúng hay sai (setCondition và getCondition) cũng như các phương pháp chỉ định | Prentice Hall and Sun Microsystems. Personal use only do not redistribute. Chapter 14 Creating Custom JSP Tag Libraries Now suppose that we want to define a set of tags that would be used like this csajsp if csajsp condition someExpression csajsp condition csaj sp then JSP to include if condition is true csaj sp then csajsp else JSP to include if condition is false csajsp else csajsp if To accomplish this task the first step is to define an IfTag class to handle the csajsp if tag. This handler should have methods to specify and check whether the condition is true or false setCondition and getCondition as well as methods to designate and check if the condition has ever been explicitly set setHasCondition and getHasCondition since we want to disallow csajsp if tags that contain no csajsp condition entry. Listing shows the code for IfTag. The second step is to define a tag handler for csajsp condition. This class called IfConditionTag defines a doStartTag method that merely checks if the tag appears within IfTag. It returns EVAL_BODY_TAG if so and throws an exception if not. The handler s doAfterBody method looks up the body content getBodyContent converts it to a String getString and compares that to true . This approach means that an explicit value of true can be substituted for a JSP expression like expression if during initial page development you want to temporarily designate that the then portion should always be used. Using a comparison to true also means that any other value will be considered false. Once this comparison is performed the result is stored in the enclosing tag by means of the setCondition method of IfTag. The code for IfConditionTag is shown in Listing . The third step is to define a class to handle the csajsp then tag. The doStartTag method of this class verifies that it is inside IfTag and also checks that an explicit condition has been set . that the IfConditionTag has already appeared within the IfTag . The doAfterBody method

TỪ KHÓA LIÊN QUAN