Struts2使用注释和输入标记进行表单验证

时间:2016-02-17 09:27:08

标签: java html jsp struts2 struts-tags

我正在学习struts2中的表单验证。我已成功实现它,当文本字段为空时,它在jsp页面上显示错误消息。但现在我删除了<s:textfield>,而不是使用<input type="text">标记。现在这个不起作用可以让我知道如何实现这个目标吗?

以下是我的代码

@Namespace("/User")
@ResultPath("/")
@Result(name = "input", location = "pages/registration.jsp")
public class UserAction extends ActionSupport implements ServletRequestAware, ModelDriven<User>
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    User user = new User();
    private HttpServletRequest servletRequest;
    List<User> users = new ArrayList<User>();

    public User getModel()
    {
        return user;
    }

    public User getUser()
    {
        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
    }

    public HttpServletRequest getServletRequest()
    {
        return servletRequest;
    }

    public void setServletRequest(HttpServletRequest servletRequest)
    {
        this.servletRequest = servletRequest;
    }

    public List<User> getUsers()
    {
        return users;
    }

    public void setUsers(List<User> users)
    {
        this.users = users;
    }

    @Action(value = "register", results = {@Result(name = "input", location = "pages/registration.jsp"), @Result(name = "success", location = "pages/registration.jsp")})
    // @InputConfig(methodName = "registeruser")
    public String register()
    {
        return SUCCESS;
    }

    @Action(value = "registeruser", results = {@Result(location = "pages/welcome.jsp")})
    public String registeruser()
    {
        UserDao userDao = new UserDaoImpl();

        try
        {
            String uploadFilePath = servletRequest.getSession().getServletContext().getRealPath("/").concat("/User/images");
            CommonUtils.uploadFile(user.getFile(), uploadFilePath, user.getFileFileName());
            userDao.save(user, uploadFilePath);
        }
        catch (Exception e)
        {
            System.out.println(e);
        }

        return SUCCESS;
    }

    public void validate()
    {
        System.out.println("in");
        if ("".equals(user.getFirstName()))
        {
            addFieldError("firstName", "First name should not be blank");       
        }
        if ("".equals(user.getLastName()))
        {           
            addFieldError("lastName", "Last name should not be blank");         
        }
    }

}

Jsp Page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="/struts-tags" prefix="s" %> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Registration</title>
    </head>
    <body>
        <form action="registeruser" method="post" enctype="multipart/form-data">
        <s:actionerror/>
            <table>
                <tr>
                    <td>First Name</td>
                    <td><input type="text" name="firstName" id="firstName"></td>
                    <%-- <td><s:textfield name="firstName"></s:textfield></td> --%>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td><input type="text" name="lastName" id="lastName"></td>
                </tr>
                <tr>
                    <td>Contact Number</td>
                    <td><input type="text" name="contactNumber" id="contactNumber"></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><input type="text" name="email" id="email"></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td><textarea rows="5" cols="50" name="address" id="address"></textarea></td>
                </tr>
                <tr>
                    <td>Gender</td>
                    <td><input type="radio" name="gender" id="male" value="male">Male<br />
                    <input type="radio" name="gender" id="female" value="female">Female</td>
                </tr>
                <tr>
                    <td>Degree</td>
                    <td><input type="checkbox" name="degree" id="graduation"
                        value="graduation">Graduation<br />
                    <input type="checkbox" name="degree" id="postgraduation"
                        value="postgraduation">Post Graduation</td>
                </tr>
                <tr>
                    <td>Technology</td>
                    <td><select name="technology" id="technology">
                            <optgroup label="Java">
                                <option value="spring">Spring</option>
                                <option value="hibernate">Hibernate</option>
                                <option value="struts2">Struts2</option>
                            </optgroup>
                            <optgroup label=".Net">
                                <option value="asp">ASP</option>
                                <option value="vb.net">VB.NET</option>
                            </optgroup>
                    </select></td>
                </tr>
                <tr>
                    <td>Upload file</td>
                    <td><input type="file" name="file" id="file"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Save"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

如果删除<s:textfield />并使用纯HTML <input type="text" />,则需要手动实现框架标记提供的功能。

Struts2使用主题生成HTML,并使用默认主题(XHTML)自动生成fieldError。另请注意,使用纯HTML标记时,您需要手动设置值。我建议你继续使用Struts2标签,但要回答这个问题,相当于

<s:textfield name="firstName" />
纯HTML中的

<input type="text" name="firstName" value="<s:properry value='firstName'>" />
<s:fielderror fieldName="firstName" />