spring mvc 框架搭建及详解
现 在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了。不过要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理下载地址
。
一、Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0)
1. jar包引入
Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、cglib-nodep-2.1_3.jar
Hibernate
3.6.8:hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-
2.7.6.jar、commons-collections-3.1、dom4j-1.6.1.jar、javassist-
3.12.0.GA.jar、jta-1.1.jar、slf4j-api-1.6.1.jar、slf4j-nop-1.6.4.jar、相应数据库的
驱动jar包
2. web.xml配置(部分)
- <!-- Spring MVC配置 -->
- <!-- ====================================== -->
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring-servlet.xml</param-value> 默认
- </init-param>
- -->
- </load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <!-- Spring配置 -->
- <!-- ====================================== -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:config/applicationContext.xml</param-value>
- </context-param>
3. spring-servlet.xml配置
spring-servlet这个名字是因为上面web.xml中<servlet-name>标签配的值为
spring(<servlet-name>spring</servlet-name>),再加上“-servlet”后缀而
形成的spring-servlet.xml文件名,如果改为springMVC,对应的文件名则为springMVC-servlet.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:p="http://www.springframework.org/schema/p"
- 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://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/context <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>">
- <!-- 启用spring mvc 注解 -->
- <context:annotation-config />
- <!-- 设置使用注解的类所在的jar包 -->
- <context:component-scan base-package="controller"></context:component-scan>
- <!-- 完成请求和注解POJO的映射 -->
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
- <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />
- </beans>
4. 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: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-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- 采用hibernate.cfg.xml方式配置数据源 -->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:config/hibernate.cfg.xml</value>
- </property>
- </bean>
- <!-- 将事务与Hibernate关联 -->
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref local="sessionFactory"/>
- </property>
- </bean>
- <!-- 事务(注解 )-->
- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
- <!-- 测试Service -->
- <bean id="loginService" class="service.LoginService"></bean>
- <!-- 测试Dao -->
- <bean id="hibernateDao" class="dao.HibernateDao">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- </beans>
二、详解
Spring MVC与Struts从原理上很相似(都是基于MVC架构),都有一个控制页面请求的Servlet,处理完后跳转页面。看如下代码(注解)下载地址 :
- package controller;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import entity.User;
- @Controller //类似Struts的Action
- public class TestController {
- @RequestMapping("test/login.do") // 请求url地址映射,类似Struts的action-mapping
- public String testLogin(@RequestParam(value="username")String username, String password, HttpServletRequest request) {
- // @RequestParam是指请求url地址映射中必须含有的参数(除非属性required=false)
- // @RequestParam可简写为:@RequestParam("username")
- if (!"admin".equals(username) || !"admin".equals(password)) {
- return "loginError"; // 跳转页面路径(默认为转发),该路径不需要包含spring-servlet配置文件中配置的前缀和后缀
- }
- return "loginSuccess";
- }
- @RequestMapping("/test/login2.do")
- public ModelAndView testLogin2(String username, String password, int age){
- // request和response不必非要出现在方法中,如果用不上的话可以去掉
- // 参数的名称是与页面控件的name相匹配,参数类型会自动被转换
- ) {
- return new ModelAndView("loginError"); // 手动实例化ModelAndView完成跳转页面(转发),效果等同于上面的方法返回字符串
- }
- return new ModelAndView(new RedirectView("../index.jsp")); // 采用重定向方式跳转页面
- // 重定向还有一种简单写法
- // return new ModelAndView("redirect:../index.jsp");
- }
- @RequestMapping("/test/login3.do")
- public ModelAndView testLogin3(User user) {
- // 同样支持参数为表单对象,类似于Struts的ActionForm,User不需要任何配置,直接写即可
- String username = user.getUsername();
- String password = user.getPassword();
- int age = user.getAge();
- ) {
- return new ModelAndView("loginError");
- }
- return new ModelAndView("loginSuccess");
- }
- @Resource(name = "loginService") // 获取applicationContext.xml中bean的id为loginService的,并注入
- private LoginService loginService; //等价于spring传统注入方式写get和set方法,这样的好处是简洁工整,省去了不必要得代码
- @RequestMapping("/test/login4.do")
- public String testLogin4(User user) {
- if (loginService.login(user) == false) {
- return "loginError";
- }
- return "loginSuccess";
- }
- }
以上4个方法示例,是一个Controller里含有不同的请求url,也可以采用一个url访问,通过url参数来区分访问不同的方法,代码下载地址 如下:
- package controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- @Controller
- @RequestMapping("/test2/login.do") // 指定唯一一个*.do请求关联到该Controller
- public class TestController2 {
- @RequestMapping
- public String testLogin(String username, String password, int age) {
- // 如果不加任何参数,则在请求/test2/login.do时,便默认执行该方法
- ) {
- return "loginError";
- }
- return "loginSuccess";
- }
- @RequestMapping(params = "method=1", method=RequestMethod.POST)
- public String testLogin2(String username, String password) {
- // 依据params的参数method的值来区分不同的调用方法
- // 可以指定页面请求方式的类型,默认为get请求
- if (!"admin".equals(username) || !"admin".equals(password)) {
- return "loginError";
- }
- return "loginSuccess";
- }
- @RequestMapping(params = "method=2")
- public String testLogin3(String username, String password, int age) {
- ) {
- return "loginError";
- }
- return "loginSuccess";
- }
- }
其实RequestMapping在Class上,可看做是父Request请求url,而RequestMapping在方法上的可看做是子Request请求url下载地址 ,父子请求url最终会拼起来与页面请求url进行匹配,因此RequestMapping也可以这么写:
- package controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- @RequestMapping("/test3/*") // 父request请求url
- public class TestController3 {
- @RequestMapping("login.do") // 子request请求url,拼接后等价于/test3/login.do
- public String testLogin(String username, String password, int age) {
- ) {
- return "loginError";
- }
- return "loginSuccess";
- }
- }
三、结束语
掌握以上这些Spring
MVC就已经有了很好的基础了,几乎可应对与任何开发,在熟练掌握这些后,便可更深层次的灵活运用的技术,如多种视图技术,例如
Jsp、Velocity、Tiles、iText 和 POI。Spring MVC框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术。
spring mvc 框架搭建及详解的更多相关文章
- Spring MVC框架搭建及其详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Spring学习 6- Spring MVC (Spring MVC原理及配置详解)
百度的面试官问:Web容器,Servlet容器,SpringMVC容器的区别: 我还写了个文章,说明web容器与servlet容器的联系,参考:servlet单实例多线程模式 这个文章有web容器与s ...
- Spring MVC框架搭建
Spring MVC篇一.搭建Spring MVC框架 本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容. 一.项目结构 本项目使用idea intellij ...
- Spring MVC 框架搭建及具体解释
如今主流的Web MVC框架除了Struts这个主力 外.其次就是Spring MVC了,因此这也是作为一名程序猿需要掌握的主流框架.框架选择多了.应对多变的需求和业务时,可实行的方案自然就多了. 只 ...
- Java Spring MVC框架搭建(一)
环境准备 >>>>>>java JDK和tomcat,eclipse 1.创建项目 2.项目名称自定义,这边为demo 3.我们已经创建完一个动态网站的项目,还得下 ...
- Spring MVC原理及配置详解
Spring MVC概述: Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单.这些控制 ...
- 《转载》Spring MVC之@RequestBody, @ResponseBody 详解
引言: 接上一篇文章讲述处理@RequestMapping的方法参数绑定之后,详细介绍下@RequestBody.@ResponseBody的具体用法和使用时机: 简介: @RequestBody 作 ...
- Spring MVC之@RequestBody@ResponseBody详解
引言: 接上一篇文章讲述处理@RequestMapping的方法参数绑定之后,详细介绍下@RequestBody.@ResponseBody的具体用法和使用时机: 简介: @RequestBody 作 ...
- Spring MVC 之@Controller@RequestMapping详解
一:配置web.xml 1)问题:spring项目中有多个配置文件mvc.xml dao.xml 2)解决:在web.xml中 <init-param> <param-name& ...
随机推荐
- CLR via C#深解笔记六 - 泛型
面向对象编程一个好处就是“代码重用”,极大提高了开发效率.如是,可以派生出一个类,让它继承基类的所有能力,派生类只需要重写虚方法,或添加一些新的方法,就可以定制派生类的行为,使之满足开发人员的需求. ...
- AndroidUI优化工具——HierarchyViewer
先说些题外话,希望路过的各位支持,博主有幸成为2013年度博客之星的候选人,期待你的一票,谢谢. 投票猛击: http://vote.blog.csdn.net/blogstaritem/blogst ...
- php分享三十三:常量
一:常量定义 1:在脚本执行期间该值不能改变(除了所谓的魔术常量,它们其实不是常量) 2:常量默认为大小写敏感 3:命名规则:用正则表达式是这样表达的:[a-zA-Z_\x7f-\xff][a-zA- ...
- LoRaWAN协议(二)--LoRaWAN MAC数据包格式
名词解析 上行:终端的数据发送经过一个或多个网关中转到达网络服务器. 下行:由网络服务器发送给终端设备,每条消息对应的终端设备是唯一确定的,而且只通过一个网关中转. LoRaWAN Classes L ...
- 剑指架构师系列-tomcat6通过IO复用实现connector
由于tomcat6的配置文件如下: <Connector port="80" protocol="org.apache.coyote.http11.Http11Ni ...
- Direct2D开发:绘制网格
转载请注明出处:http://www.cnblogs.com/Ray1024 一.引言 最近在使用Direct2D进行绘制工作中,需要实现使用Direct2D绘制网格的功能.在网上查了很多资料,终于实 ...
- Mysql学习笔记(六)增删改查
PS:数据库最基本的操作就是增删改查了... 学习内容: 数据库的增删改查 1.增...其实就是向数据库中插入数据.. 插入语句 insert into table_name values(" ...
- LitePal + Gson + Volley的ORM框架尝试方案
为了紧跟技术潮流,目前的项目开始采用ORM的思想进行重新设计. 数据库采用轻量级ORM框架LitePal,Json解析采用Gson,网络框架采用Volley. 如果只是单纯的将这些第三方框架引进来,事 ...
- JS魔法堂:浏览器模式和文档模式怎么玩?
一.前言 从IE8开始引入了文档兼容模式的概念,作为开发人员的我们可以在开发人员工具中通过“浏览器模式”和“文档模式”(IE11开始改为“浏览器模式”改成更贴切的“用户代理字符串”)品味一番,它的出现 ...
- 转载:第二弹!全球首个微信小程序(应用号)开发教程!通宵吐血赶稿!每日更新!
今天一波三折,承受了超出预料的压力和煎熬,最后还是决定继续放出我的更新教程.我想我一没有泄露公司的代码,二没有提供泄露开发工具下载,只是从程序猿角度写了篇开发日志.我已经做好了最坏的准备,就算放弃这份 ...