[JAVA] Memo to get with Struts2 + Ajax

Introduction

Completely just a note I use it when fetching data without screen transition

environment

Struts2.0.11.2 (too old ...) Java 8 (old ...)

coding

JSP

sample.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<input type="button" value="Send" onClick="getSampleData();" />

jsonArray.jsp


<!--Do I have to receive the response like this ...?-->
<%@ page language="java" contentType="text/javascript; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:property value="jsonArray" escape="false"/>

JavaScript(Ajax)

sample.js


function getSampleData() {
  new Ajax.Request("GetSampleData.action", {
    method : 'GET',
    onSuccess : function(res) {
      var arr = JSON.Parse(res.responseText);
      arr.forEach(function(data){
        console.log(data.id + ":" + data.name);
      }
    },
    onError : function() {
      console.log("Error");
    },
    onComplete : function() {
      console.log("Complete");
    }
  })
}

struts.xml

struts.xml


<action name="GetSampleData" class="PATH.BuildSampleData">
  <result>PATH/jsonArray.jsp</result>
</action>

Action Class

BuildSampleData.java


package hogehoge.fugafuga

import java.util.ArrayList;
import net.sf.json.JSONObject;

public class BuildLoginData {
  private ArrayList<JSONObject> jsonArray = new ArrayList<JSONObject>();

  //Execute if Action method is not specified()Is called
  public String execute() throws Exception {
    JSONObject tarou = new JSONObject();	
    tarou.put("id", 1);
    tarou.put("name", "tarou");
    jsonArray.add(tarou);

    JSONObject hanako = new JSONObject();	
    hanako.put("id", 2);
    hanako.put("name", "hanako");
    jsonArray.add(hanako);

    return "success";
  }

  public ArrayList<JSONObject> getJsonArray() {
    return jsonArray;
  }

  public void setJsonArray(ArrayList<JSONObject> jsonArray) {
    this.jsonArray = jsonArray;
  }
}

Impressions

It's annoying to have to prepare jsp when receiving the response from the server. Is there any other way ...?

Recommended Posts

Memo to get with Struts2 + Ajax
How to get started with slim
How to get along with Rails
I tried to get started with WebAssembly
[Note] How to get started with Rspec
How to get along with Rails
I tried to manage struts configuration with Coggle
Rails beginners tried to get started with RSpec
How to get resource files out with spring-boot
Steps to deploy Struts 2.5.8
I tried to get started with Spring Data JPA
A story about trying to get along with Mockito
Get started with "Introduction to Practical Rust Programming" (Day 3)
Get started with Gradle
How to get started with creating a Rails app
I want to get along with Map [Java beginner]
How to get jdk etc from oracle with cli
How to get values in real time with TextWatcher (Android)
How to get started with Gatsby (TypeScript) x Netlify x Docker
A memo to build Jitsi Meet on Azure with docker-compose
Now is the time to get started with the Stream API
How to get started with JDBC using PostgresSQL on MacOS
[Organization] To you who get messed up with render & redirect_to
Get started with Spring boot
Role organization memo in Struts
Implement jCaptcha reload with ajax
How to number (number) with html.erb
Get started with DynamoDB with docker
How to update with activerecord-import
Connect to DB with Java
Connect to MySQL 8 with Java
Java study memo 2 with Progate
XVim2 introduction memo to Xcode12.3
How to get boolean value with jQuery in rails simple form
How to install Adopt OpenJDK on Debian, Ubuntu with apt (-get)
What to do if you get angry with OpenSSL with pyenv install
[Rails] What to do if you can't get parameters with form_with