1

I know that in Struts2 can be used json plugin to return a json type result. A json could also be returned from the stream result like in this answer.

On the Struts2 docs page for Ajax result with JSP, I've found that it's possible to return dispatcher type result with JSP that outputs a JSON.

<%@ page import="java.util.Iterator,
         java.util.List,
         com.esolaria.dojoex.Book,
         com.esolaria.dojoex.BookManager" %>
<%
    String bookIdStr = request.getParameter("bookId");
    int bookId = (bookIdStr == null || "".equals(bookIdStr.trim())) 
        ? 0 : Integer.parseInt(bookIdStr);
    Book book = BookManager.getBook(bookId);
    if (book != null) {
        out.println(book.toJSONString());
        System.out.println("itis: " + book.toJSONString());
    }
%>

But it's using scriptlets to write JSON to the out. I know that using scriplets in JSP is highly discouraged. But I couldn't find the answer for my problem in this question How can I avoid Java code in JSP files, using JSP 2?. How can I use JSP result to generate a JSON object? Is there a better way to return JSON object from JSP?

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 2
    Is this a mere exercise in style, or there is a real-world use-case behind it that would lead to some kind of benefit in using JSON from DISPATCHER result instead of getting it with JSON or STREAM result as usual ? Where all of this comes from, which is the point ? – Andrea Ligios Mar 03 '16 at 08:54
  • @AndreaLigios The style that is used in the code is bad because it's using scriptlets. BTW I will accept the answer if you undelete it. – Roman C Mar 24 '16 at 07:20
  • I don't see scriptlets in my code, there's a single ``... BTW, undeleted. – Andrea Ligios Mar 24 '16 at 08:44
  • I'm really curious, could you please explain what is the reason behind this tons of edits and un-downvotes ? Not that I have anything to complain, I've always been for *live and let live*, I'm just confused. Please don't flag this comment before answering it, then I'll delete it myself since it's OT. – Andrea Ligios Mar 24 '16 at 08:46
  • @AndreaLigios I think it will help with future upvotes to my posts, because currently as it is *not fair*. I will not flag, there's *no justice* anyway, may be next time you post your offensive comment, don't do it, you are a nice guy. – Roman C Mar 24 '16 at 09:46
  • Well, I guess stop downvoting for the wrong reasons (downvoting a *bad* Q/A is right, downvoting a *not-enough-good* Q/A is wrong, IMHO) could definitely help. Also I believe (I've tried to tell you a couple of years ago in a comment, but you got me wrong) that a good way to get more upvotes is taking the time to format the question (especially writing in proper English: a lot of people have it as pet-peeve, and won't upvote an answer that seems written by Sitting Bull). We're in a tag with a low upvote rate, but that's my thought... code formatting+language formatting=more upvotes. Good luck – Andrea Ligios Mar 24 '16 at 10:12
  • @AndreaLigios You are right if you got > 10 upvotes then you take a time to format it to get next 10. It's a simple arithmetic. I you got 0 upvotes, you are thinking about delete the post, but staying neutral for the hope. When I'm browsing SO *having* a question it doesn't matter how it's formatted, I *seek* a solution that quickly fix my problem, and if I find it I'm upvoting as well. So I think the problem is not in formatting, I have a lot of post *well* formatted and 0 upvotes. I have a lot of posts upvoted by people who have found a solution. Buts the statistic says *bad*. – Roman C Mar 24 '16 at 10:41
  • It's not that *the problem* is in formatting, it's just that *eye-candy* wants (and does) its part. A good answer is a good answer, but If a question has two answers, one greatly formatted, the other one with a bunch of words with almost no punctuation, every user will try the first, then if it doesn't work they will come back and read the second. It doesn't matter if it's more good, if noone is encouraged to read it. P.S: I've got upvotes for answers that has been at 0 votes for even 4 years, so if the answer it's good, I never delete it. – Andrea Ligios Mar 24 '16 at 10:53

1 Answers1

2

You can return a JSP through the dispatcher result, then use <s:property /> tag to call an action method that will return the serialized data in the JSP.

You should also express the right contentType for your JSP:

public class DispatcherJsonAction extends ActionSupport {

    private Book book;

    @Action("dispatcherJson")
    @Result(name = ActionSupport.SUCCESS, location = "page.jsp")        
    public String execute(){
        book = loadBookSomeHow();
        return SUCCESS;
    }

    public String getJsonBook(){
        Gson gson = new Gson();
        try {
            return gson.toJson(book);
        } catch (Exception e){
            return gson.toJson(e.getMessage());
        }
    }

}

page.jsp:

<%@page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<s:property value="jsonBook" />
Roman C
  • 49,761
  • 33
  • 66
  • 176
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243