SpringMVC ภาค3

February 20th, 2009 by roofimon Leave a reply »

หายหัวไปนานครับเพราะงานเข้าว่างแล้ว พร้อมกับไอเดียในสมองอีกเพียบแต่อย่างไรก็ตามเรามาว่าให้จบก่อน ด้วยเรื่องของการ Post ค่ากลับจากฟอร์มเพื่อเก็บค่าเหล่านั้นเข้าไปในฐานข้อมูล ข้อดีของ web Framework สมัยใหม่คือมันสามารถ bind ค่าจากฟอร์มไปสู่ Domain Class ของเราได้เลยโดยสิ่งที่เราต้องทำก็เพียงแค่ระบุชื่อ Attribute ให้ตรงตามที่เราต้องการลงไปในฟอร์ม เพียงเท่านี้เราก็ไม่ต้องมานั่ง request.getParemeter กันอีกต่อไปดังนั้นเพื่อไม่ให้เสียเวลาเราจะมาสร้างฟอร์มกันที่ WEB-INF/pages/ โดยให้ชื่อ usrForm.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<html>
    <head>
        <title>Post Form</title>
    </head>
    <body>
        <form:form method="POST" commandName="usr">
            <form:errors path="*" cssClass="error" />
            <table>
                <tr>
                    <td>User ID</td>
                    <td><form:input path="usrId" /></td>
                    <td><form:errors path="usrId" cssClass="error" /></td>
                </tr>
                <tr>
                    <td>UserName</td>
                    <td><form:input path="usrName" /></td>
                    <td><form:errors path="usrName" cssClass="error" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><form:input path="usrPwd" /></td>
                    <td><form:errors path="usrPwd" cssClass="error" /></td>
                </tr>
                <tr>
                    <td colspan="3"><input type="submit" /></td>
                </tr>
            </table>
        </form:form>
    </body>
</html>

ส่วนที่น่าสนใจคือ form:form method=”POST” commandName=”usr” บริเวณนี้จะทำการระบุว่าเราจะใช้ Domain Class ชื่อ usr อ่าวแล้วมันมาจากไหนอันนี้ต้องสงสัยครับเพราะไม่งั้นซวย โดยที่ค่า commandName นี้เราสามารถกำหนดได้ที่ Controller Config ครับเดี๋ยวทำให้ดู อีกส่วนคือ form:input path=”usrName” อันนี้เอาไว้ระบุ attribute ที่จะถูกใช้เพื่อ bind ครับ ต่อไปเรามาสร้าง Controller กัน

/**
 *
 * @author twinp
 */
public class UsrFormController extends SimpleFormController {

    private AppUserService appUserService;

    public UsrFormController() {
        setCommandClass(AppUser.class);
        setCommandName("usr");
    }

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    }

    @Override
    protected void doSubmitAction(Object command) throws Exception {
        AppUser usr = (AppUser) command;
        appUserService.createUser(usr);
    }

    /**
     * @return the usrService
     */
    public AppUserService getAppUserService() {
        return appUserService;
    }

    /**
     * @param usrService the usrService to set
     */
    public void setAppUserService(AppUserService appUserService) {
        this.appUserService = appUserService;
    }
}

จะเห็นว่า Controller เราดูโง่มากไม่มีอะไรเลยซึ่งจริงๆมันก็ทำอยู่แค่นี้แหละครับแต่ถ้าทำ Validate จะเลอะกว่านี้เอาแบบนี้ไปก่อนนะครับ doSubmitAction(Object command) จะทำการรับ command object ที่ถูกใส่ค่ามาให้เรียบร้อยแล้วและส่งต่อไปให้ service เท่าีนี้แหละครับและเพื่อให้ Controller ตัวนี้ทำงานได้ แน่นอนเราต้องไปเพิ่ม configuration ไว้ที่ spring66-servlet.xml ครับ อ่ออีกนิดนึงครับที่ Constructor ของ Controller เราจะกำหนดตัว command name, command class ให้กับฟอร์มของเรานะครับ

    <bean id="usrFormController" class="com.spring66.tutorial.web.controller.UsrFormController">
        <property name="appUserService" ref="appUserService"/>
        <property name="formView" value="usrForm" />
        <property name="successView" value="usrList" />
    </bean>

ขั้นตอนต่อไปคือเราต้องไปบอก URL Mapper ด้วยว่าเมื่อไหร่ที่เราจะให้ Controller ตัวนี้ทำงานโดยเหมือนเดิมครับไปทำที่ spring66-servlet.xml

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                usrList.html=usrListController
                usrForm.html=usrFormController
            </value>
        </property>
    </bean>

อีกนิดนึงครับเนื่องจากเรากรอกค่าจากฟอร์มเพียงสามค่าดังนั้นค่าที่เหลือจะต้องถูกกำหนดที่ใดที่หนึ่ง ซึ่งค่าเหล่านั้นเป็นเรื่องของวันที่ผมจึงเอาไปใส่ไว้ใน Service ก่อนที่จะส่งต่อไปให้ DAO เป็นคน Persist ค่าลง database ดังนี้

    public AppUser createUser(AppUser user) {
        Date currentDate = new Date();
        user.setLogDate(currentDate);
        user.setRegDate(currentDate);
        user.setUsrFirstLogin(currentDate);
        appUserDao.insert(user);
        return user;
    }

เรียบร้อยแล้วครับต่อไปเราก็ทำการสั่ง “mvn jetty:run” และเข้าไปที่ URL: http://localhost:8080/{application}/usrForm.html

Advertisement

1 comment

  1. พี่ @roofimon ครับผมสงสัยตรง Object command นะครับว่า ถ้าเรามี input
    ที่รับค่า ที่ไม่ได้อยู่ใน command ที่เรากำหนดไป เราจะยังใช้ request.getParemeter
    ดึงค่าออกมาได้เหมือนเดิมหรือเปล่าครับ

Leave a Reply

You must be logged in to post a comment.