1. First, you build the xhtml file
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"> <f:view encoding="UTF-8" contentType="text/html"> <h:head> <title>#{msgs.windowTitle}</title> <h:outputStylesheet library="css" name="styles.css" /> </h:head> <h:body> <h:form> <h:outputText value="#{msgs.greeting}" styleClass="emphasis" /> <br /> <h:messages errorClass="errors" layout="table" /> <h:panelGrid columns="3"> #{msgs.namePrompt}: <h:inputText id="name" value="#{user.name}" required="true" label="#{msgs.namePrompt}" /> <h:message for="name" errorClass="errors" /> #{msgs.agePrompt}: <h:inputText id="age" value="#{user.age}" required="true" size="3" label="#{msgs.agePrompt}" /> <h:message for="age" errorClass="errors" /> </h:panelGrid> <h:commandButton value="#{msgs.submitPrompt}" /> </h:form> </h:body> </f:view> </html> |
The correct label should be declared as:
|
1 2 |
<h:inputText id="name" value="#{user.name}" required="true" label="#{msgs.namePrompt}" /> |
and the correct error message should be declared as:
|
1 2 |
<h:message for="name" errorClass="errors" /> #{msgs.agePrompt}: |
with “for” as same as “name” of the target. 2. and then build user.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @Component("user") @Scope("session") public class User implements Serializable { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } } |
Recent Comments