组件说明:
DispatcherServlet:前端控制器
HandlerMapping:处理器映射器
Handler:处理器
HandlAdapter:处理器适配器
ViewResolver:视图解析器
入门程序:
1.创建工程导入jar包
在web.xml里面配置DispatcherServlet:
<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.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>
2.在src下创建springmvc.xml
<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
<!--视图映射器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
/
<context:component-scan base-package="com.baidu.controller"></context:component-scan>
<!--<bean name="/item.action" id="item"-->
<!--class="com.baidu.controller.ItemList"/>-->
</beans>
<mvc:annotation-driven></mvc:annotation-driven>
完全可以代替代老式代码{
<!-- 处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
}
3.在src下新建JAVA文件继承Controller接口实现它的方法
4.注解方式快速入门springmvc
4.1在类名上面配置注解@Controller
4.2在方法上面配置@RequestMapping("/XXX.action") ,可以在web.xml中的DispatcherServlet中更换拦截后缀,这个便是后续浏览器访问路径的名称
5.写入方法
System.out.println("请求来了");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("yd","yd");
modelAndView.setViewName("aa");
return modelAndView;
modelAndView.addObject("yd","yd");
=相当于代码=
request.setAttribute("yd","yd");
6.创建WEB-INF/jsp/aa.jsp
在aa.jsp通过${yd}取出作用域的值
7.浏览器访问路径
localhost:8080/XXX.action