0

我有两页 - 搜索和结果以及一个 bean。如果我使用 ViewScoped,我的结果不会显示。如果我使用 RequestScoped,我的页面链接将不起作用。我该如何解决?如果我使用 Viewscoped 将提交放入 init,一切正常,但如果我这样做,我应该如何从表单中获取变量?

我的豆子:

@Named
@ViewScoped
public class Result implements Serializable {

private static final long serialVersionUID = 1L;
private DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");
private RecipeDAO recipeDAO = javabase.getRecipeDAO();
private ResourceBundle messages;
@Inject
LocaleBean locale;
private List<Recipe> recipes;
private List<Recipe> recipesView;
private String language;
private List<Page> pagesArray;
private Integer currentPage;

public void init() {
    System.out.println("Language is: " + language);
    String language1 = "en";
    recipes = new ArrayList<>();
    recipes = recipeDAO.searchRecipe(language1);
    int showValue = 5; //split value
    int pages = recipes.size() / showValue;  //pages value
    if (recipes.size() % showValue != 0) {
        pages = pages + 1;
    }
    for (int i = 0; i < recipes.size(); i++) {
        int a = i + 1;
        //System.out.println("i is: " + i + " recipe number is: " + a);
    }
    //System.out.println("pages is: " + pages + " size is: " + recipes.size());
    pagesArray = new ArrayList<>();
    Page page;
    for (int i = 0; i < recipes.size(); i = i + showValue) {
        page = new Page();
        page.setPageDisabled(false);
        page.setFirstItem(i);
        page.setLastItem(i + showValue - 1);
        pagesArray.add(page);
    }
    for (int i = 0; i < pagesArray.size(); i++) {
        pagesArray.get(i).setPageNumber(i + 1);
    }
    pagesArray.get(pagesArray.size() - 1).setLastItem(recipes.size() - 1);
    /* for (int i = 0; i < pagesArray.size(); i++) {
     System.out.println(pagesArray.get(i).getPageNumber() + " " + pagesArray.get(i).getFirstItem() + " " + pagesArray.get(i).getLastItem() + " " + pagesArray.get(i).isPageDisabled());
     }*/
    recipesView = new ArrayList<>();
    Recipe recipe;
    for (int i = pagesArray.get(0).getFirstItem(); i <= pagesArray.get(0).getLastItem(); i++) {
        recipe = new Recipe();
        recipe.setAuthor(recipes.get(i).getAuthor());
        recipe.setId(recipes.get(i).getId());
        recipe.setCreateDate(recipes.get(i).getCreateDate());
        recipe.setTitle(recipes.get(i).getTitle());
        recipesView.add(recipe);
    }
    pagesArray.get(0).setPageDisabled(true);
}

public void doPages() {
    FacesContext fc = FacesContext.getCurrentInstance();
    this.currentPage = Integer.parseInt(getCountryParam(fc));
    System.out.println(currentPage);
    recipesView = new ArrayList<>();
    Recipe recipe;
    for (int i = pagesArray.get(currentPage - 1).getFirstItem(); i <= pagesArray.get(currentPage - 1).getLastItem(); i++) {
        System.out.println(i);
        recipe = new Recipe();
        recipe.setAuthor(recipes.get(i).getAuthor());
        recipe.setId(recipes.get(i).getId());
        recipe.setCreateDate(recipes.get(i).getCreateDate());
        recipe.setTitle(recipes.get(i).getTitle());
        recipesView.add(recipe);
    }
    for (int i = 0; i < pagesArray.size(); i++) {
        pagesArray.get(i).setPageDisabled(false);
    }
    pagesArray.get(currentPage - 1).setPageDisabled(true);
}

public String getCountryParam(FacesContext fc) {
    Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
    return params.get("currentPage");
}


//setters&getters

}

我的搜索视图:

<h:form>
         <h:selectOneMenu value="#{search.language}">
            <f:selectItem itemLabel="English" itemValue="en"/>
            <f:selectItem itemLabel="Select" itemValue=""/>
            <f:selectItem itemLabel="Bulgarian" itemValue="bg"/>
        </h:selectOneMenu>
        <h:commandButton value="Submit" type="submit" action="#{search.submit()}">
            <f:param name="lang" value ="#{search.language}"/>
        </h:commandButton>
</h:form>

我的结果视图:

    <f:metadata>
        <f:viewParam name="lang" value="#{result.language}" />
        <f:viewAction action="#{result.init()}" />
    </f:metadata>

<h:form>
            <ui:repeat value="#{result.recipesView}" var="rec">
                <h:link value="#{rec.title}" outcome="recipeshow">
                    <f:param name="id" value="#{rec.id}" />
                </h:link>
                <br/>
                <h:outputText value="#{rec.id}"/><br/>
                <h:outputText value="#{rec.author}"/><br/>
                <h:outputText value="#{rec.createDate}"/><br/>
                <br/>
            </ui:repeat>
            <br/>
            <ui:repeat value="#{result.pagesArray}" var="page">
                <h:commandLink value="#{page.pageNumber}" disabled="#{page.pageDisabled}">
                    <f:ajax listener="#{result.doPages()}" render="@form"/>
                    <f:param name="currentPage" value="#{page.pageNumber}"/>
                </h:commandLink>                  &nbsp;
            </ui:repeat>
        </h:form>

我的页面.java

public class Page {

private int pageNumber;
private boolean pageDisabled;
private int firstItem;
private int lastItem;

public int getPageNumber() {
    return pageNumber;
}

public void setPageNumber(int pageNumber) {
    this.pageNumber = pageNumber;
}

public boolean isPageDisabled() {
    return pageDisabled;
}

public void setPageDisabled(boolean pageDisabled) {
    this.pageDisabled = pageDisabled;
}

public int getFirstItem() {
    return firstItem;
}

public void setFirstItem(int firstItem) {
    this.firstItem = firstItem;
}

public int getLastItem() {
    return lastItem;
}

public void setLastItem(int lastItem) {
    this.lastItem = lastItem;
}
}
4

1 回答 1

2

您从操作方法返回了导航案例结果,因此您的视图范围 bean 将被重新创建,这就是您看不到结果的原因。另一方面,由于您似乎正在根据视图范围中保存的信息进行分页,因此您的请求范围 bean 不允许正确分页。

至于解决方案,您应该从您的操作方法进行回发,从而返回nullor void,或者使用 get 参数将必要的信息传递给结果视图。

还值得注意的是,您在 中预加载了太多信息recipes = recipeDAO.searchRecipe(language);:您应该加载与视图中显示的一样多的信息,例如:recipes = recipeDAO.searchRecipe(language, min, max);

于 2013-09-23T11:52:47.153 回答