SpringMVC通过使用MVC注解的方式将POJO类成为请求控制器不需要实现接口,支持REST风格的URL请求,采用松散耦合可插拔组件结构比其他MVC框架更具扩展性和灵活性
SpringMVC在web.xml文件中的配置一般格式:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMVCStudy</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--配置DispatcherServlet -->
<servlet>
<servlet-name>springDispatcherServlet</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在web应用被加载的时候创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
SpringMVC的配置文件一般格式:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="指定注解自动扫描的全包名"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">(配置视图解析器,控制器返回值对应的实际物理视图界面,通过前缀prefix值+控制器返回return值+后缀suffix值获得实际url地址,然后做转发操作)
<property name="prefix" value="/WEB-INF/views/"></property>(拼接url前缀)
<property name="suffix" value=".jsp"></property>(拼接url后缀)
</bean>
</beans>
SpringMVC在类前加注解@Controller指定类为控制器,在控制器类的方法前加@RequestMapping("url地址")指定请求映射的方法