JSF最佳传递参数的方法 [英] JSF best Way to pass parameters

查看:86
本文介绍了JSF最佳传递参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用JSF,但是由于我是一个老旧而笨拙的php-dev,所以我总是把请求的GET-Parameters弄乱了. (在PHP中,您可以使用$_GET["paramname"]数组随时访问请求参数.

i'm currently working with JSF, but since i'm a old and grouchy php-dev i always mess up with the GET-Parameters of an request. (In PHP you can access the request Paramerts whenever you want, using the $_GET["paramname"] Array.

现在,我正在开发一个用户管理系统. (CRUD)通常,我会列出所有可用用户的列表,并提供诸如编辑/删除之类的选项.然后,编辑链接指向http:\\localhost\editUser.xhtml?userId=5.

Now, i'm working on an User Managment System. (CRUD) Usually i go with a list of all available users, having options like edit / delete. The edit link is then pointing to http:\\localhost\editUser.xhtml?userId=5 for example.

我还有一个名为UserEditController的控制器,该控制器基本上持有用户实体并调用更新.

I have also an Controller called UserEditController which basically holds the user entity and invokes the update.

因此,在此Controller内,我正在使用@PostConstruct注释从DataService加载ID为5的用户并将其存储在属性中.

So, inside this Controller I'm using the @PostConstruct Annotation to load the user with the id 5 from the DataService and store it inside a property.

这里的问题是:Wenn在APPLY_REQUEST_VALUES -Phase im中输入PostConstruct-Method无法访问请求的Faces Context参数映射(将对userId返回null)

Problem here is: Wenn entering PostConstruct-Method in the APPLY_REQUEST_VALUES-Phase im not able to access the request Parameter Map of Faces Context (Will return null for userId)

控制器(简体):

@Named
@RequestScoped
UserEditController{


@Inject
private UserDataService userDataService;

private User currentUser;

@PostConstruct
public void initValues(){
    String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("userId");

    this.currentUser = userDataService.getUserById(id);
}

public String saveCurrentUser(){
    userDataService.updateUser(this.currentUser);
    return "userManagement"; //return to overview after saving
}

//getter and setter
}

这可以按预期进行.现在,所有的表单域"都已绑定到用户的属性,例如#{userEditController.currentUser.forename}等.

This works as intened. All the Form-Fields now are bound to user's properties like #{userEditController.currentUser.forename} etc.

我现在添加了具有动作属性{userEditController.saveCurrentUser()}

I now added a save button with action attribute {userEditController.saveCurrentUser()}

但是脚本随后进入APPLY_REQUEST_VALUES-阶段,并再次调用UserEditController的initValues.

However the script then enters the APPLY_REQUEST_VALUES-Phase And the UserEditController's initValues is called again.

  • 尝试访问RequestParameterMap时,这会导致nullpointer异常
  • 显然,我不想在保存用户之前重新加载用户:-)

这听起来像是一个愚蠢的问题,但是我认为我在搞混PHP样式和JSF样式之间来处理事情.

This sounds like a stupid question but i think i'm messing up between the PHP-Style and the JSF-Style to handle things.

任何帮助将不胜感激.

推荐答案

将用户(或用户ID)传递到BackingBean的常用方法是使用以下选项之一:

The common way to pass the user (or userID) to your BackingBean, is using one of the following options:

http ://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/

我认为主要问题是您的bean是@RequestScoped.如果在编辑页面上按保存按钮,则将发送新请求,因此将创建请求范围的Bean的新实例.只需将您的bean更改为@ViewScoped.

I think the main problem is that your bean is @RequestScoped. If you push the save button on your edit-page you are sending a new request, so a new instance of a request scoped bean will be created. Just change your bean to @ViewScoped.

更多信息:

这篇关于JSF最佳传递参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆