1

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

我有一个与简单 JDBC 一起使用的 JSF 应用程序。

以“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
    }

...

我的 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>

谢谢 !

4

1 回答 1

1

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

我认为控制器“可能”是两者之间的接口,这就是我天真地混合它们的原因。

在对您的评论进行了一些测试之后,我让我的控制器只使用 JSF 并使用 @ManagedBean 注入服务(我也不知道使用 @ManagedBean 它可以注入 @Service Spring-managed bean),这样就回答了我的问题: )

以下是更正后的代码工作。

也感谢您将我重定向到正确的方向!

控制器

/**
 * 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.
于 2013-01-06T20:15:25.907 回答