小编asl*_*lan的帖子

根据操作Spring MVC进行验证的最佳实践

我正在尝试使用Spring验证执行验证.我想知道执行验证的最佳做法是什么,主要取决于用户的行为,此后,我有三种不同的方法,但我不知道哪种方法最好.

假设,我们有以下类Foo进行验证,并根据用户执行的操作验证许多不同的验证规则.

public class Foo {
    private String name;

    private int age;

    private String description;

    private int evaluation;

    // getters, setters
}
Run Code Online (Sandbox Code Playgroud)

执行这些验证的最佳方法是什么(例如:在创建期间只需要名称和年龄,在评估操作期间,我只需要评估以进行验证等)

解决方案1:每个验证规则一个验证器类

public class CreateFooValidator implements Validator {
    //validation for action create
}
public class EvaluateFooValidator implements Validator {
    //validation for evaluate action
}
Run Code Online (Sandbox Code Playgroud)

解决方案2:一个带有多种方法的验证器类

public class FooValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return Foo.class.equals(clazz);
    }

    //the default validate method will be used to validate during create action

    @Override
    public void validate(Object target, Errors …
Run Code Online (Sandbox Code Playgroud)

java validation spring spring-mvc

11
推荐指数
1
解决办法
1万
查看次数

在表格中显示多对多关系的额外字段

我一直在寻找,但没有找到任何解决方案来做以下事情.

我试图显示一个包含让我们说用户列表的表.显示来自用户的字段并不是什么大问题,但对于关联字段来说它更复杂.假设我有以下表格:User,Post和UserPosts,在UserPosts中我们有一个标志main来指示给定帖子是否是用户的主要位置.

在我的数据表中,我必须显示用户的信息,它表示用户表中的字段+他/她的主要帖子.

到目前为止,我有这样的事情:

<table> 
    <thead> 
    <tr> 
        <th>name</th> 
        <th>FrstName</th>
        <th>Age</th> 
        <th>Main position</th>
        <th>Action</th> 
    </tr> 
        <tbody> 
           <c:forEach items="${listUsers}" var="user" varStatus="status">
            <tr>
                <td><c:out value="${user.name}" /></td> 
                <td><c:out value="${user.firstName}" /></td>
                <td><c:out value="${user.age}" /></td> 
                <td><c:out value="Position here" /></td> 
                <td>
                Delete
                <a href="/myAppli/user/${user.idUser}">Update</a>
                </td> 
            </tr> 
            </c:forEach>
        </tbody> 
</thead> 
Run Code Online (Sandbox Code Playgroud)

我认为对于位置我将不得不从给定列中给定用户的数据库主位置检索,但是如何设置此值(javascript,ajax?)

我是否以错误的方式思考,我应该使用关联类.我的意思是,检索main field = true的所有UserPosts?然后迭代userPosts而不是用户,在这种情况下,我会有这样的事情:

<c:forEach items="${listUserPosts}" var="userPost" varStatus="status">
        <tr>
            <td><c:out value="${userPost.user.name}" /></td> 
            <td><c:out value="${userPost.user.firstName}" /></td>
            <td><c:out value="${userPost.user.age}" /></td> 
            <td><c:out value="${userPost.post.postName}" /></td> 
            <td>
            Delete
            <a href="/myAppli/user/${userPost.user.idUser}">Update</a>
            </td> 
        </tr> 
</c:forEach>
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎很奇怪,也许我错了.

javascript datatable jsp many-to-many jstl

5
推荐指数
1
解决办法
279
查看次数

在 Maven 测试阶段为测试目的初始化数据库

我正在尝试执行以下操作:

  • 在 mvn 测试阶段执行一些数据库脚本到 hsqldb

  • 将该数据库用于测试目的

    我能够配置 maven,以便每次调用测试阶段时所有脚本都成功执行,但是(当然有一个 BUT),我所有的测试都失败了。

我的配置:

pom.xml

<build>
    <plugins>
        <!-- Plugin maven for sql -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>sql-maven-plugin</artifactId>

            <dependencies>
                <!-- Dependency to jdbc driver -->
                <dependency>
                    <groupId>org.hsqldb</groupId>
                    <artifactId>hsqldb</artifactId>
                    <version>${hsql-version}</version>
                </dependency>
            </dependencies>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
                <encoding>UTF-8</encoding>
                <driver>org.hsqldb.jdbcDriver</driver>
                <url>jdbc:hsqldb:mem:sweetdev_skill_db;shutdown=false</url>
                <settingsKey>hsql-db-test</settingsKey>
                <!--all executions are ignored if -Dmaven.test.skip=true-->
                <skip>${maven.test.skip}</skip>
            </configuration>
            <executions>
                <!--  Create integration test data before running the tests -->
                <execution>
                    <id>create-integration-test-data</id>
                    <phase>process-test-resources</phase>
                    <inherited>true</inherited>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <url>jdbc:hsqldb:mem:db;shutdown=false</url>
                        <autocommit>true</autocommit>
                        <orderFile>ascending</orderFile>
                        <fileset>
                            <basedir>${basedir}/src/test/resources/sql</basedir>
                            <includes>
                                <include>create.sql</include>
                                <include>insert.sql</include>
                            </includes>
                        </fileset>
                    </configuration> …
Run Code Online (Sandbox Code Playgroud)

sql spring unit-testing hsqldb maven

2
推荐指数
1
解决办法
7592
查看次数