基于Struts2的自定义标签编辑

1,建立Tag文件,该文件继承于struts2的基本标签组件,如果查看struts2源码,可以看到该类继承了BodyTagSupport,即是一个标准的JSP标签

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import net.esj.struts.components.TextWrap; 

import org.apache.struts2.components.Component; 
import org.apache.struts2.components.Property; 
import org.apache.struts2.views.annotations.StrutsTag; 
import org.apache.struts2.views.jsp.ComponentTagSupport; 

import com.opensymphony.xwork2.util.ValueStack; 

public class TextWrapTag extends ComponentTagSupport { 

private String value; 

@Override 
public Component getBean(ValueStack stack, HttpServletRequest req, 
HttpServletResponse res) { 
return new TextWrap(stack); 
} 

protected void populateParams() { 
        super.populateParams(); 
        TextWrap tag = (TextWrap) component; 
        tag.setValue(value); 
    } 

public void setValue(String value) { 
this.value = value; 
} 

} 



2,建立解析文件
import java.io.IOException; 
import java.io.Writer; 

import net.esj.basic.utils.view.HtmlUtils; 

import org.apache.log4j.Logger; 
import org.apache.struts2.components.Component; 
import org.apache.struts2.views.annotations.StrutsTag; 
import org.apache.struts2.views.annotations.StrutsTagAttribute; 

import com.opensymphony.xwork2.util.ValueStack; 

@StrutsTag(name="textwrap", tldBodyContent="empty", tldTagClass="net.esj.struts.views.jsp.TextWrapTag", 
    description="Print out expression with word-wrapped") 
public class TextWrap extends Component { 

protected static final Logger logger = Logger.getLogger(TextWrap.class); 

private String value; 

public TextWrap(ValueStack stack) { 
super(stack); 
} 
//获得标签传递的值 如此处:传递的值为key(注意不是key的内容) 
    @StrutsTagAttribute(description="Value to be displayed", type="Object", defaultValue="<top of stack>") 
    public void setValue(String value) { 
        this.value = value; 
    } 

public boolean start(Writer writer) { 
        boolean result = super.start(writer); 

        String actualValue = null; 

        if (value == null) { 
            value = "top"; 
        } 
        else { 
        value = stripExpressionIfAltSyntax(value); 
        } 

//获得该键值对应的内容 
        actualValue = (String) getStack().findValue(value, String.class, throwExceptionOnELFailure); 

//处理该内容并写到writer中 
        try { 
            if (actualValue != null) { 
                writer.write(prepare(actualValue)); 
            } 
        } catch (IOException e) { 
        logger.info("Could not print out value '" + value + "'", e); 
        } 

        return result; 
    } 

private String prepare(String actvalue){ 
String result = HtmlUtils.formateTextarea(actvalue); 
return result; 
} 
} 


3.在WEB-INF下建立tld文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <description><![CDATA["To make it easier to access dynamic data;
                        the Apache Struts framework includes a library of custom tags.
                        The tags interact with the framework's validation and internationalization features;
                        to ensure that input is correct and output is localized.
                        The Struts Tags can be used with JSP FreeMarker or Velocity."]]></description>
  <display-name>"Melon Tags"</display-name>
  <tlib-version>2.0</tlib-version>
  <short-name>m</short-name>
  <uri>/melon-tags</uri>
 
  <!--自定义测试 -->
  <tag>
    <description><![CDATA[Execute an action from within a view]]></description>
    <name>textwrap</name>
    <tag-class>net.esj.struts.views.jsp.TextWrapTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
      <description><![CDATA[Value to be displayed]]></description>
      <name>value</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <dynamic-attributes>false</dynamic-attributes>
  </tag>
</taglib>

在使用struts的框架中这样添加自定义标签就几乎不用去了解jsp的标签定义了,什么pagecontext多麻烦

你可能感兴趣的:(JAVA STRUTS TAG)