JSF整合Spring时各文件的配置

最近在做一个需要JSF和Spring整合的项目,之前不知道如何在后台管理Bean获取被Spring管理的业务类对象,现在解决了,可以跑起来了!

  1. web.xml
     1  < ?xml version = " 1.0 "? >
     2  < web - app xmlns = "http: // java.sun.com / xml / ns / j2ee"
     3    xmlns:xsi = "http: // www.w3.org / 2001 / XMLSchema - instance"
     4    xsi:schemaLocation = "http: // java.sun.com / xml / ns / j2ee
     5      http: // java.sun.com / xml / ns / j2ee / web - app_2_4.xsd"
     6    version = " 2.4 " >
     7   
     8     < display - name > example </ display - name >
     9 
    10       <context-param>    
    11         <param-name>contextConfigLocation</param-name>
    12         <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    13     </context-param>
    14 
    15     <listener>
    16       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    17     </listener>

    18 
    19       < servlet >
    20           < display - name > FacesServlet </ display - name >
    21           < servlet - name > FacesServlet </ servlet - name >
    22           < servlet - class > javax.faces.webapp.FacesServlet </ servlet - class >
    23           < load - on - startup > 0 </ load - on - startup >
    24      </ servlet >
    25 
    26      < servlet - mapping >
    27         < servlet - name > FacesServlet </ servlet - name >
    28         < url - pattern > *.faces </url-pattern>
    29      </servlet-mapping>
    30       
    31  </web-app>

  2. faces-config.xml
     1  < ?xml version = ' 1.0 '  encoding = ' UTF-8 ' ? >
     2  < !DOCTYPE faces - config  PUBLIC
     3  " -// Sun Microsystems, Inc. // DTD JavaServer Faces Config  1.1 // EN"
     4  "http: // java.sun.com / dtd / web - facesconfig_1_1.dtd" >
     5  < faces - config >
     6     <application>
     7         <variable-resolver>
     8             org.springframework.web.jsf.DelegatingVariableResolver
     9         </variable-resolver>
    10     </application>


    12  < managed - bean >
    13     < managed - bean - name > infoBean </ managed - bean - name >
    14     < managed - bean - class >
    15        com.jam.managedbeans.InfoBean
    16     </ managed - bean - class >  
    17     < managed - bean - scope > request </ managed - bean - scope >
    18  </ managed - bean >
    19 
    20  </ faces - config >


  3. InfoBean.java
    假如InfoBean中需要使用业务类UserService的方法,而UserService交由Spring管理(id="userService"),InfoBean可以这样写:
    package com.jam.managedbeans ;

    import  java.util.Map;

    import  javax.faces.context.ExternalContext;
    import  javax.faces.context.FacesContext;

    import  org.springframework.context.ApplicationContext;
    import  org.springframework.web.jsf.FacesContextUtils;

    import  com.jam.hibeinate.po.Bloguser;
    import  com.jam.service.UserService;

    /**
     * 
    @author Jam Chan
     * 
     
    */
    public   final   class InfoBean extends  Object {

        
    /**
         * 用户名
         
    */
        
    private  String userName;
        
    /**
         * 业务类
         
    */
        
    private  UserService userService;
        
    /**
         * 密码
         
    */
        
    private  String password;

        
    /**
         * 登陆验证
         * 
         * 
    @return
         
    */
        
    public  String loginValidate() {
            FacesContext facesContext 
    =  FacesContext.getCurrentInstance();
            ExternalContext extnContext 
    =  facesContext.getExternalContext();
            // POJO类
            Bloguser objUser 
    =   new  Bloguser();
            objUser.setUsername(getUserName());
            objUser.setPassword(getPassword1());
            
    //  从Spring中获取需要的业务类UserService,我就是在这里被卡住,终于OK了
            ApplicationContext appContext = FacesContextUtils.getWebApplicationContext(facesContext);
            userService 
    = (UserService) appContext.getBean("userService"
    );

            
    //  验证用户
             boolean  result  =  userService.loginValidate(objUser);
            
    //  获取SessionMap
            Map sessionMap  =  extnContext.getSessionMap();
            
    if  (result) {  //  登陆成功
                 /*  把用户名设置在Session中  */
                sessionMap.put(
    " userName " , getUserName());
                
    return   " success " ;
            } 
    else  {  //  登录失败
                 /*  清空session  */
                
    if  (sessionMap.containsKey( " userName " )) {
                    sessionMap.remove(
    " userName " );
                }
                
    return   " failed " ;
            }
        }
        // setter getter省略
    暂时写到这,可能还要查漏补缺,发现问题继续加上!
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值