JSF1.1+Struts2.1+Spring2.0+Hibernate3.1
图示:
<a target="_blank" href="http://blog.51cto.com/attachment/201012/161935629.jpg"></a>
JSF1.1 Jar包:
<a target="_blank" href="http://blog.51cto.com/attachment/201012/154254361.jpg"></a>
MyFaces1.1 Jar包:
<a target="_blank" href="http://blog.51cto.com/attachment/201012/154536777.jpg"></a>
Struts2.1 Jar包:
<a target="_blank" href="http://img1.51cto.com/attachment/201012/144615926.jpg"></a>
Spring2.0 Jar包:
<a target="_blank" href="http://img1.51cto.com/attachment/201012/040505662.jpg"></a>
Hibernate3.1 Jar包:
官方下载的Hibernate3.1文件夹lib下的全部Jar包
其他必须包:
<a target="_blank" href="http://blog.51cto.com/attachment/201012/162248618.jpg"></a>
web.xml 配置:
<!-- JSF Configuration -->
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<!-- Struts2 Configuration -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-name>struts-cleanup</filter-name>
org.apache.struts2.dispatcher.ActionContextCleanUp
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Spring Configuration -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter-name>CharacterEncoding</filter-name>
org.springframework.web.filter.CharacterEncodingFilter
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
<!-- Sun for JSF Listener -->
com.sun.faces.config.ConfigureListener
<!-- MyFaces for JSF Listener -->
org.apache.myfaces.webapp.StartupServletContextListener
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
faces-config.xml
这里必须要配置Delegation Configuration
<!-- Delegation Configuration -->
<application>
<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
</application>
<!-- Bean Configuration -->
<managed-bean>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>com.jsf.beans.User</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<!-- 注入Service,userService已在Spring中配置 -->
<managed-property>
<property-name>service</property-name>
<value>#{userService}</value>
</managed-property>
</managed-bean>
<!-- Navigation Configuration -->
<navigation-rule>
<from-view-id>/input.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/success.jsp</to-view-id>
</navigation-case>
<from-outcome>error</from-outcome>
<to-view-id>/error.jsp</to-view-id>
</navigation-rule>
struts.xml
<!-- JSF Interceptors -->
<package name="jsf" extends="jsf-default">
<interceptors>
<interceptor-stack name="jsfFullStack">
<interceptor-ref name="params" />
<interceptor-ref name="basicStack" />
<interceptor-ref name="jsfStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="jsfFullStack" />
</package>
<!-- Struts Configuration -->
<package name="main" namespace="/" extends="struts-default">
<action name="login" class="loginAction" method="login">
<result name="input">/input2.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/success.jsp</result>
</action>
applicationContext.xml
这里的Service和Action切记将scope设置为prototype类型,该类型持有状态对象
<!-- Service Configuration -->
<bean name="userService" id="userService" class="com.jsf.service.UserService"
factory-method="getInstance" lazy-init="false" scope="prototype"/>
<!-- Action Configuration -->
<bean id="loginAction" class="com.jsf.beans.User" scope="prototype">
<property name="service"><ref local="userService"/></property>
</bean>
User.java
UserService与SSH1中的一致。
package com.jsf.beans;
import java.io.Serializable;
import com.jsf.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class User extends ActionSupport implements Serializable{
private String id;
private String name;
private String password;
private Integer age;
public User() {
super();
}
public String getId() {
return id;
public void setId(String id) {
this.id = id;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;
public Integer getAge() {
return age;
public void setAge(Integer age) {
this.age = age;
private UserService service;
public UserService getService() {
return service;
public void setService(UserService service) {
this.service = service;
//jsf的执行方法
public String execute() {
if(service.validate(this.id, this.password)) {
User user = service.getUser(this.id);
this.setAge(user.getAge());
this.setName(user.getName());
return "success";
}
return "error";
//struts执行方法
public String login() throws Exception {
ActionContext.getContext().put("userBean", user);
}
input.jsp
纯JSF方式创建页面,如果以JSF方式提交表单,系统抛出异常
>> javax.servlet.ServletException
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>JSSH JSF</title>
</head>
<body>
<f:view>
<h:form>
用户名:<h:inputText value="#{userBean.id}" />
密码:<h:inputSecret value="#{userBean.password}" />
<h:commandButton value="登录" action="#{userBean.execute}"></h:commandButton>
</h:form>
</f:view>
</body>
</html>
input2.jsp
使用JSF与Struts混合标签创建页面,以Struts方式提交表单,系统正确运转
<%@ taglib uri="/struts-tags" prefix="s"%>
<title>JSSH Struts</title>
<f:view>
<s:form action="login" namespace="/">
<s:textfield name="id" label="用户名"/>
<s:password name="password" label="密码"/>
<s:submit value="登录" />
</s:form>
</f:view>
总结:
在web.xml中使用Sun和MyFaces对JSF的监听
配置faces-config.xml中要托管的解析器DelegatingVariableResolver
在struts.xml中建立JSF的拦截器,之后的package应该继承该配置
在页面中可以实现JSF与Struts混编,但是只能以struts方式进行表单提交,如果以JSF方式进行表单提交,将会抛出异常
本文转自 sundunjam 51CTO博客,原文链接:http://blog.51cto.com/sunspot/469073,如需转载请自行联系原作者