1、建动态项目 next 生成web.xml
2、导入Mybatis jar包,放到lib目录下,及数据库连接文件和获取session文件
3、导入Spring jar包,放到lib目录下。
beans,context,aop,core,web,webmvc,expression + comms-logging
4、配置web.xml 引入前端控制器DispatcherServlet
<!-- Spring MVC配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
特别注意servlet-name,这个名字要用于创建springmvc的配置文件
5、 WEB-INF下创建配置文件,配置sprint-servlet.xml (spring这个名字要求和web.xml中配置servlet-name的名字一样)
格式和spring的基础配置文件一样
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>
spring中配置的是applicationContext.xml
4)springmvc可以采用xml配置或者注解配置,xml配置很少用,也比较麻烦,我们只讲解注解配置。
a)引入相关的命名空间mvc和context
xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
b)设置注解驱动
<mvc:annotation-driven/>
c)设置controller扫描路径的包名,spring mvc会自动的实例化该包下的controller。
controller相当于struts中的action,用于处理数据,完成转发。
<context:component-scan base-package="controller"/>
跳转方式一
跳转方式二:
整体界面分析