[英]Inject JSF request parameter in a bean managed by Spring using annotations
Right now I use the f:viewParam
tag to inject the request parameter into a field of my bean 现在,我使用
f:viewParam
标记将请求参数注入到我的bean的字段中
<f:viewParam name="id" value="#{surveyController.id}" />
But I would much rather prefer to use annotations for this. 但是我宁愿更喜欢使用注释。 I know about the
@Value
annotation, and I guess I could do something like this 我知道
@Value
批注,我想我可以做这样的事情
@Component
@Scope("view")
public class SurveyControlador {
@Value("#{new Long.parseLong('${param.id}')}")
private Long id;
....
}
But this is just plain ugly. 但这真是丑陋。
Is there a better way, where I don't need to convert the value explicitly, and maybe even omit the "param"? 有没有更好的方法,我不需要显式转换值,甚至可以省略“参数”? I'm even willing to install third party libraries
我什至愿意安装第三方库
I've used omnifaces' @Param successfully, like so: 我已经成功使用了omnifaces' @Param ,如下所示:
@Named @ViewScoped
public class SurveyController {
@Inject @Param(name = "id")
private ParamValue<Long> idParam;
public void doStuff() {
if (idParam.getValue().equals(1)) {
throw new IllegalAccessException("you don't dare");
}
}
}
Pro: You additionally get access to the raw submitted value and omnifaces optionally applies validations/conversions (check the docs). 优点:您还可以访问原始提交的值,并且可以选择使用omnifaces进行验证/转换(请检查文档)。
Con: Wrapper around your real parameter. 缺点:包装您的真实参数。 And you still need to specify
<f:viewParam>
(you don't need to bind it to a backing bean, tho) if you want to keep the parameter for navigation. 而且,如果要保留参数进行导航,则仍然需要指定
<f:viewParam>
(不需要将其绑定到支持bean)。
Note that this leverages CDI, which may or may not fit the way you do things. 请注意,这利用了CDI,它可能适合您的做事方式,也可能不适合您的做事方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.