抱歉,如果之前有人问过,我找不到。
我有以下链接:
http://www.mysite.kg/news/news.xhtml?id=2
如何在问号后获取值,例如来自 URL 的 id=2?
我想这可以解决:
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getRequest();
String url = req.getRequestURL().toString();
String id = "";
Pattern pattern = Pattern.compile( "id=(\\d+)" );
Matcher matcher = pattern.matcher( url );
if ( matcher.find() )
id = matcher.group( 1 );