Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring with JSF integaration

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 271
    Comment on it

    JavaServer Faces (JSF):- JSF is an MVC web framework which focus on or we can say that it's objective is to built user interfaces for Java web application similar to the JSP(Java Server Pages). The JSF comes with 100+ ready UI tags to build the UI for the web Applications.

    User.java

    package com.evon.user;
    
    public interface User{
    
        public String getMessage();
    
    }
    

    UserImpl.java

    package com.evon.user.impl;
    import javax.inject.Named;
    import org.springframework.stereotype.Service;
    import com.evon.user.User;
    
    @Service
    public class UserImpl implements User{
    
        public String getMessage() {
    
            return "JSF 2 + Spring Integration";
    
        }
    
    }
    

    UserBean.java

    package com.evon;
    
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.inject.Inject;
    import javax.inject.Named;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    import com.evon.user.User;
    
    @Component
    @ManagedBean
    @SessionScoped
    public class UserBean {
    
        @Autowired
        User user;
    
        public void setUser(User user) {
            this.user = user;
        }
    
        public String printMsg() {
            return user.getMessage();
        }
    
    }
    

    faces-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <faces-config xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig&#95;2_1.xsd"
        version="2.1">
    
        <application>
            <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
        </application>
    
        <managed-bean>
            <managed-bean-name>user</managed-bean-name>
            <managed-bean-class>com.evon.UserBean</managed-bean-class>
            <managed-bean-scope>session</managed-bean-scope>
            <managed-property>
                <property-name>user</property-name>
                <value>#{user}</value>
            </managed-property>
        </managed-bean>
    
    </faces-config>
    

    application-context.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
        <context:component-scan base-package="com.evon" />
    
    
    </beans>
    

    default.xhtml

    <?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:h="http://java.sun.com/jsf/html"
          >
    
        <h:body>
    
            <h1>JSF 2.0 Integration with Spring </h1>
    
             #{userBean.printMsg()}
    
        </h:body>
    
    </html>
    

    The #{..} indicate that is JSF expression language(el), in this case, #{userBean.printMsg()}, when we hit url http://localhost:8080/JavaServerFaces/. The JSF will find the userBean and set the value via the setUser() method. When default.xhtml page is displayed JSF will find the same session userBean again and display the name property value via the printMsg() method.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: