2

i'm developping a program in java ee.

i know how to display a view from a servlet with a code like this

this.getServletContext().getRequestDispatcher( VUE).forward( request, response );

where my view (VUE) is in a jsp page.

i know also how to return a json object with something like that

response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonErreurEtRole); 

I'm using google Gson to convert java objects.

Now i want to return a json object that contain a view in one field and some others messages in others field.

But i can't find how to do it.

Is there a way for that ?

EDIT

I will try to be more clear,

My problem is that : users ara requesting content by calling a servlet (a controller), i check their right on that content. And if they don't have right i send a message like "you don't have right ..." that message should not replace any content in the html page but just appear as a notification.

So i wanted to return a json object like [message, content]

by the way i solved my problem by sending only message in a json object if user don't have right and html content if he has. And in je jquery code i'm testing the return type of the response.

I used this post :

jQuery ajax returned data: json and html mix?

8
  • I think if you have a String field in your object, for example obj.html = "<p class=\"fancy-par\">Look at me!</p>"; GSon will be able to serialize it properly. Have you tried that? Commented Sep 30, 2014 at 8:08
  • i can do like that but my views is elsewhere in a jsp page and i want to maintain that separation. The views are complex. Commented Sep 30, 2014 at 8:11
  • Well I'm not aware of a way to capture the html created from a jsp on server side before sending it to the client. You could use some other templating technics to create your view html. Velocity for instance. This way you could create the html of your view and save it to string fields and do whatever you want with it, including writing it to servlet's output stream, i.e. sending it to the client just as a jsp does. Commented Sep 30, 2014 at 8:18
  • what is jsonErreurEtRole ? Commented Sep 30, 2014 at 8:51
  • 1
    @A4l ok thanks i will look at it. JsonErreurEtRole is a String variable. it contain my json object. Commented Sep 30, 2014 at 9:07

0

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.