web.xml部分 1.歡迎界面 2.字元編碼過濾器 3.springmvc配置 springmvc-serlvet.xml部分 1.InternalResourceViewResolver 視圖解析器 2.註解包掃描器 3.基礎配置和引入靜態資源配置 4.全局異常處理 5.文件上傳 ...
web.xml部分
1.歡迎界面
<welcome-file-list>
<welcome-file>/views/login.jsp</welcome-file>
</welcome-file-list>
2.字元編碼過濾器
<filter>
<filter-name>CharacterEncoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.springmvc配置
<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-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4.配置文件路徑設置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
springmvc-serlvet.xml部分
1.InternalResourceViewResolver 視圖解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
2.註解包掃描器
<context:component-scan base-package="cn.xin.controller" />
3.基礎配置和引入靜態資源配置
<mvc:annotation-driven />
<mvc:default-servlet-handler />
4.全局異常處理
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.RuntimeException">login</prop>
</props>
</property>
</bean>
5.攔截器配置
6.文件上傳
<!-- 配置MultipartResolver,用於上傳文件,使用spring的CommonsMultipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5000000"/>
<property name="defaultEncoding" value="utf-8"/>
</bean>
知識點部分