ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|
Struts 1 Action の基本形
public class LoginAction extends Action {
@Override
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 1. ActionForm をキャスト
LoginForm loginForm = (LoginForm) form;
String userId = loginForm.getUserId();
String pass = loginForm.getPassword();
// 2. 業務ロジック
UserService svc = new UserService();
User user = svc.authenticate(userId, pass);
if (user == null) {
ActionMessages errors = new ActionMessages();
errors.add("login", new ActionMessage("error.login.failed"));
saveErrors(request, errors);
return mapping.findForward("failure");
}
// 3. セッション格納
request.getSession().setAttribute("loginUser", user);
// 4. 遷移先を返す
return mapping.findForward("success");
}
}
struts-config.xml の設定
<?xml version="1.0" encoding="UTF-8"?>
<struts-config>
<!-- form bean 定義 -->
<form-beans>
<form-bean name="loginForm" type="com.example.LoginForm"/>
</form-beans>
<!-- action 定義 -->
<action-mappings>
<action path="/login"
type="com.example.LoginAction"
name="loginForm"
scope="request"
validate="true"
input="/WEB-INF/jsp/login.jsp">
<forward name="success" path="/WEB-INF/jsp/home.jsp"/>
<forward name="failure" path="/WEB-INF/jsp/login.jsp"/>
</action>
</action-mappings>
</struts-config>
ActionForm
public class LoginForm extends ActionForm {
private String userId;
private String password;
public String getUserId() { return userId; }
public void setUserId(String v) { this.userId = v; }
public String getPassword() { return password; }
public void setPassword(String v) { this.password = v; }
@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest req) {
ActionErrors errors = new ActionErrors();
if (userId == null || userId.isEmpty()) {
errors.add("userId", new ActionMessage("error.required.userid"));
}
if (password == null || password.length() < 8) {
errors.add("password", new ActionMessage("error.password.length"));
}
return errors;
}
@Override
public void reset(ActionMapping mapping, HttpServletRequest req) {
this.userId = null;
this.password = null;
}
}
JSP 側
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:form action="/login">
ユーザーID: <html:text property="userId"/><br/>
<html:errors property="userId"/><br/>
パスワード: <html:password property="password"/><br/>
<html:errors property="password"/><br/>
<html:submit value="ログイン"/>
</html:form>
DispatchAction(1 Action 複数メソッド)
機能ごとに Action クラスを作ると膨大になるため、DispatchAction で 1 クラスに複数機能を集約:
public class UserAction extends DispatchAction {
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws Exception {
req.setAttribute("users", new UserService().findAll());
return mapping.findForward("list");
}
public ActionForward show(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws Exception {
long id = Long.parseLong(req.getParameter("id"));
req.setAttribute("user", new UserService().findById(id));
return mapping.findForward("detail");
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws Exception {
// ...
return mapping.findForward("list");
}
}<!-- struts-config.xml: parameter で method を切り替え -->
<action path="/user"
type="com.example.UserAction"
parameter="method"
scope="request">
<forward name="list" path="/WEB-INF/jsp/user/list.jsp"/>
<forward name="detail" path="/WEB-INF/jsp/user/detail.jsp"/>
</action>
<!-- 呼び出し: /user.do?method=list / /user.do?method=show&id=1 -->
Action Chain (forward の連鎖)
1 Action の結果を別の Action に流す:
<action path="/login" type="com.example.LoginAction">
<!-- 成功時に /home に forward (chain) -->
<forward name="success" path="/home.do" redirect="false"/>
</action>
<action path="/home" type="com.example.HomeAction">
<forward name="default" path="/WEB-INF/jsp/home.jsp"/>
</action>
注意: Action Chain は Struts 1.x で多用されたが、デバッグが困難でアンチパターン扱い。共通ロジックはサービス層 (Service) に切り出すのが正解。
Spring MVC への移行
Struts 1 は 2013 年 4 月にサポート終了。CVE が積み上がっているため、現役の Web アプリは Spring MVC への移行が必須:
// Struts 1 → Spring MVC
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService; // DI で注入
@GetMapping
public String list(Model model) {
model.addAttribute("users", userService.findAll());
return "user/list"; // → /WEB-INF/jsp/user/list.jsp
}
@GetMapping("/{id}")
public String show(@PathVariable Long id, Model model) {
model.addAttribute("user", userService.findById(id));
return "user/detail";
}
@PostMapping("/save")
public String save(@Valid @ModelAttribute UserForm form,
BindingResult result,
RedirectAttributes ra) {
if (result.hasErrors()) return "user/edit";
userService.save(form);
ra.addFlashAttribute("msg", "保存しました");
return "redirect:/user";
}
}
Struts 1 vs Spring MVC
| 項目 | Struts 1 | Spring MVC |
|---|---|---|
| サポート | 2013/04 EOL | 継続中 |
| 設定 | struts-config.xml | アノテーション |
| Form | ActionForm (継承必須) | POJO + @ModelAttribute |
| DI | なし(手動 new) | 標準搭載 |
| REST | 非対応(手作り) | @RestController で標準対応 |
| テスト | 困難 (ServletContainer 必須) | MockMvc で容易 |
段階的移行: DelegatingActionProxy
一気に書き換えられない場合は Struts 1 + Spring の併用:
<!-- struts-config.xml: Action を DelegatingActionProxy 経由に -->
<action path="/login"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="success" path="/WEB-INF/jsp/home.jsp"/>
</action>
<!-- applicationContext.xml: Bean として Action を定義 -->
<bean name="/login" class="com.example.LoginAction">
<property name="userService" ref="userService"/>
</bean>
FAQ
Q: Struts 2 ではどうなる?
A: Struts 2 は別物(旧 WebWork)。Action は POJO で execute() が String を返す形に。Struts 1 とは互換性なし。
Q: 既存の Struts 1 アプリはすぐ捨てるべき?
A: 公開系なら最優先。社内系でも段階的移行を計画。CVE-2014-0114 等の重大脆弱性あり。
Q: ActionServlet は何をしている?
A: Struts 1 の FrontController。web.xml で *.do をマップし、URL から ActionMapping を解決して Action を呼ぶ。Spring MVC では DispatcherServlet がそれに相当。
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページはありません
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript base URL 取得方法|window.location.origin と SSR/Node.js 対応
- 4 YouTube Data API v3 エラー一覧|403/400/404 の主要原因と切り分け
- 5 Spring Frameworkのアノテーション一覧
- 6 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- IPv6とは|128bitアドレス・コロン16進表記/::省略・リンクローカル・SLAAC・デュアルスタック NEW 2026-06-22 12:34:44
- MAC アドレスフィルタリングの仕組みと限界 | ネットワーク入門 NEW 2026-06-22 12:19:10
- VPNとは|暗号トンネル・サイト間/リモートアクセス・IPsec/SSL-VPN/WireGuardを解説 NEW 2026-06-22 12:19:10
- WebRTC とは ブラウザ間 P2P の音声・映像・データ通信 | ネットワーク入門 NEW 2026-06-22 12:17:25
- HTTP/2 とは 多重化・HPACK・バイナリフレーム | ネットワーク入門 NEW 2026-06-22 12:17:25
- Web通信プロトコル入門 HTTP/2・HTTP/3・WebSocket・gRPC・WebRTC | ネットワーク入門 NEW 2026-06-22 12:17:25
- gRPC とは HTTP/2 + Protocol Buffers の高速 RPC | ネットワーク入門 NEW 2026-06-22 12:17:25
- HTTP/3 (QUIC) とは UDP ベースの低遅延 Web 通信 | ネットワーク入門 NEW 2026-06-22 12:17:25
- WebSocket とは 全二重リアルタイム通信 ws/wss | ネットワーク入門 NEW 2026-06-22 12:17:25
- 証明書と認証局(CA)とは|X.509・信頼チェーン・DV/OV/EV・失効(CRL/OCSP)を解説 NEW 2026-06-22 12:17:24
- ファイアウォールとは|パケットフィルタ・ステートフル・DMZ・次世代FW(L4/L7)を解説 NEW 2026-06-22 12:17:24
- iptables/nftablesとは|テーブル・チェーン・ルール例・永続化をLinux視点で解説 NEW 2026-06-22 12:17:24
- HAProxy とは frontend/backend と設定例 | ネットワーク入門 NEW 2026-06-22 12:17:24
- CDN とは エッジキャッシュ・TTL・Cloudflare/CloudFront | ネットワーク入門 NEW 2026-06-22 12:17:24
- TLS/SSLの仕組み|ハンドシェイク・暗号スイート・前方秘匿性・証明書検証をわかりやすく解説 NEW 2026-06-22 12:17:24
コメントを削除してもよろしいでしょうか?