简体   繁体   中英

Faces Bean Validation doesn't work on Glassfish 3.1.1

I've used bean validation in a request scoped managed bean.

@ManagedBean(name = "loginController")
@RequestScoped
public class LoginController implements Serializable {

private static final long serialVersionUID = -7174581953062231554L;

@NotNull(message = "Password!")
private String mPassword;

@NotNull
private String mUsername;
...
...
public String login() {
    ..
    return "destination.xhtml";
}

..and in the web.xml I've configured this as well for interpreting empty strings as null value:

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

The view is looking like this:

<h:form id="login">
    <h3><h:outputText value="Username" /></h3>
    <h:inputText id="username" value="#{loginController.username}" />
    <h3><h:outputText value="Password" /></h3>
    <h:inputSecret id="password" value="#{loginController.password}" autocomplete="off" />
    <h:commandButton id="submit" value="Login" action="#{loginController.login}">
        <f:param name="redirect" value="#{requestScope['javax.servlet.forward.request_uri']}" />
    </h:commandButton>
</h:form>

Environment: Glassfish 3.1.1 / Mojarra JSF 2.1

The problem: When the submit button without to fill out the username or password field is pressed, the action will be executed, though the annotation (@javax.validation.constraints.NotNull) should prevent this. The validation doesn't work! Adding the attribute "required" to the fields will solve this problem, but I don't want the validation logic in the view. Do you have any advice for me?

UPDATE

Basically, the code above works. I tested it in a separate project. In the other project, which still not working, is something different. The form is configured as login form (login-config) in the web.xml file. Does this make some changes in the jsf life cycle? ..or something other?

Would your validation not be something about String.empty instead of "notnull" ? Especially if data's coming from a form, where "null" value for a string should not exist.

Required

The required attribute is a boolean flag that indicates whether or not the user is required to provide a value for this field before the form can be submitted to the server

Maybe, below @NotNull, you coul try to add

@Size(min=1, max=X)

See here : http://download.oracle.com/javaee/6/tutorial/doc/gircz.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM