0

I have a MVC- structured web application. I have both application-, session-, and request scoped data, And I use a custom made Request Based MVC framework, similar to the ones in Spring and Struts. I have used this question's answer as a tutorial.

I have a java object called ShowModel, which is passed as session scoped data. I use this to keep track of the users selection of visible components on the webpages.

All of the possible visibility selections are represented by a checkbox. They are all set to default of visible/checked when first setting the session data.

I have a listener on all of the checkboxes, that registers change, by class name "toggle", and sends it's id and checked/unchecked status by ajax to the server/servlet. See code example 1. I want to state that my experience with ajax is very limited.

As all of my calls are intercepted by my Front Controller Servlet, I needed to make a corresponding action, to execute the ajax POST-request. This code has been successfully reached, and executed. See code example 2.

My issue, however, is that my action pattern forces redirections. And in some mysterious way, the ajax object responsetext turns out to be the entire html of my index page.

My datamodel is already updated, but as it turns out, this is a faulty approach, due to the front controller strategy pattern.

So does anyone know of a way I can update my session scoped object's variables, without reloading the entire page?

Code example 1

$(document).ready(
    function() {
        $('.toggle').change(function() {        
            var id = this.value;
            var checked = this.checked; 

            var json = new Object();
            json.id = id;
            json.checked = checked;

            $.ajax({
                    url: "selectionmodelupdate",
                    type: 'POST',
                    dataType: 'json',
                    data: JSON.stringify(json),
                    contentType: 'application/json',
                    mimeType: 'application/json',

                    success: function (data) {
                        $('.event').each(function (index, event) {
                            //Here I will put code, to update the ".event"'s to be visible or not

                        }); 
                    },
                    error:function(data,status,er) {
                        console.log(arguments);
                        alert("error: "+data+" status: "+status+" er:"+er);
                    }
            });

        });
    });
});

Code Example 2

    public class SelectionModelUpdateAction implements Action {

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();
        ShowModel showModel = (ShowModel) session.getAttribute("showmodel");
        AppData appData = AppData.getInstance();
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8"); 

        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String json = "";
        if(br != null){
            json = br.readLine();
        }

        JsonObject jsonobject = new JsonParser().parse(json).getAsJsonObject();    
        boolean checked = jsonobject.get("checked").getAsBoolean();

        String id = jsonobject.get("id").getAsString();

        if(id.equals("A")){
            showModel.setASelected(checked);
            response.getWriter().write("{isSuccess: true}");
            return "index";
        }
        else if (id.equals("B")){
            showModel.setBSelected(checked);
            response.getWriter().write("{isSuccess: true}");
            return "index";
        }
        else if (id.equals("C")){
            showModel.setCSelected(checked);
            response.getWriter().write("{isSuccess: true}");
            return "index";
        }
        response.getWriter().write("{isSuccess: false}");
        return "index";
    }
}
Community
  • 1
  • 1
jumps4fun
  • 3,994
  • 10
  • 50
  • 96
  • add `/` to ajax url like: `url: "/selectionmodelupdate"` – Abhishek Nayak Mar 27 '14 at 16:45
  • @Rembo Would you mind telling me what that is supposed to do? I can tell you that the single word url is intercepted by my front controller (see pattern in link in question), and a strategy-pattern based action library is accessed. In other words selectionmodelupdate is a string that I use as a key, when accessing a hashmap of action-classes. Changing the url to `/selectionmodelupdate` only gives me a "not found" error. – jumps4fun Mar 28 '14 at 08:15
  • some answer have posted in SO that adding `/` in beginning of url value **solved responsetext turns out to be the entire html issue** [look here](http://stackoverflow.com/questions/5637539/codeigniter-jquery-ajax-call-returns-full-html-page-instead-of-my-echo) , i thought it may help you. – Abhishek Nayak Mar 28 '14 at 09:40
  • Thanks for trying :) I have spent a day solving this problem in an entirely different way. No Ajax at all, and my functionality works as I want it. The question is probably to big and specific to help anyone else, so I might delete it in a while. Thanks anyway for trying :) – jumps4fun Mar 28 '14 at 15:26

0 Answers0