- Jakarta EE Cookbook
- Elder Moraes
- 229字
- 2021-06-24 16:12:37
How to do it...
We need to perform the following steps to try this recipe:
- Let's create a User class as the main object of our recipe:
public class User implements Serializable {
private String name;
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
//DON'T FORGET THE GETTERS AND SETTERS
//THIS RECIPE WON'T WORK WITHOUT THEM
}
- Now, we create a UserBean class to manage our UI:
@Named
@ViewScoped
public class UserBean implements Serializable {
private User user;
public UserBean(){
user = new User("Elder Moraes", "elder@eldermoraes.com");
}
public void userAction(){
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Name|Password welformed"));
}
//DON'T FORGET THE GETTERS AND SETTERS
//THIS RECIPE WON'T WORK WITHOUT THEM
}
- Now, we implement the Converter interface with a User parameter:
@FacesConverter("userConverter")
public class UserConverter implements Converter<User> {
@Override
public String getAsString(FacesContext fc, UIComponent uic,
User user) {
return user.getName() + "|" + user.getEmail();
}
@Override
public User getAsObject(FacesContext fc, UIComponent uic,
String string) {
return new User(string.substring(0, string.indexOf("|")),
string.substring(string.indexOf("|") + 1));
}
}
- Now, we implement the Validator interface with a User parameter:
@FacesValidator("userValidator")
public class UserValidator implements Validator<User> {
@Override
public void validate(FacesContext fc, UIComponent uic,
User user)
throws ValidatorException {
if(!user.getEmail().contains("@")){
throw new ValidatorException(new FacesMessage(null,
"Malformed e-mail"));
}
}
}
- And then, we create our UI using all of them:
<h:body>
<h:form>
<h:panelGrid columns="3">
<h:outputLabel value="Name|E-mail:"
for="userNameEmail"/>
<h:inputText id="userNameEmail"
value="#{userBean.user}"
converter="userConverter" validator="userValidator"/>
<h:message for="userNameEmail"/>
</h:panelGrid>
<h:commandButton value="Validate"
action="#{userBean.userAction()}"/>
</h:form>
</h:body>
Don't forget to run it in a Jakarta EE 8 server.
推薦閱讀
- 黑客攻防從入門到精通(實(shí)戰(zhàn)秘笈版)
- Photoshop智能手機(jī)APP UI設(shè)計(jì)之道
- OpenCV 3和Qt5計(jì)算機(jī)視覺應(yīng)用開發(fā)
- Mastering Kali Linux for Web Penetration Testing
- Easy Web Development with WaveMaker
- 軟件品質(zhì)之完美管理:實(shí)戰(zhàn)經(jīng)典
- Android開發(fā)三劍客:UML、模式與測試
- Visual C#.NET Web應(yīng)用程序設(shè)計(jì)
- 持續(xù)集成與持續(xù)交付實(shí)戰(zhàn):用Jenkins、Travis CI和CircleCI構(gòu)建和發(fā)布大規(guī)模高質(zhì)量軟件
- Java圖像處理:基于OpenCV與JVM
- Android系統(tǒng)下Java編程詳解
- Visual Basic程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)及考試指南
- Hack與HHVM權(quán)威指南
- Java程序設(shè)計(jì)實(shí)用教程(第2版)
- R語言與網(wǎng)站分析