Get value stacks in custom tags in struts valuestack

Source: Internet
Author: User
Tags string format

First, the author's analysis is to be corrected under the article "Custom-extended struts2 label". In this article, the author says,

Recently in the project to do the Java EE, need to encapsulate a lot of tags, found to inherit directly from Bodytagsupport, unable to obtain Valuestack, and can not borrow struts's internationalization solution. So you need to expand the Struts tab.

Recent research, in fact, even if you inherit from the Bodytagsupport class to implement a custom label class, you can also take the valuestack. , as shown below:

   Valuestack stack = tagutils.getstack (PageContext);

One of the most recently implemented date controls is to do this:


Package Com.jdgm.common.tag;
Import Java.sql.Timestamp;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

Import Java.util.GregorianCalendar;
Import javax.servlet.jsp.JspException;

Import Javax.servlet.jsp.tagext.BodyTagSupport;
Import Org.apache.commons.lang.StringUtils;

Import Org.apache.struts2.views.jsp.TagUtils;
Import Com.jdgm.framework.model.UiItem;
Import Com.jdgm.platform.ConstantsPF;
Import Com.jdgm.platform.common.enums.QueryControlType;
Import Com.jdgm.platform.common.enums.QueryOperatorType;
Import Com.jdgm.platform.common.tag.CommonStrutsTag;
Import Com.jdgm.platform.components.log.LogUtil;
Import Com.opensymphony.xwork2.util.ValueStack;  /** * * @author ZHANGPF 20120801 * Because of the many problems of the previously used Jscalendar, you have to extend your own controls in the current project, using the My97datepicker */public class Calendar
	Extends bodytagsupport{private String id= "";
	Private String name= ""; 
	Private String value= ""; /** * Date format * Month Day: YYYY-MM-DD * Month day minute minute: yyyy-mm-dd HH:mm:ss */private String format= "Yyyy-mm-dd";
	/** * When read-only is true, the picture on the right side of the input box is not displayed, and the Date editing window cannot be opened */private String readonly= "false";
	
 
	Private String width= "150"; @Override public int Doendtag () throws jspexception {try{String content = "<input readonly type= ' text ' id=
			\ "%s\" name=\ "%s\" value= '%s ' style= ' width:%s '%s > \ n%s ';
			Get the value stack, implement the echo function valuestack stack = tagutils.getstack (PageContext);
			String valuestr= "";
					 if (stack!=null) {object Obj=stack.findvalue (name);//The object being fetched, possibly timestamp or string if (Obj!=null) {
					 Convert the obtained value to the user-set format SimpleDateFormat formatter = new SimpleDateFormat (This.getformat ());
					 
					 
					 Date Date=null; 
					 if (obj instanceof Timestamp) date=formatter.parse (((Timestamp) obj). toString ());
					 else if (obj instanceof date) date= (date) obj; 
					 
					 
					 Else Date=formatter.parse (string.valueof (obj));
					 
				 if (date!=null) Valuestr=formatter.format (date); } if (Stringutils.isnotBlank (VALUESTR)) Value=valuestr; } String Out=string.format (content, This.getid (), This.getname (), Value,this.getwidth (), This.getclickaction (),
		This.getimage ());//Pagecontext.getout (). println (out);
		} catch (Exception e) {logutil.error ("failed to generate Calendar control", e);
	} return eval_page; }/** * Click on the implementation of the action JS * for the input box and calendar icon Click event * @return */private String getclickaction () {if (Readonly.touppercase ().				
		Equals ("TRUE")) return "";
	Return "Onclick=\" Wdatepicker ({el: ' "+this.getid () +" ', Datefmt: ' "+this.getformat () +" '}) \ "";  }/** * Output to the right of the calendar icon * @return */private String getImage () {if (Readonly.touppercase () equals ("TRUE")) return	
		 
		""; Return " 


 

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.