2

In my web application, where presentation tier is in JSF, i want to have button 'back to previous page' on some pages.

I'm making it through URL parameters: http://www.example.com/index.xhtml?backurl=value

for example, in mange bean i set current page A.xhtml url:

public String showCourse(Course c){
    currentCourse = c;
    String currentUrl = FacesContext.getCurrentInstance().getViewRoot().getViewId();
    return "/B.xhtml?faces-redirect=true&backurl=" + currentUrl;
}

and at B.xhtml (with url http://www.example.com/A.xhtml?backurl=A.xhtml) i have

<h:button value="Back to previous page" outcome="#{backurl}"/> 

which redirect me to previous page.

Everything works fine, but the problem is when i redirect from B.xhtlm page to C.xhtml page. I set backurl url parameter again. After go to C.xhtml page and come back to B.xhtml i lose previous backurl parameter - when i click to button i don't come back to A.xhtml page but to C.xhtml :(

Do you know beter why to do this ?

4

2 回答 2

1

为什么不使用浏览器历史记录并让用户使用浏览器的后退和前进按钮?无论如何,很难将用户限制为这样。当然,Web 应用程序设计需要支持这种行为。

于 2013-11-07T14:03:04.977 回答
1

尽管这很尴尬,但我可以想到两种方法。

第一个源于“线性流”假设,在该假设下,您有预定义的视图序列要呈现给用户,就像在流或对话中一样,并且当前返回 URL 取决于当前视图 ID。因此,您应该有一个应用程序范围的 bean,其中包含返回 URL 的映射,其中键是源视图,目标视图(如后退按钮所指)是值。然后,您可以根据当前视图 id 定义返回按钮结果:

@ApplicationScoped @ManagedBean
public class BackUrlBean implements Serializable {
    private Map<String, String> map;//getter+setter
    @PostConstruct
    public void init() {
        map = new HashMap<String, String>();
        String from = ... , back = ...;
        Map.put(from, to);//fill the map
    }
}

然后,您可以在流程中具有以下结构:

<ui:fragment rendered="#{not empty backUrlBean.map[view.viewId]}">
    <h:button value="Back to previous page" outcome="#{backUrlBean.map[view.viewId}"/>
</ui:fragment>

只要您拥有流程的线性树和一组预定义的页面,那就太好了。

另一个可行的选择是传递一个 JSON 序列化的反向 URL 列表,而不是一个普通的反向 URL。在这种方法中,您将在请求下一页时向列表中添加一个元素,并且用户在流程中继续前进,当用户单击返回时,您将从列表中删除一个元素。然后最后一个列表元素是您当前的返回 URL。From-to JSON 序列化是通过 Gson 等方式在 postconstruct 方法中执行的。

但请注意,由于服务器限制,获取查询参数的大小可能会受到限制,因此如果您的流程不那么深,这种方法是可行的。此外,当您将列表作为查询参数传递到操作方法中时,不要忘记对列表进行 URL 编码。

这种方法的优点是它的通用性。这种方法的实施留给您练习。

也就是说,后退按钮必须是后退按钮,它应该将用户带到先前请求的页面。否则,您最终可能会遇到糟糕的用户体验。

在我看来,可以使用包含导航按钮的类似向导的组件来模拟这些前后视图。带有后退按钮的方法适用于像主从页面这样的两页视图,但不适用于结构更复杂的流程。

于 2013-11-07T15:47:54.133 回答