0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

<sj:a>を使って非同期でJSPの結果を取得する

Last updated at Posted at 2014-01-20

概要

コンテンツの一部を非同期でJSPを呼び出して取得する方法は、Struts2-jQueryプラグインを使うことで簡単に実現できます。
例えば検索画面と検索結果を別々のJSPにしておいて、検索結果は非同期で取得する、など。

以下は下地となる検索画面。呼び出し部分はsjのaタグ、検索結果はdivに反映される、と定義します。

<sj:a button="true" id="search" name="search" 
      href="list" targets="resultList">検索</sj:a>
<div id="resultList"></div>

ボタンを押して取得したJSPのレスポンスは、targets属性に指定した値をidにもつ要素に格納します。
この例ではすぐ下のdivですね。

href属性に指定したパスから得られるレスポンスを格納します。
レスポンスはただのテキストでも良いですし、Actionクラスを実行するパスを指定して、その結果指定するJSPから得られるHTMLレスポンスでも問題ありません。例えば以下のようなJSPです。

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%-- Result List --%>
<!DOCTYPE html>
<table class="table table-bordered table-striped">
	<thead>
		<tr>
			<th>id</th>
			<th>title</th>
			<th>editStatus</th>
		</tr>
	</thead>
	
	<tbody><s:iterator value="resultList" var="resultMap" status="stts">
		<tr>
			<td><s:property value="id"/></td>
			<td><s:property value="title"/></td>
			<td><s:property value="editStatus"/></td>
		</tr>
	</s:iterator></tbody>
</table>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?