简体   繁体   English

我是否使用正确的JSF编码方式?

[英]am i using correct way of JSF coding?

I'm new in JSF. 我是JSF的新手。 Can i use this way of coding instead of using EL in JSF view? 我可以在JSF视图中使用这种编码方式代替EL吗? and correct me if there is something wrong in my coding or should i use better way. 如果我的编码有问题或应该使用更好的方法,请更正我。

@Named
@RequestScoped
public class RegistrationBacking extends Root {
    @EJB
    private UserManagerLocal userManager;
    public String register(){
        Map<String, Object> parameterMap = getRequestMap();
        User user = new User();
        user.setUserName((String) parameterMap.get("userName"));
        user.setPassword((String) parameterMap.get("password"));
        user.setEmail((String) parameterMap.get("email"));
        try{
            userManager.registerUser(user);
        } catch(UserExistsException ex) {
            Logger.getLogger(RegistrationBacking.class.getName()).log(Level.SEVERE, null, ex);
            getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, getBundle().getString("loginExist"), ex.getMessage()));
            return null;
        } catch(Exception ex) {
            Logger.getLogger(RegistrationBacking.class.getName()).log(Level.SEVERE, null, ex);
            getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, getBundle().getString("loginError"), ex.getMessage()));
            return null;
        }
        return "index";
    }
}

No. You're basically manually grabbing the submitted values from the request parameter map instead of binding the input values to the model. 不会。您基本上是从请求参数映射中手动获取提交的值,而不是将输入值绑定到模型。 You're manually filling the model in the controller's action method. 您正在使用控制器的操作方法手动填充模型。 You won't be able to perform JSF-managed Conversion and Bean Validation on those inputs. 您将无法在这些输入上执行JSF管理的转换和Bean验证。

The right way is the following: 正确的方法如下:

<h:form>
    <h:inputText value="#{registrationBacking.user.userName}" />
    <h:inputSecret value="#{registrationBacking.user.password}" />
    <h:inputText value="#{registrationBacking.user.email}" />
    <h:commandButton value="Register" action="#{registrationBacking.register}" />
</h:form>

And then in the backing bean: 然后在支持bean中:

private User user;

@PostConstruct
public void init() {
    user = new User();
}

public String register {
    try {
        // ...
    }
}

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JSF中使用CRUD的正确方法 - Correct way of using CRUD in JSF 使用JSF和Primefaces时更正消息字段的正确方法? - Correct way to update message-field when using JSF and Primefaces? 我在项目中使用jsf,hibernate和Spring,现在我需要实现JBPM(JBOSS) - I am using jsf, hibernate and Spring in my project and now i need to implement JBPM(JBOSS) 如何在JSF页面中禁用ctrl + v(粘贴)功能? 我正在使用primefaces组件。 - How to disable ctrl+v (paste) function in a JSF page ?. I am using primefaces component. 什么是在JSF 2.0中创建托管bean的多个实例的正确方法 - Whats the correct way to create multiple instances of managed beans in JSF 2.0 将输入值绑定到JSF托管bean属性的正确方法是什么? - What is the correct way to bind input value to JSF managed bean property? 对完整HTML页面在JSF中进行国际化的正确方法? - correct way to do internationalization in JSF for full HTML pages? 在TomEE 7 Plume中升级JSF库的正确方法是什么? - What is the correct way to upgrade JSF library in TomEE 7 Plume? 在JSF定制组件中,有什么方法可以确保属性包含正确的值? - In a JSF custom component, is there any way to ensure that an attribute contains a correct value? 在JSF中从URL获取参数的正确方法是什么 - What is the correct way to get parameter from URL in JSF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM