JSF页面组件化
页面化能带来很多优点,当多个页面引用同一个页面,这样,我们就只需要维护一个页面的代码,大大减轻开发人员的工作量。
假设网站的根目录为jsfapp,建立jsfapp/resources/echo目录,resources为默认的需要组件化的页面的根目录,echo为组件的前缀。
第一步
新增jsfapp/resources/echo/echoinputtext.xhtml文件,内容如下:
[html] 】
<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com//jstl/core"
<strong>xmlns:composite="http://java.sun.com/jsf/composite"</strong>>
<h:head>
<title>myjsf</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</h:head>
<!-- interface -->
<composite:interface>
<composite:attribute name="echo" />
</composite:interface>
<!-- implementation -->
<composite:implementation>
<p style="padding: 20px;">
<span>请输入响应文字:</span>
<h:inputtext value="#{cc.attrs.echo.echotext}"></h:inputtext>
</p>
</composite:implementation>
</html>
<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:composite="http://java.sun.com/jsf/composite">
<h:head>
<title>myjsf</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</h:head>
<!-- interface -->
<composite:interface>
<composite:attribute name="echo" />
</composite:interface>
<!-- implementation -->
<composite:implementation>
<p style="padding: 20px;">
<span>请输入响应文字:</span>
<h:inputtext value="#{cc.attrs.echo.echotext}"></h:inputtext>
</p>
</composite:implementation>
</html>注意[html] view plaincopyprint?<strong>xmlns:composite="http://java.sun.com/jsf/composite"</strong>
xmlns:composite=""声明页面组件必须用到composite名字空间。
[html]
<composite:interface>
<composite:interface>传入的参数。
[html]
<composite:implementation>
<composite:implementation>真正组件中的内容。
第二步
新增jsfapp/echo2.xhtml文件:
[html]
<?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">
<html xmlns=""
xmlns:ui=""
xmlns:h=""
xmlns:f=""
xmlns:c=""
xmlns:echo="">
<h:head>
<title>myjsf</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</h:head>
<body>
<p id="header"
style="padding: 100px 0 20px 100px; font-size: 22px; font-weight: bold; border-bottom: solid 1px red">
这个应声器的作者是:#{inputecho.encoder.author}。</p>
<p>
<h:form id="form" style="padding:20px 0 20px 100px">
<echo:echoinputtext echo="#{inputecho}"></echo:echoinputtext>
<p style="padding-left: 50px">
<h:commandbutton id="post" style="padding:3px;width:100px;"
value="提交响应" action="echo2" />
</p>
</h:form>
</p>
<h:panelgroup id="after">
<p style="padding: 20px 0 0 100px">
响应:
<h:outputtext id="echo" value="#{inputecho.outtext}" escape="false"></h:outputtext>
</p>
</h:panelgroup>
</body>
</html>
<?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">
<html xmlns=""
xmlns:ui=""
xmlns:h=""
xmlns:f=""
xmlns:c=""
xmlns:echo="">
<h:head>
<title>myjsf</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</h:head>
<body>
<p id="header"
style="padding: 100px 0 20px 100px; font-size: 22px; font-weight: bold; border-bottom: solid 1px red">
这个应声器的作者是:#{inputecho.encoder.author}。</p>
<p>
<h:form id="form" style="padding:20px 0 20px 100px">
<echo:echoinputtext echo="#{inputecho}"></echo:echoinputtext>
<p style="padding-left: 50px">
<h:commandbutton id="post" style="padding:3px;width:100px;"
value="提交响应" action="echo2" />
</p>
</h:form>
</p>
<h:panelgroup id="after">
<p style="padding: 20px 0 0 100px">
响应:
<h:outputtext id="echo" value="#{inputecho.outtext}" escape="false"></h:outputtext>
</p>
</h:panelgroup>
</body>
</html>
使用echo前缀
[html]
xmlns:echo="http://java.sun.com/jsf/composite/echo"
xmlns:echo="http://java.sun.com/jsf/composite/echo"
使用echoinputtext页面,并传入echo参数
[html]
<echo:echoinputtext echo="#{inputecho}"></echo:echoinputtext>
<echo:echoinputtext echo="#{inputecho}"></echo:echoinputtext>
bean及工具类如下:
[java]
@managedbean(name="inputecho")
@sessionscoped
public class inputecho implements serializable {
/**
*
*/
private static final long serialversionuid = 7599540489637124942l;
private string echotext = "";
private echoencoder encoder;
public inputecho() {
encoder = new echoencoder();
}
/**
* @return the encoder
*/
public echoencoder getencoder() {
return encoder;
}
/**
* @return the echotext
*/
public string getechotext() {
return echotext;
}
public string getouttext() {
return encoder.encode(echotext);
}
/**
* @param echotext the echotext to set
*/
public void setechotext(string echotext) {
this.echotext = echotext;
}
public void echo() {
}
}
@managedbean(name="inputecho")
@sessionscoped
public class inputecho implements serializable {
/**
*
*/
private static final long serialversionuid = 7599540489637124942l;
private string echotext = "";
private echoencoder encoder;
public inputecho() {
encoder = new echoencoder();
}
/**
* @return the encoder
*/
public echoencoder getencoder() {
return encoder;
}
/**
* @return the echotext
*/
public string getechotext() {
return echotext;
}
public string getouttext() {
return encoder.encode(echotext);
}
/**
* @param echotext the echotext to set
*/
public void setechotext(string echotext) {
this.echotext = echotext;
}
public void echo() {
}
}
[java]
package com.zolcorp.myjsf.bean;
import java.io.serializable;
public class echoencoder implements serializable {
/**
*
*/
private static final long serialversionuid = -5578888194809870790l;
private string author = "pout";
/**
* @return the author
*/
public string getauthor() {
return author;
}
public string encode(string msg) {
if(msg == null)
return null;
return msg.replaceall("<", "<").replaceall(">", ">").replaceall("\n", "<br/>");
}
}
package com.zolcorp.myjsf.bean;
import java.io.serializable;
public class echoencoder implements serializable {
/**
*
*/
private static final long serialversionuid = -5578888194809870790l;
private string author = "pout";
/**
* @return the author
*/
public string getauthor() {
return author;
}
public string encode(string msg) {
if(msg == null)
return null;
return msg.replaceall("<", "<").replaceall(">", ">").replaceall("\n", "<br/>");
}
}
页面组件化能大大减少维护的工作量。jsf2里配置也很简单。
上一篇: 如何渲染数据
下一篇: python线程入门