1.在eclipse3.0中新建动态web项目jsfTest
把src目录修改为src.java
把webRoot修改为web
2.添加jar包
这里我使用的是jsf2.0版本的
所以需要:
还有一些基本包:
3.然后就是修改web.xml文件了
先加上faces的servlet:
<servlet>
<servlet-name>Faces Servlet</servlet-name><servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param><!--这里是告诉系统默认的页面是xhtml页面 -->
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
4.创建backing bean
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class SubscriberAction implements Serializable {
public SubscriberAction() {
super();
// TODO Auto-generated constructor stub
}
public String userName;
public String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String checkLogin() {
return "pages/aaa.xhtml";
}
}
5.创建国际化实现
在src.java的目录下新建一个文件夹resource.Config
新建三个文件messageUI_en_US.properties
messageUI_zh_CN.properties
messageUI.properties
6.新建xhtml页面
这里我用的是html的模板
<!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">
<head>
<title><ui:insert name="title">Default title</ui:insert></title>
</head>
<body>
<div id="header">
<ui:insert name="header">
Header area. See comments below this line in the source.
<!-- include your header file or uncomment the include below and create header.xhtml in this directory -->
<!-- <ui:include src="header.xhtml"/> -->
</ui:insert>
</div>
<div id="content">
<ui:insert name="content">
<f:loadBundle basename="resource.Config.messageUI" var="msg"/>
Content area. See comments below this line in the source.
<!-- include your content file or uncomment the include below and create content.xhtml in this directory -->
<!-- <div> -->
<!-- <ui:include src="content.xhtml"/> -->
<!-- </div> -->
<h:form>
<h:panelGrid columns="2">
#{msg.nameText}<h:inputText value="#{subscriberAction.userName}"/>
#{msg.passText}<h:inputText value="#{subscriberAction.password}"/>
</h:panelGrid>
<h:commandButton value="#{msg.commandText}" action="#{subscriberAction.checkLogin}"/>
</h:form>
</ui:insert>
</div>
<div id="footer">
<ui:insert name="footer">
Footer area. See comments below this line in the source.
<!-- include your header file or uncomment the include below and create footer.xhtml in this directory -->
<!--<ui:include src="footer.xhtml"/> -->
</ui:insert>
</div>
</body>
</html>
7.效果如下:
只是做的一个简单的小例子 不要见笑哦