Custom-Extend the Struts2 label

Source: Internet
Author: User
Tags extend locale require


Recently in doing Java EE project, need to encapsulate a lot of tags, found directly from the Bodytagsupport inherited words, can't get valuestack, also can't borrow struts internationalization solution. So you need to extend the label of struts.

See a lot of online extension methods, feel can only be used as a reference or example, but it is not practical. Simply have to use Componenttagsupport to do a package.

Here are some introductions about Componenttagsupport:

When implementing a custom label in struts2.x, the 2 classes inherited are Org.apache.struts2.views.jsp.ComponentTagSupport and Org.apache.struts2.components.Component. Componenttagsupport: is actually an encapsulation of bodytagsupport, inheriting componenttags The Upport class is designed to get the property values set in the user-defined tags in the JSP page and wrap them as component objects. Component:
The component class is inherited to obtain the corresponding value from the Valuestack in the Struts2. The following gives me the source code structure, I will not say. A friend who needs it naturally understands.

Package Com.jdgm.platform.common.tag;
Import Java.io.Writer;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Locale;

Import Java.util.ResourceBundle;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import javax.servlet.jsp.JspException;
Import Org.apache.commons.beanutils.PropertyUtils;
Import Org.apache.commons.io.IOUtils;
Import Org.apache.commons.lang.StringUtils;
Import org.apache.struts2.components.Component;

Import Org.apache.struts2.views.jsp.ComponentTagSupport;
Import Com.jdgm.framework.utils.BeanUtils;
Import Com.jdgm.framework.utils.ResourceUtil;
Import Com.jdgm.framework.utils.Utils;
Import Com.jdgm.platform.ConstantsPF;
Import Com.jdgm.platform.components.log.LogUtil;
Import Com.opensymphony.xwork2.util.LocalizedTextUtil;


Import Com.opensymphony.xwork2.util.ValueStack; The/** * * Struts tab extension inherits from here * compared to the expansion from Bodytagsupport, it has the following advantages: * 1 Internationalization Solution: Commonstrutstag subclasses can enjoy the same international solution * 2) value as Struts's own label Stack: commonstrutsTag's subclass, you can get valuestack instances, but also through the class properties <valueproperty> * To do with the action of the corresponding properties of two-way binding, that is, value submission and value fill < specific See the description of the property Valueproperty > * * * @author ZHANGPF * */Public abstract class Commonstrutstag extends Componenttagsupport {/** * website's ur
	 L Address, System assigned value.
	
	* * protected String CTX;

	protected String ID;
	
	protected String templatefile;
	
	protected String Showdiv;
	Public String Getshowdiv () {return showdiv;
	} public void Setshowdiv (String showdiv) {this.showdiv = Showdiv;
	/** * All Attributes list * * * protected list<string> propertys = new arraylist<string> ();
		{Propertys.add ("CTX");
		Propertys.add ("id");

	Propertys.add ("Showdiv");

	/** * A set of attributes that require special handling in the word/protected list<string> Specproperty = new arraylist<string> ();
	
	/** * URL-related attributes */protected list<string> Urlproperty = new arraylist<string> ();  /** * Related properties that require value from Valuestack <br/> * For example: The control has a property: Titlefield, the value is "Model.title" <br/> * Corresponding Control source code is:< input NamE= "%{titlefield}"/> <br/> * This completes the one-way binding with the attribute Model.title in action, that is, when submitting a form <br/> * The value of input is automatically populated with the corresponding attribute in the action. However, it is not possible to populate the input control with the value <br/> * of the property in the action. To do this, you can modify the control source code for:<br/> * < input name= "%{titlefield}" value= "%{title}"/><br/> * Meanwhile, in the tag subclass, define a property t Itle and add it to valueproperty,<br/> * then in init (), assign the title to: "Model.title", which completes the reverse binding of the Action property value to the input control <br/ > * that is, when the JSP page is opened via an action address, if the action Model.title is not empty, the value of the property is automatically assigned to the control <br/>/protected list<string> ValuePRO
	
	
	
	Perty = new arraylist<string> ();
		@Override public Component Getbean (Valuestack arg0, HttpServletRequest arg1, HttpServletResponse arg2) {init ();
		
		Commonstrutscomponent cmp=new commonstrutscomponent (arg0);
	return CMP;
		  
		  } @Override protected void Populateparams () {super.populateparams ();
		  Commonstrutscomponent cmp= (commonstrutscomponent) component;
	Cmp.sethtmlcontent (ToHTML ()); /** * This method is used to preprocess each property in the label before the label is constructed * main convenient subclassExtended */protected void init () {} final protected String ToHTML () {ctx=constantspf.url_website; try {StringBuilder string = new StringBuilder (ioutils.tostring (CommonTag.class.getResourceAsStream (templatefile)
			
			, "UTF-8")); for (String pro:propertys) {if (Specproperty.contains (PRO) | | Valueproperty.contains (PRO) | |
				
				Urlproperty.contains (PRO)) continue;
				String regex = "%{" + Pro + "}";
					while (String.IndexOf (regex)!=-1) {int index = String.IndexOf (regex);
					String Provalue = string.valueof (Propertyutils.getproperty (This,pro));
						
					if (Stringutils.isnotblank (Provalue)) {string.replace (index, index + regex.length (), provalue);
						else {provalue = "";
					String.Replace (index, index + regex.length (), provalue);
			}} dealurlproperty (String);
			Dealvalueproperty (string);
			Dealspecproperty (string);	

		return string.tostring ();
		catch (Exception e) {	Logutil.error ("Build control Failed", e);
	 Return ""; /** * URL-related processing * @param string * @throws Exception/protected void Dealurlproperty (Strin
 			
			Gbuilder string) throws Exception {for (string pro:urlproperty) {string regex = "%{" + Pro + "}";
				while (String.IndexOf (regex)!=-1) {int index = String.IndexOf (regex);
				String Provalue = string.valueof (Propertyutils.getproperty (This,pro));
					if (Stringutils.isnotblank (Provalue)) {Provalue = ctx + provalue;
	
				String.Replace (index, index + regex.length (), provalue);
					else {provalue = "";
				String.Replace (index, index + regex.length (), provalue);  {}}}/** * related properties requiring value from Valuestack/protected void Dealvalueproperty (StringBuilder string) throws
			Exception {for (string pro:valueproperty) {string regex = "%{" + Pro + "}";
				while (String.IndexOf (regex)!=-1) {int index = String.IndexOf (regex); StRing field = string.valueof (Propertyutils.getproperty (This,pro));
					if (Stringutils.isnotblank (field)) {String fieldvalue=this.getstack (). findstring (field);
						if (fieldvalue==null) fieldvalue= "";
				String.Replace (index, index + regex.length (), fieldvalue);
				else {string.replace (index, index + regex.length (), ""); 
		
	/** * Handles special properties, primarily for subclass extensions/protected void Dealspecproperty (StringBuilder string) throws exception{
	Public String GetId () {return id;
	public void SetId (String id) {this.id = ID;
	Public String Getctx () {return ctx;
	} public void Setctx (String ctx1) {ctx = ctx1;

	} class Commonstrutscomponent extends Component {private String htmlcontent;
	Public String gethtmlcontent () {return htmlcontent;
	} public void Sethtmlcontent (String htmlcontent) {this.htmlcontent = htmlcontent;
		
	Public commonstrutscomponent (Valuestack stack) {super (stack); } public BoOlean Start (Writer Writer) {Boolean result = Super.start (Writer);
		 try {writer.write (htmlcontent);
	    catch (Exception ex) {ex.printstacktrace ();
	return result; }
}

For the above Commonstrutstag class to use, the following gives a simple example, after looking, you will feel because Commonstrutstag, write the custom label, very simple, seconds of things. Ha ha

Demotag.java

Package Com.jdgm.platform.common.tag;
Import Java.util.Locale;

Import Java.util.ResourceBundle;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import javax.servlet.jsp.JspException;
Import Org.apache.commons.beanutils.PropertyUtils;
Import Org.apache.commons.lang.StringUtils;
Import org.apache.struts2.components.Component;

Import Org.apache.struts2.views.jsp.ComponentTagSupport;
Import Com.jdgm.framework.utils.BeanUtils;
Import Com.jdgm.framework.utils.ResourceUtil;
Import Com.jdgm.framework.utils.Utils;
Import Com.opensymphony.xwork2.util.LocalizedTextUtil;


Import Com.opensymphony.xwork2.util.ValueStack; /** * * Tab CONTROL * @author ZHANGPF */public class Demotag extends Commonstrutstag {{templatefile= ' Demotag.
	TXT ";
		} {propertys.add ("field");
		
	Valueproperty.add ("value");
	Private String field;
	private String value;
	Public String GetField () {return field; public void SetField (String field) {thIs.field = field;
	Public String GetValue () {return value;
	public void SetValue (String value) {this.value = value;
 }

}


Demotag.txt:

<input type= "text" Name= "%{field}" value= "%{value}"  />

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.