第一章 SpringMVC环境搭建

x33g5p2x  于2021-03-14 发布在 其他  
字(3.5k)|赞(0)|评价(0)|浏览(300)

一 、SpringMVC概述:

Spring MVC工作在控制层, 替代的是servlet

Servlet不好的地方:
1). 一个请求对应一个servlet, servlet类的数量特别多。
2). 处理请求参数麻烦
request.getParameter()
request.getParameter()

User u = new User();
u.setxx();
u.setxxx();
MyService.addUser(u);

3). 响应

  1. 请求重定向
  2. 请求转发
  3. 返回的ajax
    处理对象向json字符串的转换
    处理json类型的response流程

4). 文件上传

5). 其他。。。

二、 Spring MVC环境搭建(注解方式)

1). 添加jar包

2). 编写springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	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: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.2.xsd  
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context-3.2.xsd  
            http://www.springframework.org/schema/mvc  
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
            http://www.springframework.org/schema/util  
            http://www.springframework.org/schema/util/spring-util-3.2.xsd">
            
    <!--配置注解形式的处理器映射器和处理器适配器-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--配置包扫描-->
    <context:component-scan base-package="com.neuedu.controller"></context:component-scan>

    <!--配置视图解析器,不配置,使用默认的-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
       <!-- <property name="suffix" value="jsp"></property>-->
    </bean>

3). 编写web.xml

<!-- 配置spring mvc的前端控制器 -->
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <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>

4). 编写控制器

@Controller
@RequestMapping("/test")
public class ActiveController {

@RequestMapping("/test")
public ModelAndView getActive()
{
}

}

5). 在springmvc.xml中配置静态资源

<!-- 配置静态资源 -->
  <!--  <mvc:resources location="/js/" mapping="/js/**"/>
   <mvc:resources location="/img/" mapping="/img/**"/>
   <mvc:resources location="/css/" mapping="/css/**"/>
   <mvc:resources location="/html/" mapping="/html/**"/>
   <mvc:resources location="/upload/" mapping="/upload/**"/>
   <mvc:resources location="/fonts/" mapping="/fonts/**"/> -->
   
   <!-- 如果路径映射不到controller上,采用服务器默认查找方式 -->
   <mvc:default-servlet-handler/>

6 ). 将资源放在WEB-INF下,修改相应的配置

<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
    <mvc:resources location="/WEB-INF/img/" mapping="/img/**"/>
    <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
    <mvc:resources location="/WEB-INF/html/" mapping="/html/**"/>
    <mvc:resources location="/WEB-INF/upload/" mapping="/upload/**"/>
    <mvc:resources location="/WEB-INF/fonts/" mapping="/fonts/**"/>

相关文章