1. 程式人生 > 實用技巧 >SpringMVC的簡介與使用

SpringMVC的簡介與使用

概述

Spring 為展現層提供的基於 MVC 設計理念的優秀的 Web 框架,是目前最主流的MVC 框架之一 。一種輕量級的、基於MVC的Web層應用框架。

簡單使用

新建Web工程,加入 jar 包

spring-aop-4.0.0.RELEASE.jar

spring-beans-4.0.0.RELEASE.jar

spring-context-4.0.0.RELEASE.jar

spring-core-4.0.0.RELEASE.jar

spring-expression-4.0.0.RELEASE.jar

commons-logging-1.1.3.jar

spring-web-4.0.0.RELEASE.jar

spring-webmvc-4.0.0.RELEASE.jar

在 web.xml 中配置 DispatcherServlet

沒有設定spriing.xml的位置

<servlet>

          <servlet-name>springMVC</servlet-name>

          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

     </servlet>



     <servlet-mapping>

          <servlet-name>springMVC</servlet-name>

          <url-pattern>/</url-pattern>

     </servlet-mapping>


設定spriing.xml的位置

<servlet>

          <servlet-name>springMVC</servlet-name>

          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

          

          <!-- 設定springMVC配置檔案的位置以及名稱 -->

          <init-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>classpath:springMVC.xml</param-value>

          </init-param>

     </servlet>



     <servlet-mapping>

          <servlet-name>springMVC</servlet-name>

          <url-pattern>/</url-pattern>

     </servlet-mapping>


加入 Spring MVC 的配置檔案:springmvc.xml

沒有設定的話,一般位於WEB-INF下;設定的話一般位於src/conf下

<!-- 掃描元件,將加上@controller註解的類作為springMVC的控制層 -->

     <context:component-scan 
base-package="com.atguigu.test"></context:component-scan>



     <!-- 配置檢視解析器 -->

     <bean id="viewResolver" 
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

          <property name="<u>prefix"</u> 
value="/WEB-INF/view/"></property>

          <property name="<u>suffix"</u> value=".jsp"></property>

     </bean>


簡單測試

index.jsp檔案

<a <u>href</u>="test">測試</a>

test檔案

@Controller

public class TestController {

       @RequestMapping(value = 
"/test",method=RequestMethod.GET) 

       public String hello() {

            System.out.println("SUCCESS"); 

            return "success"; 

       }

success檔案

<body>
     <h1>成功</h1>

     <br/>

     ${requestScope.username}

</body>

</html>

測試過程

其他配置

web.xml中

保證Method方法可用的過濾器

<filter>

          <filter-name>HiddenHttpMethodFilter</filter-name>

          <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

     </filter>

     

     <filter-mapping>

          <filter-name>HiddenHttpMethodFilter</filter-name>

          <url-pattern>/*</url-pattern>

     </filter-mapping>


保證編碼正確的過濾器,要放在前面

<filter>

          <filter-name>CharacterEncodingFilter</filter-name>

          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

          <init-param>

              <param-name>encoding</param-name>

              <param-value>UTF-8</param-value>

          </init-param>

     </filter>

     <filter-mapping>

          <filter-name>CharacterEncodingFilter</filter-name>

          <url-pattern>/*</url-pattern>

     </filter-mapping>


改變springMVC配置檔案位置

<servlet>

          <servlet-name>springMVC</servlet-name>

          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

          

          <!-- 設定springMVC配置檔案的位置以及名稱 -->

          <init-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>classpath:springMVC.xml</param-value>

          </init-param>

     </servlet>


springMVC.xml中

註解驅動

<mvc:annotation-driven />


載入靜態檔案

<mvc:default-servlet-handler/>