好的,所以我有一个JSF支持bean需要引用另一个(@NoneScoped)bean.
我应该@Inject它还是使用@ManagedProperty从容器中获取实例引用?
为什么要使用一个而不是另一个,在我看来,这两种方法实现了同样的目的.
我正在使用JavaServer Faces开发Web应用程序.
我已经找到了很多关于如何使用JavaServer Faces的示例和教程,但它们都没有实际解释Bean的用途.我最初的想法是,豆类代表形式.在表单中输入数据并单击提交按钮,关联的Bean将填充数据并调用方法.
但是,到目前为止,我只看到每页有一个Bean的示例,因此Bean也可以表示页面,因此包含多个表单.
我也对Bean的范围感到困惑.如果Bean表示表单或页面,则在请求结束后它将变为无效.如果你让bean活在会话范围内,那么Bean会发生什么?您是否仍然可以以某种方式从中获取数据,或者一旦您返回它就会为您填写相关表格?
总结一下 - 什么是Managed Bean以及如何正确使用它?
我希望托管bean在应用程序加载时在我的JSF Web应用程序中启动时在内部运行.如何在Glassfish中编写此类并进行配置?
我在JSF 2.0项目中使用SessionScoped bean时遇到了一个非常奇怪的问题.使用Netbeans 6.9.1,Glassfish 3服务器和PrimeFaces 3作为JSF组件库.
这是一些代码:
package com.hia.jsf;
import com.hia.netlabel.jpa.Genre;
import com.hia.netlabel.jpa.Label;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
@ManagedBean
@SessionScoped
public class LabelDetailJSF implements Serializable{
@ManagedProperty("#{genreLabelListJSF}")
private GenreLabelListJSF genreLabelListJSF;
private List<Genre> DetailLabelGenreList;
private Label DetailLabel;
/** Creates a new instance of LabelDetailJSF */
public LabelDetailJSF() {
}
@PostConstruct
public void init(){
System.out.print("Running init LabelDetailJSF");
if(genreLabelListJSF.getSelectedLabel()!=null)
{
System.out.print("genreLabelListJSF was not null");
this.DetailLabelGenreList=genreLabelListJSF.getSelectedLabel().getGenreList();
this.DetailLabel= (genreLabelListJSF.getSelectedLabel());
}
if(this.DetailLabelGenreList==null){
System.out.println("Bloody thing became null");
}
}
/**
* …Run Code Online (Sandbox Code Playgroud) 实际上是否可以在JSF中的托管组件之间传递任何数据?如果是的话,如何实现这一目标?
有人可以提供任何样品吗?
我需要在Servlet中修改用户会话对象(SessionScoped bean-CDI),所以我必须以某种方式获取该bean.我用以下方式注射:
@Inject
private UserSession user;
Run Code Online (Sandbox Code Playgroud)
UserSession是SessionScoped CDI bean.用户方法是从doPost或doGet servlet方法调用的.这很完美; 每次@Inject注释都会注入相应的UserSession bean,但我不明白这种行为是如何实现的.
我假设用@Inject注释的bean只注入一次(当创建对象 - 本例中的Servlet实例时),但这显然是错误的假设.
那么,这些bean什么时候注入到servlet中?按要求?当有多个UserSession对象时,这种方法如何避免冲突(一个servlet实例 - 处理它的多个线程)?
我有一个CDI托管bean,我想将请求参数设置为托管属性:
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
@Named
@RequestScoped
public class ActivationBean implements Serializable {
@ManagedProperty(value="#{param.key}")
private String key;
@ManagedProperty(value="#{param.id}")
private Long id;
// Getters+setters
Run Code Online (Sandbox Code Playgroud)
domain/activate.jsf?key=98664defdb2a4f46a527043c451c3fcd&id=5但是,URL是永远不会设置和保留的属性null.
这是怎么造成的,我该如何解决?
我知道我可以从ExternalContext下面手动抓取它们:
Long id = Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"), 10);
String key = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("key");
Run Code Online (Sandbox Code Playgroud)
但是,我宁愿使用注射剂.
jsf cdi managed-bean http-request-parameters managed-property
我刚刚创建了一个新的maven项目并添加了一个索引控制器.然后我使用了managedbean注释.但是我得到了这个消息The type ManagedBean is deprecated.所以我尝试了替代品,但我找不到任何解决方案.所有文章都使用@ManageBean.所以我觉得这里遗漏了一些东西.
我的pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>asf.asdflksdfklj</groupId>
<artifactId>Demo-App</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Demo-App</name>
<dependencies>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.3</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.3.0-m06</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.0.M0</version>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
和IndexController
package asf.asdflksdfklj.Controller;
import javax.faces.bean.ManagedBean;
@ManagedBean
public class IndexController {
public String showHello(){
return "hello world";
}
}
Run Code Online (Sandbox Code Playgroud)
难道ManagedBean真的被弃用了吗?如果是这样,我应该使用什么来使这个代码工作?
我找到了一些关于将JSF技术与Spring Boot集成的教程,但是让OmniFaces使用Spring Boot似乎是一项相当复杂的工作.将这两者结合在一起是一个好主意吗?
我正在使用Mojarra 2.2.12,在我们的项目中我们有一些@ApplicationScoped豆子.例如:
@ManagedBean
@ApplicationScoped
public class AppScopedBean{
private int commonValueForClients;
//GET, SET
public void evalNew(){
int newCommonVal;
//Evaluation of the new value, doesn't depend on the commonValueForClients
commonValueForClients = newCommonVal;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我们应该担心新指定值的可见性吗?
我在规范中找不到JSF基础结构必须同步访问@ApplicationScopedbean字段.因此,特别是对于Mojarra 2.2.12,我们应该将字段声明为volatile或明确地同步对它的访问吗?
jsf ×9
managed-bean ×6
cdi ×3
jsf-2 ×3
architecture ×1
concurrency ×1
deprecated ×1
integration ×1
java ×1
omnifaces ×1
primefaces ×1
servlets ×1
spring-boot ×1
startup ×1