2017-09-13 21 views
6

環境:春ブーツJSFの統合

Tomcatの8

春ブーツ1.5

JSF 2.2

はApache MyFacesの

Spring MVCの

コード:

私はSpringBootとJSF 2.2をServlet 3.0環境に統合しています。

コンフィグクラス:

JSFConfig.java - JSFのためのコンフィグ。

@Configuration 
@ComponentScan({"com.atul.jsf"}) 
public class JSFConfig { 

     @Bean 
     public ServletRegistrationBean servletRegistrationBean() { 
      FacesServlet servlet = new FacesServlet(); 
      return new ServletRegistrationBean(servlet, "*.jsf"); 
     } 

} 

春ブーツメインクラス:

@SpringBootApplication 
@Import({ // @formatter:off 
    JPAConfig.class, 
    ServiceConfig.class, // this contains UserServiceImpl.java class. 
    WebConfig.class, 
    JSFConfig.class, 
}) 
public class SpringbootJpaApplication extends SpringBootServletInitializer{ 

    public static void main(String[] args) { 
     SpringApplication.run(SpringbootJpaApplication.class, args); 
    } 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
      return application.sources(SpringbootJpaApplication.class); 
    } 
} 

マネージドBean:

UserBean.java - JSFのためのマネージドBean

@ManagedBean 
@SessionScoped 
public class UserBean implements Serializable{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private String name; 
    @ManagedProperty(value="#{userServiceImpl}") 
    private UserServiceImpl userServiceImpl; 

    public void addUser(){  
     System.out.println("User Gets added "+this.name);  
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
    public UserServiceImpl getUserServiceImpl() { 
     return userServiceImpl; 
    } 

    public void setUserServiceImpl(UserServiceImpl userServiceImpl) { 
     this.userServiceImpl = userServiceImpl; 
    } 
} 

のFacelets:

home.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:f="http://java.sun.com/jsf/core" 
     xmlns:h="http://java.sun.com/jsf/html"> 

    <h:head> 
     <title>JSF 2.0 Hello World</title> 
    </h:head> 
    <h:body> 
     <h2>JSF 2.0 Hello World Example - hello.xhtml</h2> 
     <h:form> 
      <h:inputText value="#{userBean.name}"></h:inputText> 
      <h:commandButton value="Submit" action="#{userBean.addUser}"></h:commandButton> 
     </h:form> 
    </h:body> 
</html> 

面-config.xmlの:

<?xml version="1.0" encoding="UTF-8"?> 
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" 
       version="2.2"> 

    <application> 
     <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
    </application> 
    <lifecycle> 
     <phase-listener>org.springframework.web.jsf.DelegatingPhaseListenerMulticaster</phase-listener> 
    </lifecycle> 

</faces-config> 

問題:

1)Iはhome.xhtml , userBean.addUserでフォームを送信するときに呼び出されます。 2)userBean.nameには、ユーザーが入力した値が設定されます。 3)ただし、userServiceImplはNULLです。 4)これは、SpringとJSFが統合されていないことを意味しますか? I

faces-config.xml 

で述べたようにも私もUserBean.javaからすべてのJSF固有のアノテーションを削除しようとした使用のみ下記のような特定のアノテーションを春SpringBeanFacesELResolverを登録している - 私は、

@Component 
    @SessionScoped 
    public class UserBean implements Serializable{ 

     /** 
     * 
     */ 
     private static final long serialVersionUID = 1L; 
     private String name; 
     @Autowired 
     private UserServiceImpl userServiceImpl; 


    } 

しかし、私はフォームを送信するときターゲットが到達できません#{userBean)の到達不能エラーです。つまり、userBeanは春には検出されません。

5)ここに何か不足していますか?

1.サーブレット登録

はJSFサーブレットを登録し、それを設定します。私は春ブーツ

答えて

4

を提供する組込みTomcatを使用していない 6)これは私がJSF春ブーツで作業している方法です起動時にロードする(web.xmlは不要)。 JSF 2を使用している場合。2は、あなたの最高は、少なくともフェイスレットを使用している場合、*.xhtmlマッピングを使用することです:

@Bean 
public ServletRegistrationBean servletRegistrationBean() { 
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
      new FacesServlet(), "*.xhtml"); 
    servletRegistrationBean.setLoadOnStartup(1); 
    return servletRegistrationBean; 
} 

はあなたのinitパラメータを設定することができるように、コンフィギュレーションクラスがServletContextAwareを実装してください。ここでは、コンフィギュレーションをロードするためにJSFを強制する必要があります。

@Override 
public void setServletContext(ServletContext servletContext) { 
    servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", 
      Boolean.TRUE.toString()); 
    servletContext.setInitParameter("javax.faces.FACELETS_SKIP_COMMENTS", "true"); 
    //More parameters... 
} 

2. EL統合

は顔-config.xmlにEL resolverを宣言します。これはあなたのビューファイルと管理対象Beanプロパティとメソッドの間の接着剤になるだろう:

<?xml version="1.0" encoding="UTF-8"?> 
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" 
    version="2.2"> 

    <application> 
     <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver 
     </el-resolver> 
    </application> 

</faces-config> 

3.ビュースコープ

は、JSFビューのスコープをエミュレートするためにカスタムSpringスコープを書く(中保ちますあなたの豆はJSFではなくSpringによって管理される予定です)。

public class ViewScope implements Scope { 

    @Override 
    public Object get(String name, ObjectFactory<?> objectFactory) { 
     Map<String, Object> viewMap = FacesContext.getCurrentInstance() 
      .getViewRoot().getViewMap(); 
     if (viewMap.containsKey(name)) { 
      return viewMap.get(name); 
     } else { 
      Object object = objectFactory.getObject(); 
      viewMap.put(name, object); 
      return object; 
     } 
    } 

    @Override 
    public String getConversationId() { 
     return null; 
    } 

    @Override 
    public void registerDestructionCallback(String name, Runnable callback) { 

    } 

    @Override 
    public Object remove(String name) { 
     return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name); 
    } 

    @Override 
    public Object resolveContextualObject(String arg0) { 
     return null; 
    } 

} 

そして、あなたの設定クラスに登録:

@Bean 
public static CustomScopeConfigurer viewScope() { 
    CustomScopeConfigurer configurer = new CustomScopeConfigurer(); 
    configurer.setScopes(
      new ImmutableMap.Builder<String, Object>().put("view", new ViewScope()).build()); 
    return configurer; 
} 

4.行くする準備ができてそれはこのようないくつかの方法になります!

ここで、マネージドBeanを以下のように宣言できます。 Spring Beansを扱っているので、@ManagedPropertyの代わりに@Autowired(好ましくはコンストラクタで)を使用することを忘れないでください。それでも私は、JSF固有のアノテーションは、春のブートコンテキストで動作し得ることができなかった

を達成するために保留

@Component 
@Scope("view") 
public class MyBean { 

    //Ready to go! 

} 

。したがって、@FacesValidator,@FacesConverter,@FacesComponentなどは使用できません。それでも、古風なやり方でfaces-config.xml(xsd参照)で宣言することができます。


も参照してください:

+0

エクストリームバイカーあなたはJSF注釈が仕事を得るための方法を見つけましたか?おそらくスプリングブート2.0? –

+0

いいえ、私はこの回答を書いてからあまりにも多くの掘り出し物はありません。ここであなたの豆を管理しているSpringは、@Componentのようにシングルトンを生成するいくつかのアノテーションを試すことができます。 –