วันอาทิตย์ที่ 16 พฤศจิกายน พ.ศ. 2557

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

จาก Java Programming Bookstore Appication (Series 5): Building a Web Application Using Struts 2 เราจะแก้ไข HelloWorld project ให้สามารถรับค่าและแสดงผลได้อย่างง่ายๆ กันก่อนครับ สิ่งที่เราต้องทำคือต้องสร้างคลาสสำหรับ action และมีหน้าจอในการกรอกชื่อ และส่วนของการแสดงผล เริ่มต้นที่หน้าจอกรอกข้อมูลกันตามตัวอย่างที่ 1 เป็นการแก้ไขไฟล์ index.jsp 


ตัวอย่างที่ 1 index.jsp
<html >
<body>
        <form action= "hello">
               <label for= "name">Enter your name </label> < br /> < input type ="text"
                      name= "name" /> < input type ="submit" value= "Submit" />
        </form>
</body>
</html>

จากตัวอย่างที่ 1
  • เมื่อเรากดปุ่ม Submit หน้าจอจะมีการเรียก action hello และจะถูกส่งไปยัง container
เมื่อมีการเพิ่ม action เราจำเป็นต้อง map URL Pattern เพิ่มที่ไฟล์ struts.xml ซึ่งจากโปรเจ็คเดิมเราจะ config ไว้แค่ action index ที่จะไปเรียก index.jsp เพื่อแสดงผล ให้แก้ไขไฟล์ 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"
               namespace="/" >
               <action name= "index">
                      <result> /index.jsp</result >
               </action>

               <action name= "hello" class="com.apress.helloworld.action.HelloWorldAction"
                      method= "execute">
                      <result name= "success">/hello.jsp </result>
               </action>
        </package>
</struts>


จากตัวอย่างที่ 2
  • action name ชื่อ hello จะถูก map ไปยังคลาส HelloWorldAction (ยังไม่สร้าง) และจะเรียกใช้เมธอด execute 
  • เมื่อรันสำเร็จจะถูกส่งไปยังหน้าจอที่ชื่อ hello.jsp
ต่อไปเราจำเป็นต้องสร้าง action คลาสขึ้นมาชื่อว่า HelloWorldAction ตามรูปที่ 1 เมื่อกดปุ่ม Finish จะได้ผลตามรูปที่ 2


รูปที่ 1


ให้เราแก้ไขไฟล์ HelloWorldAction ตามตัวอย่างที่ 3

รูปที่ 2


ตัวอย่างที่ 3 HelloWorldAction.java
package com.apress.helloworld.action;

public class HelloWorldAction {

        private String name ;

        public String execute() throws Exception {
               return "success" ;
       }

        public String getName() {
               return name ;
       }

        public void setName(String name) {
               this.name = name;
       }
}

จากตัวอย่างที่ 3
  • เมธอด execute() จะถูกเรียกโดย user action ที่ได้ config ไปตามตัวอย่างที่ 2 และ return "success" จะถูกส่งต่อไปยัง hello.jsp (ตัวอย่างที่ 4) ตาม config ไฟล์ที่ชื่อ struts.xml
ให้เราสร้างไฟล์ hello.jsp ที่ path helloWorldStruts2/src/main/webapp และแก้ไขตามตัวอย่างที่ 4

ตัวอย่างที่ 4 hello.jsp
<%@ page language= "java" contentType ="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1" %>

<%@ taglib prefix="s" uri= "/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Hello World</title >
</head>
<body>
       Hello your name is
        <s:property value= "name" />
</body>
</html>


จากตัวอย่างที่ 4
  • taglib คือ directive ที่บอกให้ servlet container รู้ว่า jsp นี้จะเรียกใช้งาน tags ของ Struts 2 
  • s:property tag จะเป็นตัวแสดงค่าที่ได้จากเมธอด getName จากคลาส HelloWorldAction
ทดสอบโดยการ start Tomcat server และเรียก http://localhost:8080/helloWorldStruts2/ จะปรากฏหน้าจอดังรูปที่ 3

รูปที่ 3


พิมพ์ชื่อ แล้วกดปุ่ม Submit จะได้ผลตามรูปที่ 4

รูปที่ 4


เราจะเริ่มสร้าง Bookstore Application ในตอนต่อไป

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





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


Java Programming Bookstore Application (Series 5): 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

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

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