
Kita lanjutkan belajar struts2 dengan judul Tutorial Produce Consume JSON Apache Struts2. Kita akan coba mengulas bagaimana cara menggunakan JSON dalam melempar ataupun menerima pada apache struts2.
Sekarang kita mulai tutorialnya:
- Buka folder workspace anda, jangan lupa pelajari dulu Tutorial Basic Apache Struts2. Karena kita akan melanjutkan source codenya.
- Buat folder “json-struts2” di workspace anda
- Copy file “pom.xml” dan folder “src” di folder “basic-struts2” tutorial sebelumnya.
- Buka file “pom.xml”, rubah artifact-id, name, dan description
12345<artifactId>json-struts2</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>Belajar JSON Struts2</name><description>Belajar JSON Struts2</description> - Tambahkan <dependency> “struts2-json-plugin”, berikut fullĀ <dependencies>:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546<dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>${struts2.version}</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-json-plugin</artifactId><version>${struts2.version}</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>${log4j2.version}</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>${log4j2.version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-junit-plugin</artifactId><version>${struts2.version}</version><scope>test</scope></dependency></dependencies> - Buka IDE eclipse
- Pilih import >> existing maven project >> browse ke folder “json-struts2” >> finish
- Project baru json-struts2 sudah berhasil di load
Mari kita mulai melakukan perubahan selanjutnya
- Tambahkan class sebagai objek model dengan nama “MyBean” di package “com.wijaksana.belajar.struts2.model”, berikut fullscriptnya:
12345678910111213141516171819202122232425262728293031public class MyBean {private int counter;private List<String> names;public MyBean() {counter = 0;names = new ArrayList<String>();}public MyBean(int counter, List<String> names) {this.counter = counter;this.names = names;}public int getCounter() {return counter;}public List<String> getNames() {return names;}public void setCounter(int counter) {this.counter = counter;}public void setNames(List<String> names) {this.names = names;}}
Pages: 1 2