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";
}
}