如何限制在使用jquery的表中已经拥有的数据库中添加文本字段值

时间:2014-07-16 13:39:36

标签: jquery

我在Java服务器页面的数据库中获得了HTML表中的一些值,在这个页面中我有一个文本字段来从用户那里获取输入。现在,如果用户输入的值在HTML表或数据库表中不可用,即不重复,我会使用另一个页面在数据库表中添加该值。如果该值已在HTML表或数据库中可用,即重复,则不应在数据库表中添加此值。 我必须使用j查询来做到这一点。我不是在使用j子。

请帮助我任何人。

GetUserType.jsp


    <%@ page import="java.util.*"%>
    <%@ page import="com.nacre.lawfirm.dto.UserTypeTO"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"   prefix="c" %>
    <c:set var="path" value="${pageContext.servletContext.contextPath}"></c:set>
    <!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>DisplayUserTypes</title>
    <script type="text/javascript" src="js/jquery-1.7.2.js"></script>
    <script type="text/javascript" language="javascript">
    $(document).ready(function(){

        $("td").each(function(){

            arr.push($(this).text());
            });

        $.each(arr,function(index,value){

            var v=$("#usertype").val();
            alert(v);

            });

    });
    </script>
    </head>
    <body bgcolor="orange">
    <br>
    <center>
    <div id="wrapper">
    <fieldset>
    <legend style="background-color: #FFFF00;" ><font color="red" size="9">Available Users</font></legend>
    <%-- getting the attribute from Action servlet --%>
     <% 
    Object object=request.getAttribute("arraylist");
    %>

    <%-- type casting the object to arrayList,  iterating values using iterator and printing on browser--%> 
    <%ArrayList arraylist=(ArrayList)object;%> 

    <TABLE BORDER="1"  id="usertypeId" class="userClass" cellspacing="0">
    <thead>
    <tr> 
        <th>UserType</th>  
    </tr> 
    </thead>
    <tbody>
    <%
    ListIterator listIterator=arraylist.listIterator();
    while(listIterator.hasNext()) {
        Object obj=listIterator.next();
        UserTypeTO userTypeTO=(UserTypeTO)obj;
    %>
        <TR class ="rowId">
        <TD class ="user" id="<%= userTypeTO.getUserTypeName()%>"><%= userTypeTO.getUserTypeName() %></TD>
        <%      }%>
        </TR>
    </tbody> 

    </table> <br>
    </fieldset>

    </div> 
    <form action="${path}/AddUserTypeAction" method="post">
    User Type Name<input type="text" name="usertype" class="usertype" id="usertype"  ><br>
    <input type="submit" name="Add-User" value="AddUserType"/>
    </center>
    </form>
    </body>
    </html>




GetUserTypeAction.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nacre.lawfirm.delegate.LawFirmDelegate;
import com.nacre.lawfirm.dto.UserTypeTO;

/**
 * Servlet implementation class GetUserTypeAction
 */
@WebServlet("/GetUserTypeAction")
public class GetUserTypeAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public GetUserTypeAction() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LawFirmDelegate lawFirmDelegate=new LawFirmDelegate();

        //creating array list reference variable with null
        List<UserTypeTO> userTypeList=null;
        try {
            //getting array list object from delegate class using that class object
            userTypeList = lawFirmDelegate.getUserType();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e);
        }

        request.setAttribute("arraylist", userTypeList);

        //sending control to GetUserType.jsp
        RequestDispatcher requestDispatcher=request.getRequestDispatcher("/UI/COMMONUI/GetUserType.jsp");
        requestDispatcher.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req, resp);
    }

}




AddUserTypeAction.java


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nacre.lawfirm.delegate.LawFirmDelegate;
import com.nacre.lawfirm.exception.DatabaseException;



/**
 * Servlet implementation class AddUserTypeAction
 */
@WebServlet("/AddUserTypeAction")
public class AddUserTypeAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddUserTypeAction() {
        super();
    }


   /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");

        String usertype=request.getParameter("usertype");

        LawFirmDelegate lawFirmDelegate=new LawFirmDelegate();
        try {
            lawFirmDelegate.addUserType(usertype);
        } catch (DatabaseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        RequestDispatcher requestDispatcher=request.getRequestDispatcher("GetUserTypeAction");
        requestDispatcher.forward(request, response);
    }
}

这是

1 个答案:

答案 0 :(得分:0)

我不确定您如何保留页面中的数据。如果您使用的是JSON格式的数据或Javascript对象,则可以使用以下脚本来识别用户输入的值是否存在于列表中

这里的lstObjects是数据库中值的集合。

var txtBoxValue = //Get the value from textbox.
if ($.inArray(txtBoxValue, lstObjects) == -1)
{
   //The value is not in the list. Its a new value.
}

这是一种检查方式,否则请使用 $。grep 对于grep,您可以参考以下链接

Grep vs Filter in jQuery?

jquery grep on json object array

我希望这会对你有帮助。,:)