JSF笔记
1、单选按钮
<
h:selectOneRadio id
=
"
sel
"
value
=
"
#{HelloBean.selected}
"
>
< f:selectItem id = " red " itemLabel = " red " itemValue = " red " />
< f:selectItem id = " green " itemLabel = " green " itemValue = " green " />
</ h:selectOneRadio >
< f:selectItem id = " red " itemLabel = " red " itemValue = " red " />
< f:selectItem id = " green " itemLabel = " green " itemValue = " green " />
</ h:selectOneRadio >
2、配置本地化
faces
-
config.xml文件中添加
< application >
< locale - config >
< default - locale > zh_CN </ default - locale >
</ locale - config >
</ application >
< application >
< locale - config >
< default - locale > zh_CN </ default - locale >
</ locale - config >
</ application >
3、配置全局错误页面
建立错误页面error.jsp
isErrorPage = " true "
……
<%= exception.getLocalizedMessage() %>
配置web.xml(可以自由配置异常类型 / 另外也可以根据error - code配置)
< error - page >
< exception - type > java.lang.Exception </ exception - type >
< location >/ error.jsp </ location >
</ error - page >
isErrorPage = " true "
……
<%= exception.getLocalizedMessage() %>
配置web.xml(可以自由配置异常类型 / 另外也可以根据error - code配置)
< error - page >
< exception - type > java.lang.Exception </ exception - type >
< location >/ error.jsp </ location >
</ error - page >
4、配置统一的页面
配置所有
"
error
"
都指向同一个页面error.jsp(faces
-
config.xml)
< navigation - rule >
< from - view - id >*</ from - view - id >
< navigation - case >
< from - outcome > error </ from - outcome >
< to - view - id >/ error.jsp </ to - view - id >
</ navigation - case >
</ navigation - rule >
< navigation - rule >
< from - view - id >*</ from - view - id >
< navigation - case >
< from - outcome > error </ from - outcome >
< to - view - id >/ error.jsp </ to - view - id >
</ navigation - case >
</ navigation - rule >
5、request.setAttribute("name")
在JSF中转换成:
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext().getRequestMap().put( " name " , " value " );
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext().getRequestMap().put( " name " , " value " );
6、配置保存视图的状态(javax.faces.STATE_SAVING_METHOD)
Web.xml:
< context - param >
< param - name > javax.faces.STATE_SAVING_METHOD </ param - name >
< param - value > client </ param - value >
</ context - param >
或者保存在服务器上:
< context - param >
< param - name > javax.faces.STATE_SAVING_METHOD </ param - name >
< param - value > server </ param - value >
</ context - param >
一般在开发应用的时候保存在客户端,如果保存在服务器端就无法知道视图是否过期,会造成修改了JSP后,页面之间不一致。
< context - param >
< param - name > javax.faces.STATE_SAVING_METHOD </ param - name >
< param - value > client </ param - value >
</ context - param >
或者保存在服务器上:
< context - param >
< param - name > javax.faces.STATE_SAVING_METHOD </ param - name >
< param - value > server </ param - value >
</ context - param >
一般在开发应用的时候保存在客户端,如果保存在服务器端就无法知道视图是否过期,会造成修改了JSP后,页面之间不一致。
7、使用JSTL
<%@ taglib uri=" http://java.sun.com/jstl/core" prefix="c" %>
8、修改系统自带的错误提示信息
在myfaces-all.jar的 javax.faces.包下面properties文件
9、MyFaces与JSF(SUN)的包会有冲突
有时无法跳转,后来发现原因在于我把Sun的JSF两个包都包括进来了,发生了冲突,其实MyFaces中就已经有了jsf-impl.jar包。
10、在JSF中应用JS
在JSF中应用javascript在提交前做一些客户端的验证或修改DOM动作时,有两种常用方法
1 、在form中添加onsubmit属性
< h:form onsubmit = " return JS() " >
……
</ h:form >
2 、在commandButton中添加onclick属性
< h:commandButton onclick = " return JS() " />
注意:JS在验证失败(即不希望提交时)必须return false ,否则都会提交。
1 、在form中添加onsubmit属性
< h:form onsubmit = " return JS() " >
……
</ h:form >
2 、在commandButton中添加onclick属性
< h:commandButton onclick = " return JS() " />
注意:JS在验证失败(即不希望提交时)必须return false ,否则都会提交。
11、保护/下面的jsp文件不被直接访问
在web.xml中加入:
< security-constraint >
< display-name >
Prevent access to raw JSP pages that are for JSF pages.
</ display-name >
< web-resource-collection >
< web-resource-name > Raw-JSF-JSP-Pages </ web-resource-name >
<!-- Add url-pattern for EACH raw JSP page -->
< url-pattern > /welcome.jsp </ url-pattern >
< url-pattern > /login.jsp </ url-pattern >
</ web-resource-collection >
< auth-constraint >
< description > No roles, so no direct access </ description >
</ auth-constraint >
</ security-constraint >
< security-constraint >
< display-name >
Prevent access to raw JSP pages that are for JSF pages.
</ display-name >
< web-resource-collection >
< web-resource-name > Raw-JSF-JSP-Pages </ web-resource-name >
<!-- Add url-pattern for EACH raw JSP page -->
< url-pattern > /welcome.jsp </ url-pattern >
< url-pattern > /login.jsp </ url-pattern >
</ web-resource-collection >
< auth-constraint >
< description > No roles, so no direct access </ description >
</ auth-constraint >
</ security-constraint >
个人认为,JSF目前技术不够成熟,采用JSF框架有很大的技术风险,当遇到问题时很难找到答案,而且兼容性不好,经常会有莫名奇妙的问题冒出来。而且同时SUN的JSTL和JSF之间的EL都不能很好的融合,期待JSF2.0,期待MyFaces1.2。但是JSF是一个标准,将来一定会变得很成熟。JSF类似.NET,所以JSF会吸收.NET的很多优点,JSF前景甚好。前不久看到在NETBEAN下开发JSF,发现JSF的组件很丰富。
JSF入门: http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=2630
RichFaces Demo: http://livedemo.exadel.com/richfaces-demo/richfaces/tabPanel.jsf
IBM JSF: http://www.ibm.com/developerworks/cn/java/j-jsf3/