简体   繁体   English

使用注解将JSF请求参数注入到Spring管理的bean中

[英]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.

相关问题 使用来自jsf托管bean的spring bean的问题 - problem with using spring beans from jsf managed bean Spring 框架。 根据请求参数在运行时注入bean - Spring Framework. Inject bean at runtime based on request parameter JSF 2使用@ManagedProperty注入Spring bean / service而没有xml - JSF 2 inject Spring bean/service with @ManagedProperty and no xml 如何使用Spring注释将Properties文件中的值注入到现有实例(不受Spring管理)的字段中? - How do I inject a value from Properties file to a field of an existing instance (not managed by Spring) using Spring annotations? 在Glassfish中使用从预先获取的JSF管理的bean中使用spring bean的问题 - Problem with using spring bean from eagerly-fetched JSF managed bean in Glassfish JSF 2.0:为什么我得到这个Inject托管bean异常 - JSF 2.0: Why do I got this Inject managed bean Exception 按下JSF命令按钮两次调用了Spring管理的请求范围的Bean构造函数 - Spring-managed request scoped bean constructor called twice on press of JSF command button Java Spring-将带有注释的参数注入超类 - Java Spring - Inject a parameter into a superclass with annotations 如何将属性值注入使用注释配置的 Spring Bean? - How can I inject a property value into a Spring Bean which was configured using annotations? JSF-托管Bean变量? - JSF - managed bean in variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM