1. 程式人生 > >JAVA之初識springMVC框架

JAVA之初識springMVC框架

在這個配置中,我們規定了 DispatcherServlet 的關聯 XML 檔名稱叫做 dispatcher-servlet,但是這裡的路徑是相對於web.xml來說的,也就是說,這個檔案也在WEB-INF的根目錄下。

所以,我們需要在WEB-INF的根目錄下新建一個dispatcher-servlet.xml檔案,新增以下程式碼:

(注:將程式碼中的對應工程名替換為自己的)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/util 
       http://www.springframework.org/schema/util/spring-util-3.0.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
      ">
         <!-- 開啟註解模式驅動 -->    
         <mvc:annotation-driven></mvc:annotation-driven>
         <!-- 掃包 -->
         <context:component-scan base-package="com.SpringTest.*"></context:component-scan>
         <!-- 靜態資源過濾 -->
         <mvc:resources location="/resources/" mapping="/resources/**"/>


         <!-- 檢視渲染 jsp/freemaker/velocity-->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
               <!-- 制定頁面存放的路徑 -->
               <property name="prefix" value="/WEB-INF/pages/"></property>
               <!-- 檔案的字尾 -->
               <property name="suffix" value=".jsp"></property>
         </bean> 
</beans>
按照配置,需要注意的是,但凡是遇到有註解的,比如@Controller , @Service , @Autowired ,就會將它們加入到Spring的bean工廠裡面去。而所有的靜態資原始檔,比如說 js , css , images 都需要放在/resources目錄下,這個目錄現在我們還沒有建。所有的展示頁面,比如jsp檔案,都需要放置在/WEB-INF/pages目錄下。

5、新建包

在Java Resources/src 下新建controller.java


6、新建資料夾

建立resources、pages資料夾,結果如下: