Quick Start

有寫過Java Servlet的人一定知道,程式進入點是web.xml,Spring也不例外

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">

    <listener>
        <listener-class>my.config.MyContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

說明:

  • SpringMVC在收到request後,會交由同一個固定的servlet分派至不同controller,這個servlet稱為DispatcherServlet
  • DispatcherServlet通過contextConfigLocation這個參數,定義所有的controller。本例為url為/開頭的,全都由DispatcherServlet分派,分派的controller定義在mvc-config.xml
  • 注意! DispatherServleturl-pattern 若定義 /*,會無法進入.jsp!

mfc-config.xml

<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="my.controller" />

    <mvc:annotation-driven/>

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

    <!-- 本例使用@Annotation的方式定義controller路徑
    <bean name="/helloworld" class="my.controller.HelloworldController"> 
        <property name="viewPage"> 
            <value>hello</value> 
        </property> 
    </bean>
    -->
</beans>

說明:

  • <context:component-scan>相當於在java裡定義@ComponentScan({"your.java.package"}),告訴Spring要掃描的package
  • <mvc:annotation-driven/>使用@Annotation的方式定義controller
  • viewResolver定義view: 如果在controller回傳ModelAndView物件時,可以直接server render。

Controller

@Controller
public class HelloworldController {
    @RequestMapping(value = "/helloworld", method = RequestMethod.GET)
    @ResponseBody
    public String helloworld(HttpServletRequest request) {
        return "Hello_World";
    }
}

說明

  • class上方請宣告@Controller
  • method上方請宣告@RequestMapping,代表相應的url
  • 宣告@ResponseBody代表回傳的是一個response body(string, ...),如果沒定義,則會進到mvc的view流程。

Folder Tree

測試

瀏覽器上輸入:

http://localhost:8080/mySpringConfigedByXml/helloworld

參考

results matching ""

    No results matching ""