2014-01-13 44 views
1

我想通過在類定義之上添加@WebService註釋以及在託管bean方法中添加@WebMethod註釋來製作jsf託管bean也是一項Web服務。如何使jsf @ManagedBean也是@WebService

這不起作用,至少在websphere上不行。

託管bean的方法通過調用注入會話bean的方法來進行一些EJB調用,因此,定義一個新的Web服務類並從其@WebMethods調用JSF託管bean的方法也不起作用。

有什麼建議嗎?

回答

3

我建議你使用託管bean只是保持頁面狀態的方法,並且它將所有事務性的東西委託給一個EJB,所以你可以公開Web服務而不是託管bean方法,但是EJB的。

更新:這適用於我與TomEE + 1.6.0,是你想要什麼?

package somepackage; 
import java.io.Serializable; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.jws.WebMethod; 
import javax.jws.WebService; 

@ManagedBean 
@WebService 
@ViewScoped 
public class ManagedBeanAndWebService implements Serializable{ 

    private static final long serialVersionUID = 4479173603147480764L; 
    private String someAttribute="xyz"; 

    public String getSomeAttribute() { 
     return someAttribute; 
    } 

    public void setSomeAttribute(String someAttribute) { 
     this.someAttribute = someAttribute; 
    } 

    @WebMethod(operationName="methodName") 
    public void someWebMethod(String s){ 
     System.out.println(s); 
    } 
} 

和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" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:pe="http://primefaces.org/ui/extensions"> 
<h:head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title>Test</title> 
</h:head> 
<h:body> 

    <h:form id="form"> 
     <p:outputLabel value="#{managedBeanAndWebService.someAttribute}"/> 
    </h:form> 
</h:body> 
</html> 

和WS

enter image description here

+0

我通常這樣做,但有一個從託管bean中調用外部應用程序的具體情況,並調用者方法也必須通過Web服務訪問。 – fledglingCoder

相關問題