How can I get the query string

若如初见. 提交于 2020-01-13 12:59:08

问题


I'm using jsf 1.1 and icefaces 1.8. I have a PartsInv.jsp page and a managed PartsInv.java bean. I've tried messing around with several classes based on google hits I've come across, and feel like I'm getting close, but I can't quite nail it. This is what I have:

HttpServletRequestWrapper hsrw;
String rcVal = hsrw.getAttribute("rc").toString();

But of course hsrw isn't properly instantiated.. I'm not really sure how to (or what to wrap it around). Any help would be appreciated.

Edit: Based on Jigar's answer, I've updated my code to the following:

    HttpServletRequest hsr = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

    if(hsr.getAttribute("rc") != null)
    {
        try
        {
            int rc = Integer.parseInt(hsr.getAttribute("rc").toString());
            this.SOM_RC.setValue(rc);
            this.changeRC(null);
        }
        catch(NumberFormatException nfe)
        {
            this.lblStatus.setValue("eASP error, please see an administrator.");
            return;
        }
    }

Edit2: ok, so i used the wrong method. hsr.getParameter() does the trick


回答1:


for JSF go

HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();



回答2:


If you want a specific parameter, inject it into your bean:

<managed-bean>
  <managed-bean-name>fooBean</managed-bean-name>
  <managed-bean-class>foo.FooBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
    <property-name>bar</property-name>
    <property-class>java.lang.String</property-class>
    <value>#{param.bar}</value>
  </managed-property>
</managed-bean>

If the bean you want to reference the parameter from is in a broader scope, you can look it up from the external context (parameter; parameters).

If you really want the query string, you can probably inject that from the request using the expression #{request.queryString}.



来源:https://stackoverflow.com/questions/7339397/how-can-i-get-the-query-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!