SpringMVC入门程序
1.需求
查询商品信息
2.开发环境
java:1.7
eclise:MARS.1
tomcat:7.0.
springmvc:3.2
springmvc是spring一个组成部分,依赖spring的其它的包的,需要将spring的所有jar包放到工程。
工程结构
3.前端控制器开发
DispatcherServlet是一个serlvet,需要在web.xml中配置。
注意:
1.如果不配置contextConfigLocation,默认加载/WEB-INF/servlet名称+"-serlvet.xml"即:springmvc-serlvet.xml
2.配置url-pattern时注意事项
第一种:*.action或*.do等,请求以.action或.do结尾 的由前端控制器解析
第二种:/,所有的请求全部由前端控制器解析,会把所有的包括静态资源 (css、html、js)也解析,前端控制器无法解析静态资源,特殊处理静态资源访问不通过前端控制器解析
第三种:/*,此方法存在问题,请求需要最终转发到jsp,此方法前端控制器把转发的jsp也解析了,前端控制器无法解析jsp的,所以此方法不能使用。
4.配置处理器映射器
编写springmvc.xml
<?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: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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
</beans>
非注解的处理器适配器。
在springmvc.xml文件配置如下:
<beanclass="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
4.Handler开发
功能:查询商品信息,商品信息先通过静态数据表示。