moved! JSF + CDI on Tomcat and Bean Validation

Introduction

Up to the last time, I created an environment setting for running JSF + CDI on Tomcat and an application for checking the operation. This time, I would like to add a function to this and run Bean Validation.

Click here for the last time-> "It worked! JSF + CDI (Environment settings) with Tomcat"

The environment is as usual:

Implementation

The implementation procedure is as follows. We will add functions to the previous operation check application.

maven settings

Use ExtVal of myfaces. Since this refers to validation-api-1.0.0GA, hibernate-validator uses the slightly older 4.3.2.Final.

pom.xml


<dependency>
    <groupId>org.apache.myfaces.extensions.validator.validation-modules</groupId>
    <artifactId>myfaces-extval-bean-validation</artifactId>
    <version>2.0.8</version>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.3.2.Final</version>
    <scope>runtime</scope>
</dependency>

Add annotation

We will add functions to the previous operation check application. Add the required validators to your login and password. First, add an annotation (@NotNull) to the Index Bean.

InexBean.java


public class IndexBean implements Serializable {

    @NotNull
    private String loginId;

    @NotNull
    private String password;

    //Omitted below
}

Next is xhtml (excerpt).

index.html


<body>
    <h:messages escape="true" id="messages" showDetail="false" closable="true"/>
    <h:form id="form">
        <br/>
        <h:inputText value="#{index.loginId}" label="username" ph:placeholder="Username" /><br/>
        <h:inputSecret value="#{index.password}" label="password" ph:placeholder="Password" /><br/>
        <h:commandButton action="#{index.loginEvent()}" value="Login" update="@form" />
    </h:form>
</body>

Up to this point, display the screen and press the login button. It will be as follows.

サンプルアプリ

This is fine, but I would like to make the message in Japanese. ValidationMessage_ja.properties

Create properties under src / main / resources.

ValidationMessage_ja.properties


javax.faces.validator.BeanValidator.MESSAGE={0}
javax.validation.constraints.NotNull.message={1}Is mandatory

I will not explain in detail, but the first line is the display format of the message used when JSF executes Bean Validation. A message is set to {0} and displayed on the screen. The second line is the message. The key corresponds to the Bean Validation Not Null. For {1}, the value specified by the label attribute of xhtml is set. This area is controlled by JSF.

Add this file to faces-config.xml to make it recognized by JSF.

faces-config.xml


<application>
    <message-bundle>ValidationMessages</message-bundle>
</application>

Complete

After implementing the above, display the screen and press the login button. サンプルアプリ

did it~!

that's all.

Other notes

BeanVaidation is also good with bval-jsr. In this case, place javaee-api-8.0.4.jar under tomcat / lib. Add it to pom as it is needed for compilation. (It doesn't work with <scope> compile </ scope>. Place it in tomcat / lib.) TomEE is better if you do so far.

pom.xml


<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>8.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.bval</groupId>
    <artifactId>bval-jsr</artifactId>
    <version>2.0.5</version>
</dependency>

reference

MyFaces ExtVal

Recommended Posts

moved! JSF + CDI on Tomcat and Bean Validation
moved! JSF + CDI (Preferences) on Tomcat
Change JDK and Tomcat versions on CentOS
JSP on tomcat
Apache and tomcat