1

我正在尝试有条件地设置元素的 css。这是jsp中的行:

<s:textfield name="registerBean.email" placeholder="Email Address*" cssClass="INSERT userNameError here" required="" />
<s:textfield name="registerBean.userName" placeholder="Username*" cssClass="INSERT emailError here" required="" />

在我的动作课中,我有这样的事情:

package actions;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import service.implementations.UserServiceImplementation;
import service.interfaces.UserService;
import util.REGISTER_RESULT;

import com.opensymphony.xwork2.ActionSupport;

import entities.User;

public class Register extends ActionSupport {

    private User registerBean;
    private String userNameError = "register";
    private String emailError = "register";

    @Override
    public String execute()
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ExamModulePu");
        EntityManager em = emf.createEntityManager();
        UserService service = new UserServiceImplementation(em);
        REGISTER_RESULT result = service.registerUser(registerBean);


        if(result.isDuplicateEmail())
            setEmailError("register error");
        if(result.isDuplicateUserName())
            setUserNameError("register error");
        em.close();
        emf.close();
        return result.toString();
    }

    public User getRegisterBean() {
        return registerBean;
    }

    public void setRegisterBean(User registerBean) {
        this.registerBean = registerBean;
    }

    public String getEmailError() {
        return emailError;
    }

    public void setEmailError(String emailError) {
        this.emailError = emailError;
    }

    public String getUserNameError() {
        return userNameError;
    }

    public void setUserNameError(String userNameError) {
        this.userNameError = userNameError;
    }
}

我怎样才能使这些变量对 jsp 可用?还是有更好的方法来完成我想做的事情?

4

1 回答 1

1

要为验证失败的字段提供一些 CSS 类,您可以使用cssErrorClass属性。

<s:textfield name="registerBean.userName" placeholder="Username*" cssErrorClass="register error" required="" />

并在您的操作中使用addFieldError方法将错误添加到特定字段。

于 2013-06-17T09:05:45.613 回答