먼저이 점을 변환하는 방법, 당신은 사용자 정의 태그의 일을 할 수있는 클래스를 생성해야합니다. javax.servlet.jsp.tagext.SimpleTagSupport
에서 상속해야합니다. 원하는 출력이 숨겨진 HTML 입력 태그 인 경우 id
및 name
에 대한 속성을 원할 것입니다. 커스텀 태그의 유저는 이것들을 제공 할 필요가있어, getter 및 setter를 가지는 커스텀 태그 클래스의 필드가됩니다. 또한 사용자는 스프링 빈에서 가져올 속성을 지정하므로 클래스의 필드이기도합니다.
package com.example;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class MyCustomTag extends SimpleTagSupport {
private String name;
private String id;
private String property;
@Override
public void doTag() throws JspException, IOException {
// Overriding doTag() from SimpleTagSupport, you do your Java work.
try {
PageContext pc = (PageContext) getJspContext();
ServletContext sc = pc.getServletContext();
ApplicationContext appCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
ReloadableResourceBundleMessageSource mySpringBean = (ReloadableResourceBundleMessageSource)appCtx.getBean("messageSource");
// use this.property to allow your tag's user to get any message from the bean.
String gridColumnValues = mySpringBean.getMessage(this.property, null, Locale.US);
// write your desired output, in this case a complete hidden html form field
JspWriter out = pc.getOut();
out.print("<input type=\"hidden\" ");
if (this.name != null)
out.print("name=\"" + this.name + "\" ");
if (this.id != null)
out.print("id=\"" + this.id + "\" ");
out.println("value=\"" + gridColumnValues + "\"/>");
} catch (Exception e) {
throw new JspException(e);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
다음으로 tld 파일 /WEB-INF/tld/mycustomtag.tld
을 만듭니다. 이 파일은 사용자 정의 태그를 정의하고 위의 클래스를 이름으로 식별합니다.
<?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>2.0</jsp-version>
<short-name>MyCustomTag</short-name>
<uri>http://example.com/mytags</uri>
<tag>
<name>custom</name> <!-- The name is how your users will invoke the tag in a jsp. You can call it anything you want. -->
<tag-class>com.example.MyCustomTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>property</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
jsp에서 이것을 사용하려면 taglib 지시문을 사용하여 사용자 정의 태그 라이브러리를 선언하십시오. 접두사는 태그에서 사용할 XML 네임 스페이스입니다. 이 예에서 나는 그것을 "내"라고 불렀다. TLD는 파일에서 태그의 이름은 "사용자 정의"입니다, 그래서 당신이 이것을 실행하고 HTML 소스를 볼 때, 당신은 당신의 봄의 값을 가진 숨겨진 입력 태그를 볼 수 있습니다 <my:custom/>
<%@ taglib prefix="my" uri="http://example.com/mytags" %>
<my:custom id="myhiddenfield" name="hiddenname" property="propertyfile"/>
호출 할 것이다 빈의 메시지.
출처
2014-10-30 14:56:58
Tap