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/>


posted @ 2020-07-15 17:45  流沙uiui  阅读(103)  评论(0编辑  收藏  举报