URL jsf2 中的参数 [英] parameter in URL jsf2
问题描述
我需要这个链接:
http://myserver:/myproject/innerpage/clip.jsf&id=9099
从这样的代码中提取id:
to extract the id from a code like this:
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
String clipId = request.getParameter("id");
当我在 tomcat 上运行它时,我得到:
When I run it on tomcat I get:
留言/OnAir/innerpage/clip.jsf&id=9099
message /OnAir/innerpage/clip.jsf&id=9099
description 请求的资源(/OnAir/innerpage/clip.jsf&id=9099)不可用.
description The requested resource (/OnAir/innerpage/clip.jsf&id=9099) is not available.
当我在没有 &id=9099 的情况下运行它时,它运行正常.
When I run it without &id=9099 it runs all right.
我怎样才能让它运行?
推荐答案
URL 中路径和查询字符串之间的分隔符是?
,而不是&
.&
是查询字符串中多个参数的分隔符,例如name1=value1&name2=value2&name3=value3
.如果省略 ?
,则查询字符串将被视为 URL 中路径的一部分,这将导致您遇到的 HTTP 404 页面/资源未找到错误.
The separator character between path and query string in URL is ?
, not &
. The &
is separator character for multiple parameters in query string, e.g. name1=value1&name2=value2&name3=value3
. If you omit the ?
, then the query string will be seen as part of path in URL, which will lead to a HTTP 404 page/resource not found error as you encountered.
因此,此链接应该可以使用 http://myserver:port/myproject/innerpage/clip.jsf?id=9099
So, this link should work http://myserver:port/myproject/innerpage/clip.jsf?id=9099
也就是说,有一个更好的方法来访问请求参数.将其设置为值为 #{param.id}
的托管属性.
That said, there's a much better way to access the request parameter. Set it as a managed property with a value of #{param.id}
.
public class Bean {
@ManagedProperty(value="#{param.id}")
private Long id;
@PostConstruct
public void init() {
System.out.println(id); // 9099 as in your example.
}
// ...
}
EL #{param.id}
返回 request.getParameter("id")
的值.
The EL #{param.id}
returns you the value of request.getParameter("id")
.
提示:每当您需要从托管 bean 内的 JSF 引擎盖下提取原始"Servlet API 时,请始终问自己(或在 SO):难道没有 JSF 风格的方法吗?".很有可能你把事情不必要地复杂化了 ;)
A tip: whenever you need to haul the "raw" Servlet API from under the JSF hoods inside a managed bean, always ask yourself (or here at SO): "Isn't there a JSF-ish way?". Big chance you're unnecessarily overcomplicating things ;)
这篇关于URL jsf2 中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!