SpringMVC ภาค 2

February 6th, 2009 by roofimon Leave a reply »

ภาคที่สองของ SpringMVC
ต่อไปเราจะทำการเปลี่ยน configuration ใน spring66-servlet.xml เพื่อที่จะโยนภาระเรื่องของการ map view ไปให้คนอื่นและทำการเพิ่ม service เข้าไปใน controller ด้วย
อันดับแรกเราจะทำการเปลี่ยน spring66-servlet.xml ก่อนนะครับโดยเราจะทำการเปลี่ยนแปลงเพื่อให้เราสามารถตั้งชื่อ bean ได้แบบปกติทั่วไป

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                usrList.lmth=usrListController
            </value>
        </property>
    </bean>
    <bean name="usrListController" class="com.spring66.tutorial.web.controller.UsrListController">
    </bean>
</beans>

จะเห็นได้ว่าเราได้เรียกใช้บริการจาก SimpleUrlHandlerMapping เข้ามาเพื่อให้เราสามารถระบุ url กับ controller(bean) ตัวที่รับผิดชอบไว้ได้และมันก็ง่ายสำหรับเราด้วยในการไล่หาผู้รับผิดชอบ
ของ request และอีกบริการที่เราเพิ่มเข้ามาคือ UrlBasedViewResolver ครับเจ้าตัวนี้มีหน้าที่เดียวครับคือคอยแกะว่า view name ที่เราระบไว้หรือส่งกลับออกมาจาก controller ชื่ออะไร จากนั้นมันจะทำการ
คนหาไฟล์ jsp ในไดเรคทอรี่ที่เรากำหนดไว้ใน “prefix” เพื่อทำการส่งผลที่ได้จาก controller ไปแสดงที่หน้า view หน้านั้นๆดังนั้นหน้าตาของ controller จะเปลี่ยนไปครับแต่ไม่มากครับ

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
        String now = (new Date()).toString();
        return new ModelAndView("usrList", "now", now);
    }

จะเห็นได้ว่าเราไม่ต้อง hardcode ประเภทของ view ลงไปใน controller ของเราซึ่งจะส่งผลให้เราสามารถเปลียน view ได้สะดวกมากขึ้นเช่นย้ายไป Velocity หรือ Freemarker ได้ง่ายขึ้นโดยการเปลี่ยน configuration
เพียงที่เดียวครับ
ส่วนต่อไปคือเราจะทำการเพิ่ม service เข้าไปใน controller ของเราซึ่งตามภาพพื้นฐานแล้วสิ่งที่เราต้องทำนั้นก็คือการสั่งให้เวบแอพพลิเคชั่นของเราทำการสร้างและ integrate ตัว application context ของเราเข้าไปด้วยกัน
ในจังหวะที่ start up เพราะฉะนั้นแล้วสิ่งที่เราต้องทำถัดมาคือการระบุที่อยู่ของไฟล์ applicationContext.xml ของเราเข้าไป พร้อมกับเพิ่ม listener เข้าไปอีกหนึ่งตัวดังนี้

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

หลังจากเราบอกให้ เวบของเราสร้าง applicationcontext แล้วสิ่งต่อไปคือการเพิ่ม service เข้าไปที่ controller

    private UserService usersService;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
        UsersExpression userEx = new UsersExpression();
        userEx.createCriteria().andUsrNameIsNotNull();
        List<Users> list = usersService.getUsers(userEx);
        return new ModelAndView("usrList", "usrList", list);
    }
    /**
     * @return the usersService
     */
    public UserService getUsersService() {
        return usersService;
    }

    /**
     * @param usersService the usersService to set
     */
    public void setUsersService(UserService usersService) {
        this.usersService = usersService;
    }

ถ้าเราดู code ใน controller อาจจะงงเล็กน้อยครับ เพราะใน SpringMVC จะมีการส่งค่าคืนออกมาสองประเภทคือ
1. ชื่อ view ต่อไปที่จะรับผิดชอบเอาข้อมูลไปแสดง
2. คือชื่อข้อมูลหรือที่เราเรียกว่า model ที่จะนำไปแสดงครับ
จากนั้น dependency injection จะไม่ทำงานครับถ้าเราไม่บอก applicationContext ว่า bean controller ของเราต้องการใช้ service ตัวนี้จะ ซึ่งเราก็สามารถทำได้ด้วยการแก้ไข spring66-servlet.xml

    <bean name="usrListController" class="com.spring66.tutorial.web.controller.UsrListController">
        <property name="usersService">
            <ref bean="usersService"/>
        </property>
    </bean>

เท่านี้เราก็จะได้ controller ที่มี service ผูกมาให้เราเรียบร้อยแล้วครับจากนั้นเราจะนำข้อมูลที่ได้ไปแสดงละครับที่หน้า jsp ของเราดังนั้นสิ่งที่ต้องทำคือการแก้ไข หน้านั้นให้เรียบร้อย

<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
    <body>
        <h2>Hello World!vvvv</h2>
        <c:forEach var="usr" items="${usrList}">
            <c:out value="${usr.usrId}"/>
        </c:forEach>
    </body>
</html>

เพียงเท่านี้เราจะได้ หน้าแสดงข้อมูลทั้งหมดออกมาแล้ว ครับ

Advertisement

Leave a Reply

You must be logged in to post a comment.