gpt4 book ai didi

jsf - 托管属性(property)继承

转载 作者:行者123 更新时间:2023-12-02 00:25:04 25 4
gpt4 key购买 nike

我的问题与此类似issue 。我有一个 BaseBean,目前只有一个属性,该属性被注释为 @ManagedProperty

但是,当我在命令按钮的操作方法中访问此继承的托管属性的 getter 时,它返回 null。我调试并确认基本 bean 构造函数被调用了两次 - 一次在页面加载时调用,下一步在单击按钮时调用,如上述链接中所述。

我遵循了文章选择的答案以及 this 中提到的建议。发帖了,但是没有效果。

以下是我的代码:

public abstract class BaseBean
{
@ManagedProperty(value = "#{serviceLocator}")
private IServiceLocator serviceLocator;

public IServiceLocator getServiceLocator() {
return serviceLocator;
}

public void setServiceLocator(IServiceLocator serviceLocator) {
this.serviceLocator = serviceLocator;
}
}
<小时/>
@ManagedBean
@ViewScoped
public class RegistrationBean extends BaseBean implements Serializable
{
private static final long serialVersionUID = -6449858513581500971L;

private String userID;
private String password;
private String firstName;
private String lastName;
private String email;
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String pincode;

private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationBean.class);

/* getter / setters */

public String register()
{
String nextPage = null;
try {
RegistrationDetails userDetails = ModelBuilder.populateRegistrationData(this);
int registrationID = getServiceLocator().getUserService().registerUser(userDetails);
LOGGER.info("Registered user successfully. Registration ID - {}", registrationID);
nextPage = "success";
}
catch (RegistrationException e) {
LOGGER.error(e.getMessage());
}
return nextPage;
}

public void checkUserExists()
{
int regID = getServiceLocator().getUserService().findUser(getUserID());

if(regID > 0) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "User already exists !!", null);
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}

为什么在表单提交时会再次调用构造函数??? :/

即使在通过 ajax 对 userID 字段的模糊事件调用的 checkUserExists() 方法中,getter 也会返回 null。

编辑:添加了 ServiceLocator 的代码..

@ManagedBean
@ApplicationScoped
public class ServiceLocator implements IServiceLocator
{
private static final String USER_SERVICE = "userService";
private static final String MOVIE_SERVICE = "movieService";

@PostConstruct
public void init() {
final ServletContext sc = FacesUtils.getServletContext();
this.webAppContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
this.userService = (IUserService) webAppContext.getBean(USER_SERVICE);
this.movieService = (IMovieService) webAppContext.getBean(MOVIE_SERVICE);
}

private ApplicationContext webAppContext;

private IUserService userService;

private IMovieService movieService;

@Override
public IUserService getUserService() {
return userService;
}

@Override
public IMovieService getMovieService() {
return movieService;
}
}

最佳答案

据我所知,您正在尝试混合两个答案:一个用于 @RequestScoped mbean,另一个用于 @ViewScoped mbean。如果您看到您发布的第一个链接,BalusC 表示您不必在 @ViewScoped mbean 中具有 @ManagedProperty,如 ViewParam vs @ManagedProperty(value = “#{param.id})” 中所示。 .

如果您无法通过 View 参数传递serviceLocator,则必须找到另一种方法来获取该值(从 session 中保存/检索它)。

另外,请查看 BalusC 的信息,了解为什么可以在每个请求上重新创建 @ViewScoped mbean:

In a nutshell: the @ViewScoped breaks when any UIComponent is bound to the bean using binding attribute or when using JSTL or tags in the view. In both cases the bean will behave like a request scoped one. The first one is in my opinion a pretty major bug, the second one is only an extra excuse to get rid of the whole JSTL stuff in Facelets.

This is related to JSF 2.0 issue 1492. Here's an extract of relevance: This is a chicken/egg issue with partial state saving. The view is executed to populate the view before delta state is applied, so we see the behavior you've described. At this point, I don't see a clear way to resolve this use case. The workaround, if you must use view-scoped bindings would be setting javax.faces.PARTIAL_STATE_SAVING to false.

来自

<小时/>

根据您的评论和编辑,您可以使用此处提供的代码访问 @ApplicationScoped mbean:

这就是行:

FacesContext.getCurrentInstance().getExternalContext()
.getApplicationMap().get("serviceLocator");

您必须使用该代码,因为显然 @ViewScoped bean 无法接受 @ManagedProperty 的注入(inject)。

关于jsf - 托管属性(property)继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13218329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com