1

我正在尝试ModelDriven使用注释在服务器端使用方法+验证。但似乎注释根本不起作用。我有一个简单的User类,有name, surname,email等:

@Entity
@Table(name = "USER")
@Validation
public class User {
    private Long id;
    private String name;
    private String gender;
    private String country;
    private String aboutYou;
    private Boolean mailingList;
    ...
    @Column(name = "USER_NAME")
    @RequiredStringValidator(message = "User Name field is empty.")
    public String getName() {
        return name;
    }

    @Column(name = "USER_GENDER")
    @RequiredStringValidator(message = "User Gender field is empty.")
    public String getGender() {
        return gender;
    }
...
}

@RequiredStringValidator用于验证gendername字段。

UserAction处理请求的课程是:

@Validation
public class UserAction extends ActionSupport implements ModelDriven<User> {

    private static final long serialVersionUID = -6659925652584240539L;

    private User user = new User();
    private List<User> userList = new ArrayList<User>();
    private UserDAO userDAO = new UserDAOImpl();

    @VisitorFieldValidator(message = "", appendPrefix = false)
    public User getModel() {
        return user;
    }

    /**

     * To save or update user.
     *
     * @return String
     */
    @VisitorFieldValidator(message = "", appendPrefix = false)
    public String saveOrUpdate() {
        userDAO.saveOrUpdateUser(user);
        return SUCCESS;
    }

    /**
     * To list all users.
     *
     * @return String
     */
    public String list() {
        userList = userDAO.listUser();
        return SUCCESS;
    }

    @VisitorFieldValidator(message = "", appendPrefix = false)
    public User getUser() {
        return user;
    }

    ...
}

我使用@VisitorFieldValidator(message = "", appendPrefix = false)它是因为我已经读过它是需要的,以防万一ModelDriven

struts.xml的是:

<struts>
    <package name="default" extends="hibernate-default">
        <action name="saveOrUpdateUser" method="saveOrUpdate" class="com.tutorials4u.web.UserAction">
            <result name="success" type="redirect">listUser</result>
            <result name="input">/register.jsp</result>
        </action>
        <action name="listUser" method="list" class="com.tutorials4u.web.UserAction">
            <result name="success">/register.jsp</result>
            <result name="input">/register.jsp</result>
        </action>
</struts>

我读过它需要定义<result name="input">验证。

这是我register.jsp在其中定义了必须验证的字段:

<s:form action="saveOrUpdateUser" method = "POST" validate="true">
    <s:actionerror/>
    <s:fielderror />
    <s:push value="user">
        <s:hidden name="id" />
        <s:textfield name="name" label="User Name" />
        <s:radio name="gender" label="Gender" list="{'Male','Female'}" />
        <s:select name="country" list="{'India','USA','UK'}" headerKey=""
                  headerValue="Select" label="Select a country" />
        <s:textarea name="aboutYou" label="About You" />
        <s:checkbox name="mailingList"
                    label="Would you like to join our mailing list?" />
        <s:submit />
    </s:push>
</s:form>

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <description>struts2</description>
    <display-name>struts2</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

问题是这一切都行不通。我仍然为此奋斗了 4 个小时,我脑海中唯一的想法告诉我,进行my_own_bicycle验证比使用这种方便的解决方案进行验证更容易。

4

1 回答 1

0

首先,Validation注释已被弃用。

其次,字段名称应准确映射操作中的对象。所以,push标签是没用的,User对象被modelDriven拦截器全部推送到值栈。

通知 Struts 您正在使用validate="true"客户端 javascript 验证。

确保您s:head在 JSP 中启用了标记和 javascript。

于 2013-07-29T21:17:44.520 回答