[英]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 {
// ...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.