0

My requirement is

  1. I will have a JSP where emails for that particular user will be displayed

  2. The user can select the mails which he wish to delete

  3. On click of delete button the IDs of the check-box (which the user selected for deletion) has to be captured and given to server and the server in turn deletes it by running an HQL

Currently am handling this by joining the ids with pipe symbol and sending it in query-string as a single string object and in server side am tokenizing the ids and deleting it in iteration

below is the jquery script in which am joining the ids with pipe symbol

$("#deleteBtn").click(function() {
            var deleteIDList = "" ;

            $("[name='deleteCheckboxGroup']").each(function() {
                if($(this).is(':checked')) {
                    var chckId = $(this).attr("id") ;
                    deleteIDList+=chckId.split("_")[0] +"|";
                }   
            });
            if (deleteIDList!="") {
                // to remove pipe symbol from the last character, we are using 'slice' method of javascript
                deleteIDList = deleteIDList.slice(0,-1) ;
                var actionUrl = "deleteMessageText.htm?messageIDs="+deleteIDList ;
                $("#studentMessFrm").attr("action",actionUrl) ;
                $("#studentMessFrm").submit() ;
            }else {
                return false ;
            }           
        }) ;

and this is how i tokenize and delete the object from DB

public void deleteMessages(String messageIDs) throws ReadsException{

        //List<Long> deletedIdList = new ArrayList<Long>() ;

        StringTokenizer stringTokenizer = new StringTokenizer(messageIDs,"|") ;

        Map<String,Object> parameterMap = null ;
        while (stringTokenizer.hasMoreTokens()) {
            String query = "DELETE FROM MESSAGES MES WHERE MES.MESID=:MESID";
            parameterMap = new HashMap<String,Object>();
            parameterMap.put("MESID", Long.valueOf(stringTokenizer.nextToken()));
            executeUpdateOrDelete(query, parameterMap);
        }

        LOGGER.info("deleted successfully..");
    }

I know that this is unprofessional type of coding. I want the list of ids to be passed to MVC controller in requestParam and then the list ids should be deleted at "ONE SHOT".

Please suggest me on this. Thanks

Regards Arun

1 Answer 1

2

There are different ways of achieving this. My favourite is minimizing the use of Javascript when it's not really needed. Here is what I would try to do:

HTML form:

<form action="message/delete" method="POST">
    <input type="checkbox" name="messageId" value="7" />Hi!<br/>
    <input type="checkbox" name="messageId" value="33" />It's me!<br/>
</form>

Spring controller:

@RequestMapping("message/delete")
public void deleteMessages(Long[] messageId) {
    messageRepository.delete(messageId);
}

JPA Repository:

public void delete(Long... ids) {
    if (ids.length > 0) {
        entityManager.createQuery("DELETE FROM Message WHERE id IN :id").setParameter("id", ids).executeUpdate();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.