JSF组件开发(实现简单的输出)

这是我们的项目的文件位置

 




 
 1、开发我们的JSF组件

      编写UIOutput类型的组件Java类

import java.io.IOException;

import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

public class UIBolder extends UIOutput {
  
   
  public void encodeBegin (FacesContext context) throws IOException {
    // FIXME: Open the HTML tag for bold text
	  ResponseWriter writer = context.getResponseWriter();
	  writer.startElement("h2", this);

	  
  }

  public void encodeEnd (FacesContext context) throws IOException {
    // FIXME: Close the HTML tag for bold text
	  context.getResponseWriter().endElement("h2");
  }
}

 faces-config.xml中注册我们组件类

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
	<!-- Custom components: FIXME: Register the new custom component -->
	<!-- View and data beans: -->
	<!-- 注册组件 -->
	<component>
		<component-type>bold</component-type>
		<component-class>training.UIBolder</component-class>
	</component>

	<!-- Navigation rules: -->
	<navigation-rule>
		<from-view-id>*</from-view-id>
		<navigation-case>
			<from-outcome>show</from-outcome>
			<to-view-id>/jsp/View.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>
	<!-- Application configuration: -->
	<application>
		<locale-config>
			<default-locale>en</default-locale>
		</locale-config>
	</application>
</faces-config>

 2、开发自定义标签

   编写标签Java类

import javax.faces.webapp.UIComponentELTag;

public class BolderTag extends UIComponentELTag {

  @Override
  public String getComponentType () {
	  //返回faces-config.xml中组件的名字
    return "bold";
  }

  @Override
  public String getRendererType () {
    
    return null;
  }

}

 在bolder.tld文件中定义标签

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
	<tlib-version>1.0</tlib-version>
	<short-name>bold</short-name>
       <!-- 定义页面使用的标签 -->         
       <uri>http://www.snailteam.org/</uri>
	<tag>
		<name>uibold</name>
		<tag-class>training.BolderTag</tag-class>
		<body-content>JSP</body-content>
	</tag>
</taglib>
 

3 、使用我们的自定义组件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://www.snailteam.com/"  prefix="d"%>


<html>

<head>
<title>Lab JSF Component</title>
<link rel="stylesheet" href="fred.css" type="text/css" />
</head>

<body>

	<f:view>

		<p>Fred says, show how to use custom component!</p>

		<h:form id="main">
			<h:panelGrid id="grid" columns="2">
				<f:verbatim>New component:</f:verbatim>
				<!-- FIXME: Use the new custom tag to display the following text in bold font -->
			   <!-- 使用tld中的tag name -->
			   <d:uibold>This my Bolder font</d:uibold>
			     
				<f:verbatim>JSF bold component</f:verbatim>
			</h:panelGrid>
		</h:form>

	</f:view>

</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值