如何将请求参数添加到查询条件中?

时间:2013-06-05 19:42:15

标签: java-ee jsf-2 ejb cdi

我一直在阅读有关JSF2和Weld示波器的所有内容。我想我几乎已经阅读了BalusC关于这个主题的所有内容,但我仍然无法弄清楚这一点。不希望你们认为我没有努力让自己得到这个。

我有一个非常简单的应用程序,包含搜索页面和结果页面。 (真正的应用程序比这个“测试”应用程序更大,有20个参数)有2个RequestScoped bean,一个支持搜索,一个支持结果。有一个EJB接受输入,创建查询并返回匹配实体的列表到结果。

我有1个问题和1个问题。问题是我尝试从搜索到结果获取参数值不起作用。我已经看到这种模式频繁使用,但是我试图让它工作失败了。

问题是......从搜索页面获取参数到EJB的最佳方法是什么,它们将用作查询的一部分。我是不是应该从EJB访问请求参数映射?我已经尝试过并且没有成功 - 不确定这是我正在做的事情,还是这根本不应该是可能的。这个EJB应该是有状态的还是无状态的?

Search.xhtml

<html>

<h:head></h:head>

<h:form>

    <h:panelGrid columns="2">
        <h:outputText value="Hostname" />
        <h:inputText value="#{deviceSearch.hostname}" />
    </h:panelGrid>

    <h:commandButton action="#{deviceSearch.navigate('devices')}"
        value="Devices Page">
        <f:param name="hostname" value="#{deviceSearch.hostname}" />
    </h:commandButton>

    <h:button value="Reset" immediate="true" type="reset" />


</h:form>
</html

Results.xhtml

<html>
<h:head></h:head>

<f:metadata>
    <f:viewParam name="hostname" value="#{deviceResult.hostname}"></f:viewParam>
</f:metadata>

<h:form>
<h:button value="Return to Search" includeViewParams="false" outcome="index" />
</h:form>

<h:dataTable value="#{deviceResult.deviceList}" var="device">
<h:panelGrid columns="2">
    <h:outputText value="Device is #{device.name}" />
    <h:outputText value="dev id is #{device.devId}" />
    <h:outputText value="os is #{device.os}" />
    <h:outputText value="platform is #{device.platform}" />
</h:panelGrid>

</h:dataTable>

</html>

DeviceSearch.java

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class DeviceSearch {


    // Search terms
    String hostname;

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {  
        this.hostname = hostname;
    }

    public String navigate(String className) {

        if (className.equals("devices")) {
            return "result.xhtml?faces-redirect=true";
        }
        return null;
    }

}

DeviceResult.java

@Named
@RequestScoped
public class DeviceResult {

    @Inject DeviceService deviceListService;


    String hostname;

    public void init(){
        deviceList = deviceListService.fetchDevices(hostname);
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }


    // Results
    List<Devices> deviceList = new ArrayList<Devices>();

    public List<Devices> getDeviceList() {
        deviceList = deviceListService.fetchDevices(hostname);
        return deviceList;
    }
}

DeviceService.java

@Stateful
public class DeviceService {

    @PersistenceContext
    EntityManager em;

    public DeviceService() {
    }

    String hostname;
    List<Devices> devicesList = new ArrayList<Devices>();

    @SuppressWarnings("unchecked")
    public List<Devices> fetchDevices(String hostname) {
        Query qry = em.createQuery("SELECT d FROM Devices d WHERE d.name=:name");
        qry.setParameter("name", hostname);
        devicesList = qry.getResultList();
        return devicesList;
    }
}

0 个答案:

没有答案
相关问题