
Kita lanjutkan belajar struts2 dengan judul Tutorial Action Chaining Apache Struts2. Mari kita lihat apa itu Action Chaining
The framework provides the ability to chain multiple actions into a defined sequence or workflow. This feature works by applying a Chain Result to a given Action, and intercepting its target Action’s invocation with a Chaining Interceptor.
“As a rule, Action Chaining is not recommended. First explore other options, such as the Redirect After Post technique.”
sumber: apache
Sekarang ayo kita mulai Tutorial Action Chaining Apache Struts2:
- Buka folder workspace anda, jangan lupa pelajari dulu Tutorial Basic Apache Struts2. Karena kita akan melanjutkan source codenya.
- Buat folder “action-chaining-struts2” di workspace anda
- Copy file “pom.xml” dan folder “src” di folder “basic-struts2” tutorial sebelumnya.
- Buka file “pom.xml”, rubah artifact-id “basic-struts2” ke “action-chaining-struts2”
- Rubah juga tag name dan description “Belajar Action Chaining Struts2”
- Buka IDE eclipse
- Pilih import >> existing maven project >> browse ke folder “action-chaining-struts2” >> finish
- Project baru action-chaining-struts2 sudah berhasil di load
Mari kita mulai melakukan perubahan selanjutnya
- Create package “com.wijaksana.belajar.struts2.action”
- Tambahkan class sebagai controller/action dengan nama “ActionA” di package “com.wijaksana.belajar.struts2.action” dan tambah extend “ActionSupport”
- Tambahkan properti “name” dan setting getter/setter-nya
123456789private String name;public String getName() {return name;}public void setName(String name) {this.name = name;} - Tambahkan @Override method “execute”
- Tambahkan script dibawah ini pada method “execute”
1return SUCCESS; - Tambahkan class sebagai controller/action dengan nama “ActionB” di package “com.wijaksana.belajar.struts2.action” dan tambah extend “ActionSupport”
- Tambahkan properti “name” dan setting getter/setter-nya
123456789private String name;public String getName() {return name;}public void setName(String name) {this.name = name;} - Tambahkan @Override method “execute”
- Tambahkan script dibawah ini pada method “execute”
1return SUCCESS; - Buka file struts.xml (src/main/resources), replace script didalam tag <struts> dengan script dibawah ini
1234567891011121314151617181920212223242526<struts><constant name="struts.enable.DynamicMethodInvocation" value="false"/><constant name="struts.devMode" value="true"/><package name="default" namespace="/" extends="struts-default"><default-action-ref name="index"/><action name="index"><result>WEB-INF/index.jsp</result></action><action name="actionA" class="com.wijaksana.belajar.struts2.action.ActionA"><param name="name">Chain from A to B</param><result name="success" type="chain"><param name="actionName">actionB</param></result></action><action name="actionB" class="com.wijaksana.belajar.struts2.action.ActionB"><result>WEB-INF/ActionB.jsp</result></action></package></struts> - Tambahkan file index.jsp (src/main/webapp/WEB-INF), replace semua script dengan script dibawah ini:
12345678910111213<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>Action Chaining</title></head><body><s:a action="actionA">Action A</s:a></body></html> - Tambahkan file ActionB.jsp (src/main/webapp/WEB-INF), replace semua script dengan script dibawah ini:
1234567891011121314<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>Action B</title></head><body><h2>Value from Action A: <s:property value="name"/></h2></body></html> - Selesai, bisa kita test sekarang
- Caranya masih ingatkan untuk menjalankan programnya? iya bener, Klik kanan project name >> Run As >> Maven Build… >> pada isian goals ketik “jetty:run” >> klik RUN
- Buka browser http://localhost:8080
Selamat mencoba,
salam berbagi,
wijaksana