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
    • 323
    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

    1. package com.evon.user;
    2.  
    3. public interface User{
    4.  
    5. public String getMessage();
    6.  
    7. }

    UserImpl.java

    1. package com.evon.user.impl;
    2. import javax.inject.Named;
    3. import org.springframework.stereotype.Service;
    4. import com.evon.user.User;
    5.  
    6. @Service
    7. public class UserImpl implements User{
    8.  
    9. public String getMessage() {
    10.  
    11. return "JSF 2 + Spring Integration";
    12.  
    13. }
    14.  
    15. }

    UserBean.java

    1. package com.evon;
    2.  
    3. import javax.faces.bean.ManagedBean;
    4. import javax.faces.bean.SessionScoped;
    5. import javax.inject.Inject;
    6. import javax.inject.Named;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.context.annotation.Scope;
    9. import org.springframework.stereotype.Component;
    10. import com.evon.user.User;
    11.  
    12. @Component
    13. @ManagedBean
    14. @SessionScoped
    15. public class UserBean {
    16.  
    17. @Autowired
    18. User user;
    19.  
    20. public void setUser(User user) {
    21. this.user = user;
    22. }
    23.  
    24. public String printMsg() {
    25. return user.getMessage();
    26. }
    27.  
    28. }

    faces-config.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2.  
    3. <faces-config xmlns="http://java.sun.com/xml/ns/javaee"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig&#95;2_1.xsd"
    6. version="2.1">
    7.  
    8. <application>
    9. <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    10. </application>
    11.  
    12. <managed-bean>
    13. <managed-bean-name>user</managed-bean-name>
    14. <managed-bean-class>com.evon.UserBean</managed-bean-class>
    15. <managed-bean-scope>session</managed-bean-scope>
    16. <managed-property>
    17. <property-name>user</property-name>
    18. <value>#{user}</value>
    19. </managed-property>
    20. </managed-bean>
    21.  
    22. </faces-config>

    application-context.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans
    5. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    6. http://www.springframework.org/schema/context
    7. http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    8.  
    9. <context:component-scan base-package="com.evon" />
    10.  
    11.  
    12. </beans>

    default.xhtml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4. <html xmlns="http://www.w3.org/1999/xhtml"
    5. xmlns:h="http://java.sun.com/jsf/html"
    6. >
    7.  
    8. <h:body>
    9.  
    10. <h1>JSF 2.0 Integration with Spring </h1>
    11.  
    12. #{userBean.printMsg()}
    13.  
    14. </h:body>
    15.  
    16. </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
Reset Password
Fill out the form below and reset your password: