jsf2

一,配置JSF

把JSF2.0内的两个开发包jsf-api.jar,jsf-impl.jar拷贝到Eclipse项目的lib目录中(建立web项目略). 在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.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>

<context-param>
<param-name>com.sun.faces.resourceUpdateCheckPeriod</param-name>
<param-value>-1</param-value>
</context-param>

<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>*.do</url-pattern> <!--此处名字可以随便起,可以是*.action,*.faces 等 我习惯用*.do-->
</servlet-mapping>

这样jsf框架已经添加进项目中了.接下来就要建立Bean了.如下所示:

package com.joy.jsf.beans;

//@ManagedBean(name="users") jsf2.0可以不用配置文件来管理bean
public class User {

private String userName;
private String userPassword;
private String errorMessage;

public String getErrorMessage() {
return errorMessage;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserPassword() {
return userPassword;
}

public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

public String Check() {
if (!this.userName.equals("JOY") || !this.userPassword.equals("123456")) {
this.errorMessage = "名称或密码错误";
return "failure";
} else {
return "success";
}
}
}

之后要定制导航规则,新建face-config.xml 如下所示:

<faces-config>
<!-- <application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application> -->
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/welcome.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>

<!--<managed-bean> <managed-bean-name>users</managed-bean-name> <managed-bean-class>
com.joy.jsf.beans.User </managed-bean-class> <managed-bean-scope>request</managed-bean-scope>
</managed-bean> -->
</faces-config>

配置文件建立完了,添加view页面. index.jsp和welcome.jsp

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
<f:view>
<h:form>
姓名:<h:inputText value="#{users.userName }" /><br/>
密码:<h:inputText value="#{users.userPassword }"></h:inputText><br/>
<h:commandButton value="submit" action="#{users.Check}"></h:commandButton>
<br/>
<h:outputText value="#{users.errorMessage}"/><p>

</h:form>
</f:view>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎回来</title>
</head>
<body>
<f:view>
<h:form>
Hello <h:outputText value="#{users.userName }"></h:outputText> ,Welcome to here!

</h:form>
</f:view>
</body>
</html>

保存,部署,运行http://localhost:8080/JSF/index.do

二,整合Spring

添加spring开发包,spring.jar,stadard.jar,commons-logging.jar,jstl.jar

web.xml 添加如下配置

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<description>添加Srping支持</description>
<display-name>Spring</display-name>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

然后在face-config.xml中添加

<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>

从spring工厂中获取bean ,如果和jsf的托管bean一起使用,则托管bean的优先级要高于spring.

添加applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="users" class="com.joy.jsf.beans.User">

</bean>
</beans>

重新部署,运行.

你可能感兴趣的:(jsf2)