• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >JSP > Spring MVC 框架搭建配置方法及详解

Spring MVC 框架搭建配置方法及详解

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-11

通过本文主要向大家介绍了spring mvc 框架,spring mvc 框架搭建,spring mvc框架结构,spring mvc 框架源码,spring mvc验证框架等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

现在主流的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>1</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>
</div>

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>
</div>

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>
</div>

二、详解

  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相匹配,参数类型会自动被转换 
     
    if (!"admin".equals(username) || !"admin".equals(password) || age < 5) { 
      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(); 
 



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Spring MVC 框架搭建配置方法及详解

相关文章

  • 2017-05-11jsp从数据库获取数据填充下拉框实现二级联动菜单的方法
  • 2017-05-11response.setContentType()的作用及MIME参数详解
  • 2017-05-11Jsp页面实现文件上传下载类代码第1/2页
  • 2017-05-11jsp Hibernate 函数简介
  • 2017-05-11struts2中一个表单中提交多个请求的例子(多个提交按钮)
  • 2017-05-11Java Servlet生成JSON格式数据并用jQuery显示的方法
  • 2017-05-11JSP由浅入深(11)—— 标记库
  • 2017-05-11十、会话状态
  • 2017-05-11jsp中 ajax的get请求的中文乱码问题的解决方法
  • 2017-05-11JSP常见的三个编译指令page、include、taglib

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • JSP九大内置对象的作用和用法总结
    • 通用弹出层页面(兼容IE、firefox)可关闭控制宽高及屏蔽背景
    • Java 获取URL的内容
    • 检测输入的字符是否为0-9的数字(测试)
    • 利用JSP建立Web站点
    • JSP之plugin的使用
    • java SOAPHEADER的web service
    • js实现随机的四则运算题目效果
    • IIS6 和Tomcat5 的整合
    • jsp实现页面实时显示当前系统时间的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有