วันศุกร์ที่ 14 พฤศจิกายน พ.ศ. 2557

Java Programming Bookstore Appication (Series 5): Building a Web Application Using Struts 2





ใน series ต่อไปนี้เราจะลองใช้ struts 2 กันเบื้องต้นโดยเขียน HelloWorld กันจะมีการใช้ tool ที่เรียกว่า Maven ด้วยครับ Maven เป็น command-line tool ใช้ในการ build และ package project รวมถึงช่วยเรื่อง manage dependencies ช่วยให้เราพัฒนา project ต่างๆ ได้ง่ายขึ้น ซึ่งเราจะใช้ไฟล์ชื่อ pom.xml ในการ config ค่าต่างๆ ในบทความนี้เราจะใช้ Maven 4 ซึ่งบรรจุอยู่ใน Eclipse-kepler อยู่แล้ว (การพัฒนาโปรแกรม Java ด้วย Eclipse IDE for Windows 64-bit)

เริ่มต้นสร้าง Maven Project ตามรูปที่ 1 ให้เปิด eclipse เลือกเมนู File > New > Other...
จะปรากฏหน้าจอตามรูปที่ 2

รูปที่ 1


จการูปที่ 2 ให้เลือกโฟลเดอร์ Maven > Maven Project แล้วกดปุ่ม Next >  

รูปที่ 2


จะได้ผลตามรูปที่ 3 จากนั้นกดปุ่ม Next > 

รูปที่ 3



ให้เลือกตามรูปที่ 4 เป็นการเลือก template ของการสร้างเว็บแบบง่ายๆ แล้วกดปุ่ม Next > 

รูปที่ 4


ให้กรอกข้อมูลตามรูปที่ 5 แล้วกดปุ่ม Finish

รูปที่ 5


เราจะได้โครงสร้าง project ตามรูปที่ 6 

รูปที่ 6


จากนั้นเราจำเป็นต้องเพิ่ม struts 2-core dependency ในไฟล์ config ที่ชื่อว่า pom.xml ตามตัวอย่างที่ 1 (ส่วนที่เป็นสีส้มคือ code ที่เพิ่ม)

ตัวอย่างที่ 1 pom.xml
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion> 4.0.0</ modelVersion>
        <groupId> com.apress</groupId >
        <artifactId> helloWorldStruts2</artifactId >
        <packaging> war</ packaging>
        <version> 0.0.1-SNAPSHOT</version >
        <name> helloWorldStruts2 Maven Webapp</name >
        <url> http://maven.apache.org</url >
        <dependencies>
               <dependency>
                      <groupId> junit</groupId >
                      <artifactId> junit</artifactId >
                      <version> 3.8.1</ version>
                      <scope> test</ scope>
               </dependency>
               <dependency>
                      <groupId> org.apache.struts</groupId >
                      <artifactId> struts2-core</artifactId >
                      <version> 2.3.16.3</ version>
                      <type> jar</ type>
                      <scope> compile</ scope>
               </dependency>
        </dependencies>
        <build>
               <finalName> helloWorldStruts2</finalName >
        </build>
</project>


จากนั้นให้เราทำการแก้ไข web.xml ซึ่งก็คือ deployment descriptor โดยเราจำเป็นต้อง config servlet filter ขึ้นตามตัวอย่างที่ 2

ตัวอย่างที่ 2 web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
        <display-name> Hello World Struts 2 Web Application</display-name >
        <filter>
               <filter-name> struts2</ filter-name>
               <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class>
        </filter>

        <filter-mapping>
               <filter-name> struts2</ filter-name>
               <url-pattern> /*</ url-pattern>
        </filter-mapping>
</web-app>

จากตัวอย่างที่ 2
  • StrutsPrepareAndExecuteFilter จะทำหน้าที่เป็น servlet filter ตาม URL pattern ที่เราได้ map ไว้
ต่อไปให้สร้างไฟล์ struts.xml (สร้างโฟลเดอร์ classes ใต้โฟลเดอร์ WEB-INF แล้วสร้างไฟล์ struts.xml ใต้ classes อีกที)  ตามรูปที่ 7

รูปที่ 7


รูปที่ 8 


จากรูปที่ 8 ให้เลือกโฟลเดอร์ XML > XML File แล้วกดปุ่ม Next >
ต่อไปให้ตั้งชื่อไฟล์ว่า struts.xml แล้วกดปุ่ม Finish

รูปที่ 9


ให้แก้ไขไฟล์ struts.xml ตามตัวอย่างที่ 2


ตัวอย่างที่ 2 struts.xml
<?xml version= "1.0" encoding ="UTF-8"?>
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
        <constant name= "struts.devMode" value ="true" />
        <package name= "basicstruts2" extends ="struts-default">
               <action name= "index">
                      <result> /index.jsp</result >
               </action>
        </package>
</struts>

จากตัวอย่างที่ 2
  • action name index จะทำการ render หน้า index.jsp (ตามรูปที่ 6 ถูกสร้างตั้งแต่ตอนสร้างโปรเจ็ค) เมื่อมีการเรียก action นี้

ให้เรา deploy web application ไปยัง Tomcat server ที่เรา set ไว้ตั้งแต่ series 4 (Java Programming Bookstore Application (Series 4): Building Web Applications Using Servlets and JSP

ก่อน run project ให้เรา คลิกเมาส์ขวาที่ไฟล์ pom.xml และเลือก Run As > Maven install ให้สังเกตที่ console tab ตามรูปที่ 11

รูปที่ 10


รอจน eclipse download dependency จนสำเร็จตามรูปที่ 11

รูปที่ 11


จากนั้นลอง start Tomcat Server แล้วทดสอบกัน http://localhost:8080/helloWorldStruts2/
ได้ผลตามรูปที่ 12

รูปที่ 12


เมื่อได้ผลตามรูปที่ 12 เราก็แน่ใจได้ว่าเรา config ถูกต้องแล้ว ต่อไปเราจะให้ HelloWorld Project ของเราแสดงข้อความยินดีต้อนรับผู้ใช้งานกัน

Java Programming Bookstore Appication (Series 5.1): Building a Web Application Using Struts 2

ตอนต่อไปติดตามได้ที่


Java Programming Bookstore Appication (Series 5.1): Building a Web Application Using Struts 2

Java Programming Bookstore Application (Series 6): Building Java Web Applications with Spring Web MVC

Java Programming Bookstore Application (Series 7): Component-Based Web Development Using JSF 2

Java Programming Bookstore Application (Series 8): Rapid Web Development with Grails

Java Programming Bookstore Application (Series 9): Play with Java and Scala



อ้างอิง

ผู้เขียน: procodeblog

ไม่มีความคิดเห็น:

แสดงความคิดเห็น