Spring MVC框架学习(一)
首先要学习Spring MVC我们需要先了解什么是Spring MVC
Spring MVC是Spring框架的一个子框架,它是基于MVC的web框架。(MVC代表的是Model-View-Controller 也就是模型 - 视图 - 控制器)。
简绍一下SpringMVC架构图
来自于http://jinnianshilongnian.iteye.com/blog/1594806
Spring MVC的简单搭建,我运用的是myeclipse10+tomcat7+jdk1.7环境
搭建完成后的整体图
第一步:创建maven工程
点击project进入
选择Maven Project
如图选择点击Next
如图填写
Group Id:对应JAVA的包的结构,是main目录里java的目录结构。
一般填写com.leafive.test。
Artifact Id:对应项目的名称,就是项目根目录的名称。
得到
再创建相应的目录
新建的java文件包是存放.java文件的。
现在不需要添加直接点击完成。
这便是我们创建的,点击ok。
导入Spring核心包
这里的是自动生成,表示applicationContext.xml生成的路径。
1.创建所要用的java文件和jsp文件。
2.首先配置web.xml文件
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>applicationContext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>applicationContext</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Other XML Configuration -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Spring ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.配置applicationContext.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
default-lazy-init="true">
<!-- 配置注解要搜索的范围 -->
<context:component-scan base-package="com.student"></context:component-scan>
<context:annotation-config />
<!-- 视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
运行程序
新建一个要运行的文件。
选择运行所用的tomcat。
“`
填写运行项目的名称和运行所用的maven。
点击Run运行。
这里不报错就可以输入网址了。