Spring MVC 框架搭建配置方法及详解
作者:mdxy-dxy 发布时间:2024-03-13 19:04:36
现在主流的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>
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相匹配,参数类型会自动被转换
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();
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
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时,便默认执行该方法
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
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) {
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
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) {
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
return "loginError";
}
return "loginSuccess";
}
}
三、结束语
掌握以上这些Spring MVC就已经有了很好的基础了,几乎可应对与任何开发,在熟练掌握这些后,便可更深层次的灵活运用的技术,如多种视图技术,例如 Jsp、Velocity、Tiles、iText 和 POI。Spring MVC框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术。


猜你喜欢
- 本文研究的主要是python可视化包Bokeh的相关内容,具体如下。问题:需要把pandas的数据绘图并通过网页显示,matplotlib需
- 这几天写代码中遇到的一个常见问题,在Python中如何批量的生成一些变量,如生成变量X1, X2, X3,并在后续的方法中调用,完成赋值、取
- 目录logging的简单使用|2logging常见对象|3logging基本使用|4logging之Formatter对象|5logging
- 前言这个系列的文章我们使用以下的顺序进行讲解:Pattern 详解;Matcher 详解;正则表达式语法详解。接下来先来介绍 Pattern
- 你同样可以使用cache标签来缓存模板片段。 在模板的顶端附近加入{% load cache %}以通知模板存取缓存标签。模板标签{% ca
- #coding: utf-8 import Image,ImageDraw,ImageFont,os,string,random,Image
- 很多朋友对FrontPage2003中增加的网页布局功能很感兴趣,现在我们一起来深入了解这一实用功能。 用FrontPage200
- 前言栈、队列和优先级队列都是非常基础的数据结构。Python作为一种“编码高效”的语言,对这些基础的数据结构都有比较好的实现。在业务需求开发
- 引由于需要解决大批量Excel处理的事情,与其手工操作还不如写个简单的代码来处理,大致选了一下感觉还是Python最容易操作。安装库Pyth
- 通过valuelist的queryMap传递过来的参数默认都为string类型,在valuelist配置文件的hql中,如果直接将该值赋给整
- 有这么一段代码,可以先看一下有没有什么问题,作用是输入一段json字符串,反序列化成map,然后将另一个inputMap的内容,merge进
- 前言近几年,制造业作为国民经济主体,是国家创造力、竞争力和综合国力的重要体现。作为制造强国建设的主攻方向,可以说,智能制造发展水平关乎我国未
- 简介canvas 是HTML5 提供的一种新标签,它可以支持 JavaScript 在上面绘画,控制每一个像素,它经常被用来制作小游戏,接下
- 前言CSDN博客好久没有换过头像了,想换个新头像,在相册里面翻来翻去,然后就找到以前养的小宠物的一些照片,有一张特别有意思惊恐到站起来的金丝
- 本文实例讲述了js实现适用于素材网站的黑色多级菜单导航条效果。分享给大家供大家参考。具体如下:这是一款适用于素材网站的黑色多级菜单导航条,无
- 说明1、字典中没有下标的概念,使用key值访问字典中对应的value值。当访问的key值不存在时,代码会报错。2、get('key&
- 数据合并是数据处理过程中的必经环节,pandas作为数据分析的利器,提供了四种常用的数据合并方式,让我们看看如何使用这些方法吧!1.conc
- 症状 在 Service Pack 4 (SP 4) 运行 Microsoft Windows Server 2003、 Microsoft
- 在python中利用numpy创建一个array, 然后我们想获取array的最大值,最小值。可以使用一下方法:一、创建数组这样就可以获得一
- 前言昨天上线后通过系统报警发现了一个bug,于是紧急进行了回滚操作,但是期间有用户下单,数据产生了影响,因此需要排查影响了哪些订单,并对数据