经过学习,感觉还是比Strut2好用多了,少去了Action标签的配置,我只需要使用注解即可定位到某个页面
工作原理:
用户通过一个访问路径进行请求,服务器端通过一个前端控制器(org.springframework.web.servlet.DispatcherServlet)进行路径分发,这时候发给对应的Controller,controller调用相应的业务处理完后,返回一个ModelAndView(比如jsp,html或者服务器内部的servlet即重定向),前端控制器渲染这个视图(view),返回给用户
配置:
1.
1.1 首先配置前端控制器件DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
1.2设置web容器启动,该servlet初始化,在<servlet></servlet>中加入
<load-on-startup>1</load-on-startup>
2.对应DispathcerServlet有一个xml配置文件为xx-servlet.xml, xx是<servlet-name></servlet-name>配置的servlet名字,默认放在WEB-INF下
<?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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
3.写Controller控制器类(类名自定义)
注意:使用注解的方式得到了很大的方便性,如果不使用注解就要实现Controller接口或者继承