STRUTS2 development of custom tags use follow--Custom Pull-down menu

Source: Internet
Author: User
Tags stringbuffer

The last time the use of custom tags to solve the relevant code into the corresponding Chinese meaning of the requirements, the effect is good, tasted the sweetness of the custom label, ready to be some drop-down menu optimization using a custom tag to achieve.

1, JSP page:

<%@ taglib prefix= "Lsxy" uri= "/lsxy"%>

<lsxy:sel name= "OP2" codeid= "CPT20348" style= "width:180px"/>

2, according to the page custom label, define the label structure file Lsxy.tld placed under Web-inf:

<?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>2.2.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>lsxy</short-name>
<uri>/lsxy</uri>
<display-name> "Lsxy Tags" </display-name>

<!--custom Drop-down menu label-->
<tag>
<name>Sel</name>
<tag-class>lsxy.report.taglib.LsxySelTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>codeID</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>style</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

3, custom class Lsxyseltag.java:

Package lsxy.report.taglib;

Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Lsxy.report.service.LsxySelTagService;
Import org.apache.struts2.components.Component;
Import Org.apache.struts2.views.jsp.ComponentTagSupport;
Import Com.opensymphony.xwork2.util.ValueStack;

public class Lsxyseltag extends Componenttagsupport {

Private static final long serialversionuid = 1L;
private String name;
Private String Codeid;
private String style;

Public String Getcodeid () {
return Codeid;
}

public void Setcodeid (String codeid) {
This.codeid = Codeid;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public String GetStyle () {
return style;
}

public void SetStyle (String style) {
This.style = style;
}

@Override
Public Component Getbean (valuestack valuestack, httpservletrequest request,
HttpServletResponse response) {
return new Lsxyseltagservice (Valuestack);
}

@Override
protected void Populateparams () {
Super.populateparams ();
Lsxyseltagservice service = (lsxyseltagservice) getcomponent ();
Service.setcodeid (Getcodeid ());
Service.setname (GetName ());
Service.setstyle (GetStyle ());
}

}

4, Service processing class Lsxyseltagservice.java:

Package lsxy.report.service;

Import java.io.IOException;
Import Java.io.Writer;
Import Java.util.Iterator;
Import Java.util.Map;
Import Lsxy.report.dao.ReportCodeDao;
Import org.apache.struts2.components.Component;
Import Com.opensymphony.xwork2.util.ValueStack;

public class Lsxyseltagservice extends Component {
private static StringBuffer html;
private String name;
Private String Codeid;
private String style;

Public String Getcodeid () {
return Codeid;
}

public void Setcodeid (String codeid) {
This.codeid = Codeid;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public String GetStyle () {
return style;
}

public void SetStyle (String style) {
This.style = style;
}

Public Lsxyseltagservice (Valuestack stack) {
Super (stack);
}

@Override
Public boolean start (Writer Writer) {
try {
Buildselecttag ();
SYSTEM.OUT.PRINTLN ("HTML:" + html.tostring ());
Writer.write (Html.tostring ());
catch (IOException e) {
E.printstacktrace ();
}
Return Super.start (writer);
}

 /**
  * Build Select
  */
 public void Buildselecttag () {
  html = (new Stri Ngbuffer ("<select name=\")). Append (name). Append ("\")
    .append ("id=\"). Append ( Name). Append ("\");
  if (style!= null) {
   html.append ("style=\"). Append (Style). Append ("\");
& NBSP;&NBSP}
  html.append (">");
  html.append (GetOptions ());
   Html.append ("</select>");
 }

/**
* Build option
*
* @return
*/
@SuppressWarnings ("Unchecked")
Private String getoptions () {
StringBuffer optionbuffer = new StringBuffer ();
Reportcodedao dao = new Reportcodedao ();
Map map = Dao.getcodemap (Codeid);
for (Iterator iterator = Map.keyset (). iterator (); Iterator.hasnext ();) {
String key = (string) iterator.next ();
Optionbuffer.append ("<option value="). Append (Key). Append ("'");
Optionbuffer.append (">"). Append ((String) Map.get (key). Append (
"</option>");
}
return optionbuffer.tostring ();
}
}

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.