简体   繁体   English

Pretty Faces EL注入不起作用

[英]Pretty Faces EL Injection does not work

I am building an online application in JSF 2 with Primefaces and Spring. 我正在使用Primefaces和Spring在JSF 2中构建在线应用程序。 I want to use Pretty Faces to make our search urls bookmarkable. 我想使用Pretty Faces使我们的搜索URL可添加书签。

Here is my pretty-config.xml: 这是我的pretty-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<pretty-config xmlns="http://ocpsoft.org/prettyfaces/3.3.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://ocpsoft.org/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.3.xsd">

    <url-mapping id="index">
        <pattern value="/" />
        <view-id value="/sites/public/template/index.xhtml" />
    </url-mapping>

    <url-mapping id="searchWithParams">
        <pattern value="/search/#{searchView.searchQuery}/#{searchView.searchTags}/#{searchView.searchOrder}" />
        <view-id value="/sites/public/product/productSearch.xhtml" />
    </url-mapping>

    <url-mapping id="searchWithoutParams">
        <pattern value="/search" />
        <view-id value="/sites/public/product/productSearch.xhtml" />
    </url-mapping>

</pretty-config>

Here is my Bean I want to inject in: 这是我要注入的Bean:

@Component
@Scope("request")
public class SearchView {

    private String searchQuery = "";

    private String searchTags = "";

    private String searchOrder = "";


    public SearchView() {
        // void
    }

    public void navigate() {
        String url = "";
        try {
            url =  "/search" + 
                    "/" + URLEncoder.encode(searchQuery, "UTF-8") + 
                    "/" + URLEncoder.encode(searchTags, "UTF-8") + 
                    "/" + URLEncoder.encode(searchOrder, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            url = "/search";
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(url);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    // Getters and Setters ...

}

Only "/search" works perfectly fine. 只有“ /搜索”可以很好地工作。 I also tried changing the order of the mappings in the pretty-config.xml and using dummy values for the other two parameters and only using one parameter. 我还尝试过更改pretty-config.xml中的映射顺序,并对其他两个参数使用伪值,并且仅使用一个参数。 Another thing I tried was to change teh expressions "#{bean.param}" to "#{getParam : bean.param}". 我尝试的另一件事是将表达式“#{bean.param}”更改为“#{getParam:bean.param}”。 Changing the scope of my bean doesn't help either. 更改bean的范围也无济于事。

On the webpage I use a primefaces commandlink which has "searchView.navigate" as action parameter. 在网页上,我使用了带有“ searchView.navigate”作为操作参数的primefaces命令链接。

I always get the following error message: 我总是收到以下错误消息:

HTTP Status 404 - /search/a/b/c HTTP状态404-/ search / a / b / c

type Status report 类型状态报告

message /search/a/b/c 消息/ search / a / b / c

description The requested resource (/search/a/b/c) is not available. 说明所请求的资源(/ search / a / b / c)不可用。

Definitely having a pattern like /search/#{searchView.searchQuery}/#{searchView.searchTags}/#{searchView.searchOrder} is a bad idea. 绝对采用/search/#{searchView.searchQuery}/#{searchView.searchTags}/#{searchView.searchOrder}这样的模式是一个坏主意。

Here a quote of my conversation with @Lincon, Prettyfaces team lead : 这是Prettyfaces团队负责人@Lincon 对话的引文:

  • Me : And other one, is it possible to add two params in pattern? :另外一个,是否可以在模式中添加两个参数? Like /detail/#{appId}/#{navigationIndex} . / detail /#{appId} /#{navigationIndex}一样
  • Lincon : You could certainly create a secondary URL-mapping that would also display the same view-ID, and as long as the parameters are different, eg: One mapping is "/detail" and the other is "/detail/#{appId}", you'll be fine. Lincon :当然,您可以创建一个辅助URL映射,该映射也将显示相同的view-ID,并且只要参数不同即可,例如:一个映射为“ / detail”,另一个为“ / detail /#{appId }“, 你会没事的。 But if they have the same pattern, you'll have problems. 但是,如果它们具有相同的模式,那么您将遇到问题。

Personally I don't even use EL expressions into url patterns. 我个人甚至不将EL表达式用于网址格式。 I began to use them but they have a big drawback: they force you to specify the url as it is written in the pattern. 我开始使用它们,但是它们有一个很大的缺点:它们会迫使您指定在模式中写入的url。

For example, having /students/#{studentId} will always force you to specify a student id to reach that page. 例如,拥有/ students /#{studentId}总是会迫使您指定学生ID才能到达该页面。 Otherwise you need to declare a second pattern for the same view-id. 否则,您需要为同一view-id声明另一个模式。 Appart from that, a second parameter in the GET request will always force you to use the http standard way to pass the parameters (imagine you want to add a second one, you would need /students/detail/#{studentId}?viewMode=2 , passing them in a different way). Appart从此开始,GET请求中的第二个参数将始终迫使您使用http标准方式传递参数(假设您想添加第二个参数,则需要/ students / detail /#{studentId}?viewMode = 2 ,以其他方式传递它们)。

For me is much easier to declare something like /students/detail and after pass the params in the classic (not as pretty) http way: /students/detail?studentId=1&viewMode=2 . 对我来说,声明/ students / detail之类的东西要容易得多,然后以经典(不太漂亮)的HTTP方法传递参数: / students / detail?studentId = 1&viewMode = 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM