搜索功能什么都不做 [英] Search function not doing anything

查看:84
本文介绍了搜索功能什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这是一个很糟糕的问题,但是这一功能使我发疯了好几天了,所以我想把它贴在这里,看看你们是否可以帮助我

sorry if this is a poor question but this one feature have been driving me mad for days so i thought id post it here to see if you guys can help me

基本上,我想从jsf页面执行的所有操作都是让用户搜索用户,并让我返回所有详细信息

basically all i want to do from a jsf page have the user search a user and for me to return all the details

<h:form id="searchForm">
                    <h:outputLabel value="Search: " style="font-weight:bold" />
                    <h:inputText id="search" value="#{userdetailsController.search}" />   
                    <h:commandButton value="Search" action="index"/>

                </h:form>

那是jsf页面,工作正常

that is the jsf page, working fine

它调用了我的userdetailsController类

it calls my userdetailsController class

@Named("userdetailsController")
@SessionScoped
public class UserdetailsController implements Serializable {

    private Userdetails current;
    private DataModel items = null;
    @EJB
    private Richard.beans.UserdetailsFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;
    private String search;

    public String getSearch() {
        System.out.println("inGetSearch");
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }
......

contactsService类

a contactsService class

@Stateless
public class ContactsService {
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @EJB
    private UserdetailsFacade cf;

    public List<Userdetails> searchByString(String string) {
        return cf.searchByString(string);
    }


    public List<Userdetails> getAllPersons() {
        return cf.findAll();
    }
}

AbstractFacade类

an AbstractFacade class

   /* trying out a search function */
    public List<T> searchByString(String string) {
        System.out.println("in SearchByString");
        return getEntityManager().createNamedQuery("Userdetails.findByUsername").setParameter("string", "%" + string + "%").getResultList();
    }

和Userdetails类以及我要搜索的查询

and the Userdetails class with the query i am trying to search

 @NamedQuery(name = "Userdetails.findByUsername", query = "SELECT u FROM Userdetails u WHERE u.username = :username")})

当前只有吸气剂和设置在Getsearch中有效

currently only the getters and settings are working in Getsearch

我已经花了几天时间来使用这项功能,但是仍然没有关闭,我该如何使它正常工作,对不起,这是我第一次来此

how can i make this work as i have spent days on this feature and are still no closer, sorry this is my first time at this

谢谢大家

编辑

将添加

public List<Userdetails> getAllPersons() {
    if (search == null) {
        return cs.getAllPersons();
    }
    return cs.searchByString(search);
}

在UserdetailsController中足够了吗?

in the UserdetailsController be enough ?

推荐答案

您不在此处执行任何操作:

You're not invoking any action here:

<h:commandButton value="Search" action="index"/>

所以不做任何事情"确实是合乎逻辑的.

So it's indeed logical that it isn't "doing anything".

您需要调用托管bean动作,该动作又执行所需的代码以从数据库中获取所需的数据并分配给属性:

You need to invoke a managed bean action which in turn executes the desired code to obtain the desired data from the DB and assign to a property:

<h:commandButton value="Search" action="#{userdetailsController.submit}" />

内含UserdetailsController:

private String search;
private List<UserDetail> items; // No need for DataModel here.
@EJB
private UserdetailsFacade ejbFacade;

public String submit() {
    items = ejbFacade.searchByString(search);
    return "index";
}

您的整个ContactsService似乎毫无用处.

Your whole ContactsService seems useless by the way.

根据您在问题更新中尝试使用getter方法的操作,请不要这样做.出于为什么JSF多次调用吸气剂

As per your attempt in the getter method in the update of your question, please don't do that. You should never call the DB in a getter method for the reasons mentioned in Why JSF calls getters multiple times

这篇关于搜索功能什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆