Monday, December 24, 2012

Ví dụ cơ bản về Spring Framework web


Tạo một package mới với tên tùy ý bên trong của thư mục src. Class Spring Controller extends đến org.springframework.web.servlet.mvc.AbstractController. Để tạo ra một Class Controller mới kích chuột phải vào thư mục src và tạo ra một lớp java mới, hãy nhập tên Controller class và tên super class và nhấn nút Finish.


Copy đoạn mã sau vào trong lớp HelloWorldController.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController {
       private String message; 

       @Override
       protected ModelAndView handleRequestInternal(HttpServletRequest request,       
              HttpServletResponse response) throws Exception {
              return new ModelAndView("welcomePage","welcomeMessage", message);
       }

        public void setMessage(String message) {
              this.message = message;
        }
}

HelloWorldController class có message property được thiết lập thông qua setter injection. HelloWorldController class sẽ ghi đè lên của phương pháp handleRequestInternal() để xử lý yêu cầu. Sau khi xử lý của yêu cầu handleRequestInternal() phương thức trả về một ModelAndView đối tượng trở về DispatcherServlet.


DispatcherSevlet, như tên cho thấy, là một servlet duy nhất để quản lý quá trình xử lý yêu cầu toàn bộ. Khi yêu cầu được gửi đến của DispatcherServlet nó đại diện công việc bằng cách gọi các bộ điều khiển thích hợp để xử lý yêu cầu. Giống như bất kỳ servlet khác DispatcherServlet cần phải được cấu hình trong mô tả triển khai web như được hiển thị.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
     <servlet-name>dispatcher</servlet-name>
     <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
     <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

Ở đây tên servlet là dispatcher. Theo mặc định các DispatcherServlet sẽ tìm một tên tập tin dispatcher-servlet.xml để nạp cấu hình của Spring MVC. Tên tập tin được tạo thành bởi việc ghép tên servlet ("dispatcher") với "-servlet.xml". Ở đây chúng ta sử dụng của của url-pattern là ". htm" inorder để ẩn công nghệ triển khai cho người sử dụng.

Những redirect.jsp sẽ được gọi đầu tiên khi chúng ta thực thi các ứng dụng web Spring. Đây là file jsp duy nhất bên ngoài thư mục WEB-INF và nó ở đây để cung cấp một chuyển hướng đến của DispatcherServlet. Tất cả của views khác cần được lưu giữ dưới thư mục WEB-INF để họ có thể được gọi chỉ thông qua quy trình controller.

Để tạo một tập tin cấu hình bean kích chuột phải vào thư mục WebContent và chọn New -> Other. Hộp thoại sau xuất hiện.


Chọn cấu hình Spring Bean tập tin và kích Next.


Nhập tên file là "dispatcher-servlet.xml" và nhấn vào nút Finish.

Bây giờ các tập tin cấu hình bean của Spring được tạo ra, chúng ta cần phải cấu hình Controller và các lớp ViewResolver. Những mã sau đây cho thấy làm thế nào để làm điều này.

<?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.xsd">

<bean id="viewResolver" class=" org.springframework.web.servlet.view. InternalResourceViewResolver" >
     <property name="prefix">
            <value>/WEB-INF/jsp/</value>
     </property>
     <property name="suffix">
            <value>.jsp</value>
      </property>
</bean>
<bean name="/welcome.htm" class="com.vaannila.HelloWorldController" >
        <property name="message" value="Hello World!" />
</bean>
</beans>

Đầu tiên chúng ta hãy hiểu làm thế nào để cấu hình controller.

<bean name="/welcome.htm" class="...HelloWorldController" >
       <property name="message" value="Hello World!" />
</bean>

Những ViewResolver được cấu hình bằng cách sử dụng đoạn mã sau.

<bean id="viewResolver" class=" org.springframework.web.servlet.view.InternalResourceViewResolver" >
      <property name="prefix">
             <value>/WEB-INF/jsp/</value>
      </property>
      <property name="suffix">
             <value>.jsp</value>
      </property>
</bean>

Các tập tin thư viện sau đây là cần thiết để chạy các ví dụ.

antlr-runtime-3.0
commons-logging-1.0.4
org.springframework.asm-3.0.0.M3
org.springframework.beans-3.0.0.M3
org.springframework.context-3.0.0.M3
org.springframework.context.support-3.0.0.M3
org.springframework.core-3.0.0.M3
org.springframework.expression-3.0.0.M3
org.springframework.web-3.0.0.M3
org.springframework.web.servlet-3.0.0.M3

Để thực hiện ví dụ như chạy các tập tin redirect.jsp. Những trang sau sẽ được hiển thị.


Chúc bạn thành công

No comments:

Post a Comment