本篇是关于SpringMVC配置的相关整理
1.web.xml:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-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>
2.SpringMVC配置文件:
<!-- spring 默认的就是BeanNameUrlHandlerMapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 通过bean的name,完成url映射 -->
<bean name="/welcome.html" class="cn.controller.WelcomeController"/>
为了方便我们一般用注解的方式替代以上方式
<!-- 使用annotation的方式完成映射 -->
<context:component-scan base-package="cn.controller"/>
<mvc:annotation-driven/>
注解的方式是基于org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
3.Controller类中:
在配置文件中被扫描的包下的类,在类上用@Controller进行注解即可实现