用户名可用性检查使用Struts2和AJAX调用

时间:2016-09-06 20:37:49

标签: javascript java jquery ajax struts2

我正在使用Struts2作为框架构建一个Web应用程序。正如标题所示,我试图使用AJAX调用来检查用户名的可用性(如果有更容易实现相同结果的话,请打开其他建议。)

现在我有两个问题:

  • 我的doPost函数中的请求为空。
  • 即使我将uname设置为字符串,也没有任何反应。

以下是我的代码。

的Login.jsp

<s:form action="login.action" method="post" data-parsley-validate="">
            <s:textfield name="user.username" key="Username" id="username" onchange="checkUsername()"/><span class="status"></span>
            <s:submit method="doTheThing" align="center" onclick="hide()"/>
        </s:form>

的Javascript

function checkUsername(){
    var uname = $(username).val();
    if (uname.length > 3) {
        $(".status").html("Checking availability...");
        $.ajax({
            type : "POST",
            url : "usernameCheck.action",
            data : "uname=" + uname,
            success : function(msg) {
                $(".status").ajaxComplete(
                        function(event, request, settings) {
                            $(".status").html(msg);
                        });
            }
        });
    }

    else {
        $(".status").html("username should be at least 3 characters");
    }
}

check.java

public class check extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ActionSupport connectionSupport = new ActionSupport();

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Connection connection = null;
            String URL = "172.ExampleIP";
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(URL, "user","pass");
            String uname = request.getParameter("user.username");
            PreparedStatement ps = connection.prepareStatement("select username from users where username=?");
            ps.setString(1, uname);
            ResultSet rs = ps.executeQuery();

            if (!rs.next()) {
                out.println("<b>" + uname + "</b> is avaliable");
            } else {
                out.println("<font color=red><b>" + uname + "</b> is already in use</font>");
            }
            out.println();
        } catch (Exception ex) {
            out.println("Error ->" + ex.getMessage());
        } finally {
            out.close();
        }
    }

    public void doGet()
            throws ServletException, IOException {      
        doPost(ServletActionContext.getRequest(), ServletActionContext.getResponse());
    }
}

struts.xml中

<action name="usernameCheck" class="java.com.my.package.check" method="doGet"></action>

我不确定这是否是重要信息,但表单是在JQuery模式框中。

1 个答案:

答案 0 :(得分:-1)

我找到了解决方案。我根据Andrea的建议更改了我的java类以扩展ActionSupport。我将struts动作结果更改为stream类型。

<action name="usernameCheck" class="java.com.my.package.check" method="doGet">
    <result type="stream">
        <param name="contentType">text/html</param>
        <param name="inputName">stream</param>
    </result>
</action>

我在Java类中添加了以下内容

private InputStream stream;

public InputStream getStream() {
        return stream;
    }

并将我的JavaScript成功函数更改为以下内容:

success : function(msg) {
    $(".status").html(msg);
}

谢谢大家的所有输入。