spring入门(五)【springMVC环境搭建】
springMVC作为spring的一个WEB组件,是一个MVC的思想,减少了WEB开发的难度,现介绍springMVC环境的搭建,具体的原理放在后面介绍。用过框架的朋友都知道要在WEB项目中使用一个框架,必须要引入这个框架,和其他框架的引用方式一样,springMVC的引入方式是通过DispatcherServlet,那么我们就要在web.xml中配置此servlet,在lib目录下我已经把用到的jar包全部导入,下面看我的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" 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">
<display-name>springmvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!--log4j的配置文件-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param>
<!--spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param> <!--配置springmvc-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--springmvc的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/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> <!--配置spring-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--配置系统中的日志-->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
</web-app>
springMVC
在配置文件中我们配置了DispatherServlet,并且配置了它的初始化参数contextConfigLocation,指定了文件所在的路径为:/WEB-INF/classes下;配置了拦截的URL,我们这里拦截所有的url
spring
你可能会疑惑,这里我们为什么又配置了spring,难道只有springmvc不可用吗?答案是可以的,我们完全可以把所有的配置都放在springmvc的配置文件中,但是我们这里遵循层次化的配置,让springMVC是一个配置文件,spring是一个配置文件,把他们所要完成的功能分开,其实所有的配置完全可以由dispatherServlet进行加载。
spring的加载方式这里使用了一个监听器,ContextLoaderListener,他会读取context中的配置参数:contextConfigLocation,读取spring的配置文件,加载spring容器。
log4j
由于在项目中要使用日志功能,所以这里配置了日志,且引用context中的配置文件。
下面看springmvc的配置文件,
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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"
>
<!--配置了组件扫描,就不需要配置<context:annotation-config> ,因为组件扫描已经包含了,
组件扫描,扫描的注解有@Controller,@Service,@Resposity,@Component,
@Controller注解必须由dispatherDispatcherServlet来扫描,
也实现了依赖注入,对
@Autowired,@Resource的支持
-->
<context:component-scan base-package="com.cn.my">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> <!--开启基于注解的驱动-->
<context:annotation-config></context:annotation-config>
<!--开启mvc的注解驱动 ,可以使用@RequestParam注解,可以将请求参数帮到到控制器参数上-->
<mvc:annotation-driven/> <mvc:resources location="/resource/*" mapping="/resource/**"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property> </bean>
</beans>
上面是springmvc的配置文件,首先配置了组件扫描,组件扫描的作用是会自动类级别的注解(@Controller、@Component、@Service、@Resposity),这里配置了只扫描带有@Controller注解的类,是因为具有@Controller注解的类只能由DispatherServlet加载,接着开启了基于注解的驱动,此驱动可以实现依赖注入,对@Autowired、@Resource注解起作用,其实可以不配置,因为在组件扫描中已经包含了此功能;基于mvc的注解驱动,可以方便将请求参数邦定到控制器参数上。
最下面配置了视图解析器,这里使用的默认的视图解析器。
再看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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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"
> <context:component-scan base-package="com.cn.my">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> <!--配置一个数据源,使用spring提供的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property> </bean>
</beans>
spring的作用可以实现数据源的配置,事务或者service层的依赖注入,首先是组件扫描,扫描的对象是除了@Controller的注解,接着是一个数据源,这里使用了spring提供的数据源,其他还可以使用DBCP、C3P0、JNDI等方式,在后边讨论。
然后配置了一个数据访问对象JdbcTemplate,通过此对象可以对数据库进行操作。
至此我们的环境已经搭建完毕,下面是具体的使用,
编写Controller类,
由于我们使用了组件扫描,所以在类上使用注解@Controller
package com.cn.my.controllor; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import com.cn.my.service.CourseServiceInter; @Controller
@RequestMapping("/my/*")
public class MyController { @Autowired
private CourseServiceInter csi; //一般类型的url @RequestMapping("my")
public String method(HttpServletRequest request,@RequestParam("courseId") String courseid){ csi.findCourse(courseid);
return "success";
}
//restful形式的URL
//http://localhost:8080/my/my2/12
@RequestMapping("my2/{courseId}/{name}")
public String method2(@PathVariable("courseId") String courseId,@PathVariable("name") String name){
csi.findCourse(courseId+" "+name);
return "success";
}
}
除了在类上使用了@Controller注解,还是用了@RequestMapping注解,注明访问的路径,接着在方法method上又使用@RequestMapping注解,一个完整的url访问路径由类上的路径加方法上的路径组成,例:/my/my.do,这就是一个访问路径,我们还使用了service层的一个findCourse方法,service层的对象有框架依赖注入;在方法中使用了@RequestParam注解,此注解可以把url中的参数邦定到方法的参数上,其中@RequestParam中的值要和url中的请求参数名一致,后面的则是对应的方法中参数名。方法最后返回的是一个String类型的值,这里返回的是逻辑视图名,也可以返回JSON串,这种方式在后边介绍。
controller配置好之后,便可以访问了,这样一个sprigMVC的环境及controller的编写就完成了。
有不正之处欢迎指出,谢谢!
spring入门(五)【springMVC环境搭建】的更多相关文章
- SpringMVC环境搭建和详解
1.Spring容器和SpringMVC容器是父子容器 1.1 SpringMVC容器可以调用Spring容器中的所有内容 1.2 图示 2.SpringMVC环境搭建 1.导入jar包 2.在web ...
- springmvc环境搭建及实例
一. 软件环境 eclipse-jee-mars-R-win32-x86_64 jdk1.7.0_79 apache-tomcat-7.0.52 spring-framework-3.2.0.RELE ...
- 【转载】Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建
原地址:http://blog.csdn.net/wp1603710463/article/details/48247817#t16 Maven+druid+MyBatis+spring+Oracle ...
- Mule 入门之:环境搭建
Mule 入门之:环境搭建 JDK1.5或以上版本Eclipse3.3以上 下载与安装:目前最新版本为2.2.1 下载,下载后得到一名为mule-standalone-2.2.1.zip的压缩文件,解 ...
- 基于Maven的Spring + Spring MVC + Mybatis的环境搭建
基于Maven的Spring + Spring MVC + Mybatis的环境搭建项目开发,先将环境先搭建起来.上次做了一个Spring + Spring MVC + Mybatis + Log4J ...
- Cesium入门2 - Cesium环境搭建及第一个示例程序
Cesium入门2 - Cesium环境搭建及第一个示例程序 Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ 验 ...
- Spring框架入门之开发环境搭建(MyEclipse2017平台)
基于MyEclipse2017平台搭建Spring开发环境,这里MyEclipse已将Spring集成好了,我们只需要做一简单配置即可 一.环境配置 OS:Windows7 64位 IDE工具:MyE ...
- SpringMvc环境搭建(配置文件)
在上面的随笔里已经把搭建springmvc环境的基本需要的包都下下来了,拉下来就是写配置文件了. 下面左图是总的结构,右图是增加包 一.最开始当然是web.xml文件了,这是一个总的宏观配置 < ...
- springmvc环境搭建以及常见问题解决
1.新建maven工程 a) 打开eclipse,file->new->project->Maven->Maven Project b) 下一步 c) 选择创建的工程为 ...
随机推荐
- Bower是什么?
一.简介 Bower是一个客户端技术的软件包管理器,它可用于搜索.安装和卸载如JavaScript.HTML.CSS之类的网络资源.其它一些建立在Bower基础之上的开发工具,如YeoMan和Grun ...
- css双飞翼布局
双飞翼布局是一种比较灵活的布局,始于淘宝UED,玉伯提出的,他着重介绍的是双飞翼栅格布局. 三列布局为"双飞燕"布局,可以把三栏比作一只鸟,main部分相当是于鸟的身体,而lef ...
- 测试框架Mocha与断言expect
测试框架Mocha与断言expect在浏览器和Node环境都可以使用除了Mocha以外,类似的测试框架还有Jasmine.Karma.Tape等,也很值得学习. 整个项目源代码: 为什么学习测试代码? ...
- c#运算表达式
1.取补运算 操作符:~ 操作数:限定int,uint,long,ulong和枚举类型,返回值于操作数类型相同 sbyte,byte,short,ushort,也可以运算,但运算前都将隐式转换为int ...
- 实战MEF(4):搜索范围
在前面的文章中,几乎每个示例我们都会接触到扩展类的搜索位置,我们也不妨想一下,既然是自动扩展,它肯定会有一个或者多人可供查找的位置,不然MEF框架怎么知道哪里有扩展组件呢? 就像我们用导航系统去查找某 ...
- MVC4做网站后台:用户管理 —用户
这块进行用户管理,可以浏览.查询已注册的用户,修改用户资料,删除用户等.没有做添加用户,不知是否必要.列表页还是使用easyui的datagrid.这个思路跟用户组的方式差不多. 1.接口Interf ...
- Android调用系统相机功能
在常规应用开发过程中,我们经常会使用到手机的相机功能,通过调用系统相机方便快捷的帮助我们实现拍照功能,本篇我将带领大家实现一下,如何通过调用系统相机实现拍照. 第一种:调用系统相机拍照,通过返回的照片 ...
- 从零开始编写自己的C#框架(3)——开发规范
由于是业余时间编写,而且为了保证质量,对写出来的东西也会反复斟酌,所以每周只能更新两章左右,请大家谅解,也请大家耐心等待,谢谢大家的支持. 初学者应该怎样学习本系列内容呢?根据我自己的学习经验,一般直 ...
- 如何利用Oracle外部表导入文本文件的数据
同事最近在忙数据一致性比对工作,需要对不同文本文件中的数据进行比对,有的文件较大,记录较多,如果用普通的文本编辑器打开的话,很显然,会很卡,甚至打不开. 基于此,可将该文本文件的数据导入到数据库中,在 ...
- Visual Studio 2015 开发 ASP.NET 5 有何变化?
本篇博文目录: ASP.NET 5 模版 ASP.NET 5 目录结构 前端管理工具 无编译开发 Microsoft Git Provider 智能感知和错误信息 Smart Unit Testing ...