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
描述所请求的资源 (/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")
的值.
提示:每当您需要从托管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屋!