springMVC_注解方式搭建基础环境

---恢复内容开始---

一、jar包环境,web配置文件和Spring-MVC配置文件的,相关的modelAndview

1.配置DispatcherServlet

<servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

2.配置bean.xml文件的监听器

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

3.配置Spring-MVC.xml文件

3.1配置扫描控制器Cotroller包路径和注解驱动

    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-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/context
            http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

    <!-- 自动扫描 -->
    <context:component-scan base-package="springmvcpracticewebcontroller">
    </context:component-scan>
    
    <mvc:resources location="/resources/" mapping="/images/**"/>
    
    <!-- 注解驱动 -->
    <mvc:annotation-driven>
    </mvc:annotation-driven>

3.2配置文件上传解析器

<!-- 文件上传解析器 id 必须为multipartResolver -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760">
        </property>    
    </bean>

3.3配置内部资源解析器和静态视图资源路径

<!-- 映射静态资源 -->
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/upload/" mapping="/upload/**"/>
    
    
    <!-- 内部资源视图解析器  prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsps/"/>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>

3.4配置beans.xml文件的Service包路径的自动扫描

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-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/context
            http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

    <!-- 自动扫描 -->
    <context:component-scan base-package="springmvcpracticeservice"/>
    
</beans>

二、配置编写,person bean,personService和contorller的实体类

三、

---恢复内容结束---

原文地址:https://www.cnblogs.com/jinb/p/6551555.html