当前位置:编程学习 > JS >>

Struts2 自定义标签(JSP视图)实现图形验证功能

先看一下官网关于component标签的注释:
 
Description
Renders an custom UI widget using the specified templates. Additional objects can be passed in to the template using the param tags.
 
Freemarker:
 
Objects provided can be retrieve from within the template via $parameters.paramname.
 
Jsp:
 
Objects provided can be retrieve from within the template via <s:property value="%{parameters.paramname}" />
 
In the bottom JSP and Velocity samples, two parameters are being passed in to the component. From within the component, they can be accessed as:-
 
Freemarker:
 
$parameters.get('key1') and $parameters.get('key2') or $parameters.key1 and $parameters.key2
 
Jsp:
 
<s:property value="%{parameters.key1}" /> and <s:property value="%{'parameters.key2'}" /> or <s:property value="%{parameters.get('key1')}" /> and <s:property value="%{parameters.get('key2')}" />
 
Currently, your custom UI components can be written in Velocity, JSP, or Freemarker, and the correct rendering engine will be found based on file extension.
 
Remember: the value params will always be resolved against the ValueStack so if you mean to pass a string literal to your component, make sure to wrap it in quotes i.e. value="'value1'" otherwise, the the value stack will search for an Object on the stack with a method of getValue1(). (now that i've written this, i'm not entirely sure this is the case. i should verify this manana)
 
 
 
 
If Jsp is used as the template, the jsp template itself must lie within the webapp itself and not the classpath. Unlike Freemarker or Velocity, JSP template could not be picked up from the classpath.
 
templateDir and theme attribute
The final path to the template will be built using the templateDir andtemplate attributes, like ${templateDir}/${theme}/${template}. If for example your component is under /components/html/option.jsp, you would have to set templateDir="components", theme="html" and template="options.jsp".
 
For any Struts tag that you use in your component, make sure that you set its templateDir="template"
 
-------------------------------------------------------------------------------------------------------------------------------------
 
 
self.tld:
 
[html]  
<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
 "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">  
<taglib>  
    <tlib-version>1.0</tlib-version>  
    <jsp-version>1.2</jsp-version>  
    <short-name>map</short-name>  
  
    <tag>  
        <name>self</name>  
        <tag-class>servlet.SelfDefinationTag</tag-class>  
        <body-content>JSP</body-content>  
    </tag>  
</taglib>  
释义:这是JSP模板文件,定义了self标签,self标签的处理类为SelfDefinationTag。
 
SelfDefinationTag.jsp:
 
[html] 
<%@ page language="java" pageEncoding="gb2312"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<%@taglib prefix="s" uri="/struts-tags"%>  
<html>  
   <body>  
      <s:component template="/components/image.jsp templateDir="MyTemplate" theme="xhtml" />  
   </body>  
</html>  
释义:Struts2中的component标签是用来方便开发者定义自己开发的标签。(component:组件;template:模板)
 
这里定义了一个新的JSP文件,在该文件中使用component标签来调用image.jsp这个模板文件。
 
templateDir:定义模板文件所在的根目录名,若不显示声明则默认为"template";
 
theme:定义主题,若不显示声明则默认为"xhtml"。
 
因此,若templateDir和theme都不声明,则系统调用的模板文件就是/template/xhtml下的模板文件。
 
image.jsp:
 
[html] 
<%@ page language="java"  pageEncoding="gb2312"%>  
<%@ taglib uri="Self" prefix="s" %>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <body>    
    <s:self></s:self>  
  </body>  
</html>  
释义:自定义标签<s:self>,具体的标签含义在下面的SelfDefinationTag标签处理类。
 
SelfDefinationTag.java:
 
[java] 
package servlet;  
  
import java.io.IOException;  
  
import javax.servlet.ServletContext;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.tagext.TagSupport;  
  
public class SelfDefinationTag extends TagSupport {  
    private static final long serialVersionUID = 1L;  
    String contextPath;  
      
    public int doStartTag() throws JspException {         
         JspWriter out = pageContext.getOut();  
         try{  
             out.println("<img src=\"..\\validateImage\"/>");  
         }catch (IOException ioe1){  
             ioe1.printStackTrace();  
         }  
                  
        return EVAL_BODY_INCLUDE  ;  
    }  
  
    public String getContextPath( HttpServletRequest req ) {  
          String servletPath = req.getServletPath();  
          ServletContext servletContext = pageContext.getServletContext();  
          String realPath = servletContext.getRealPath( servletPath );  补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,