【问题标题】:Struts2 form validation with annotation and input tag [duplicate]带有注释和输入标签的Struts2表单验证[重复]
【发布时间】:2016-02-17 09:27:07
【问题描述】:

我正在学习 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 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>

【问题讨论】:

  • 得到答案:D 你可以使用
  • 谢谢@Aleksandr M.. :)
  • 如果它有效,请记住接受答案@PradeepZaptech
  • @goodyzain 我没有,因为他的评论(在我的回答后一分钟发布)报告了我用过的拼写错误(

标签: java html jsp struts2 struts-tags


【解决方案1】:

如果您删除 &lt;s:textfield /&gt; 并改用纯 HTML &lt;input type="text" /&gt;,则需要手动实现框架标签提供的功能。

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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多