未调用managedbean后构造



我通过这样做来学习,这可能是一个愚蠢的问题,但我找不到任何答案。

我有一个JSF应用程序,它可以很好地与简单的JDBC一起使用。

以"domain.xhtml"为例,它有一个表,列出了来自"DomainController"bean的元素。这一切都很好,然后我们切换到JPA。该控制器必须使用服务,所以它被声明为@Component,并从那里包含(@Autowired)服务。它也工作得很好,除了我所有的JSF注入(@ManagedProperty)不再被注入,我的@PostConstruct不再被调用

是我错过了什么,还是这种方式有问题?

@ManagedBean
@Component
public class DomainController implements Serializable {
    private static Logger log = Logger.getLogger(DomainController.class);
    private static final long serialVersionUID = -2862060884914941992L;
    private List<Domain> allItems;
    private Domain[] selectedItem;
    private SelectItem[] yesNoNull;
    private DomainFilter filter = new DomainFilter();
    @Autowired
    private DomainService domainService;
    @Autowired
    private ValidationLookUpService validationLookUpService;
    @Autowired
    private ValidationService validationService;
    @ManagedProperty("#{workspace.on}")
    private boolean wsOn;
    //  @ManagedProperty("#{libraryVersionController.selectedItem.id}")
    //  private Integer selectedLibVersionID;
    @ManagedProperty("#{libraryVersionController.selectedItem}")
    private LibraryVersion selectedLibVersion;
    @ManagedProperty("#{obsoleteEntry}")
    private PObsoleteEntry pObsoleteEntry;
    @ManagedProperty("#{validationFailedItemsController}")
    private ValidationFailedItemsController validationFailedCont;
    private Domain itemEdited;
    private boolean persisted = false;
    public DomainController() {
        log.info("Creating metadata controller");
        allItems = new ArrayList<Domain>();
        // model for a yes/no/null column filtering
        yesNoNull = new SelectItem[4];
        yesNoNull[0] = new SelectItem("", "All");
        yesNoNull[1] = new SelectItem("true", "yes");
        yesNoNull[2] = new SelectItem("false", "no");
        yesNoNull[3] = new SelectItem("null", "not yet validated");

    }
    @PostConstruct
    public void test() 
    {
        log.info("!!!");
        log.info("WS is ... "+wsOn);
        // NOT CALLED ANYMORE
    }
...

My web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">
    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name> <!-- indique le fichier de configuration pour Spring -->
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param> <!-- to really skip comments in xhtml pages -->
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>
    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/app/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/spring/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

    <listener> <!-- Creates the Spring Container shared by all Servlets and Filters -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener> <!-- links JSF with spring -->
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener> <!-- parses JSF configuration -->
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>
    <listener> <!-- vide le cache d’introspection Spring à l’arrêt du serveur. Ce listener n’est pas obligatoire mais conseillé -->
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>


    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
</web-app>

谢谢!

如果我错了请纠正我,但我不知道我怎么能选择一个,因为Spring管理后端而JSF(+primefaces)管理前端。

我认为控制器"可能"是两者之间的接口,这就是为什么我天真地把它们混在一起。

在对您的评论进行了一些测试后,我使我的控制器只使用JSF,它使用@ManagedBean注入服务(我也不知道使用@ManagedBean它可以注入@Service spring管理的bean),以便回答我的问题:)

下面是正确的代码。

也谢谢你给我指明了正确的方向!

h2控制器

/**
 * This is the controller for a Domain
 * 
 */
@ManagedBean
public class DomainController implements Serializable {
    private static Logger log = Logger.getLogger(DomainController.class);
    private static final long serialVersionUID = -2862060884914941992L;
    private List<Domain> allItems;
    private Domain[] selectedItem;
    private SelectItem[] yesNoNull;
    private DomainFilter filter = new DomainFilter();
    @ManagedProperty(value="#{domainService}")
    private DomainService domainService;
    @ManagedProperty("#{workspace.on}")
    private boolean wsOn;
    @ManagedProperty("#{libraryVersionController.selectedItem}")
    private LibraryVersion selectedLibVersion;
    private Domain itemEdited;
    private boolean persisted = false;
    /**
     * creates a list populated from the database
     */
    public DomainController() {
        log.info("Creating metadata controller");
        allItems = new ArrayList<Domain>();
        // model for a yes/no/null column filtering
        yesNoNull = new SelectItem[4];
        yesNoNull[0] = new SelectItem("", "All");
        yesNoNull[1] = new SelectItem("true", "yes");
        yesNoNull[2] = new SelectItem("false", "no");
        yesNoNull[3] = new SelectItem("null", "not yet validated");

    }
    @PostConstruct
    public void test()
    {
        Domain d = new Domain();
        d.setDataset("will it work ?"); // yes
        try {
            domainService.saveOrUpdate(d);
        } catch (DataModelConsistencyException e) {
            e.printStackTrace();
        }
    }
    // all functions 
    public DomainService getDomainService() {
        return domainService;
    }
    public void setDomainService(DomainService domainService) {
        this.domainService = domainService;
    }

}

服务
public interface DomainService extends IVersionedServiceBase<Domain> {
    public Domain saveOrUpdate(Domain d) throws DataModelConsistencyException;
    public Domain getRelatedVariables(Domain d, VersionedObjectFilter versionedObjectFilter) throws DataModelConsistencyException;
    StringAndError getVarNameAndKeyOrderForDomain(Domain d, VersionedObjectFilter versionedObjectFilter) throws DataModelConsistencyException;
}

服务的实现

@Service("domainService")
@Transactional(readOnly = true)
public class DomainServiceImpl extends VersionedServiceBase<Domain> implements DomainService {
    /**
     * Private logger for this class
     */
    @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(DomainServiceImpl.class.getName());
    @Autowired
    private DomainDao domainDao;
    @Autowired
    private VariableDao variableDao;
    @Autowired
    private DomainPurposeDao domainPurposeDao;
    @Autowired
    private DomainClassDao domainClassDao;
etc.

相关内容

  • 没有找到相关文章

最新更新