基于Spring MVC框架的环境搭建

根据上一篇spring mvc工作原理的介绍,我们一步步搭建环境。

如果spring mvc工作原理还没弄清楚的话,建议先看一看spring mvc工作原理的分析

准备工作

  1. IDE : 我用的是Eclipse
  2. spring Jar包,没有的话,可以去spring官网下载
  3. 将 jar包拷贝到lib文件夹下

配置前端控制器(DispatherServlet)

根据spring mvc工作原理,客户从浏览器发出请求,首先接受请求的是前端控制器,所以我们先配置前端控制器

在新建的动态web项目中找到配置文件web.xml,没有的自己新建一个。web.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file></welcome-file>
	</welcome-file-list>


	<!-- 前端控制器配置 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!--contextConfigLocation配置springmvc加载的配置文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- 指定将会配置映射器 适配器及视图解析器等的配置文件路径 -->
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!-- 从浏览器发出的请求路径(指定格式将会被前端控制器接受, 这里后缀名以.action结尾的路径会被前端控制器接受到) -->
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

</web-app>



配置映射器 适配器及视图解析器等

在web.xml中有这么几行配置:

<!--contextConfigLocation配置springmvc加载的配置文件  -->
          <init-param>
              <param-name>contextConfigLocation</param-name>
                 <!-- 指定将会配置映射器 适配器及视图解析器等的配置文件路径 -->
              <param-value>classpath:springmvc.xml</param-value>
          </init-param>

这是初始参数的配置:配置spring mvc加载的配置文件,例如映射器 适配器及视图解析器等的配置文件,这些配置都指定在springmvc.xml中配置,所以接下来,我们要新建springmvc.xml来配置映射器 适配器及视图解析器等,处于规范所有地新建配置文件最好放在一个新建的sources folder文件夹中分类管理,一遍后期与其他框架整合时好管理与区分。

springmvc.xml配置如下:

<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:mvc="http://www.springframework.org/schema/mvc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
     http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.2.xsd
   http://www.springframework.org/schema/mvc   
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
   
<!-- 注解的映射器和适配器的配置(默认加载了很多参数绑定方法 比如json转换解析器) --> 
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 对于注解的控制器(controller)可以单个配置     实际开发中不推荐使用  推荐使用下面的扫描方式-->
<!-- 
<bean class="com.hbeu.controller.controller1"></bean>
 -->
<!-- 可以扫描 controller  service  mapper
<context:component-scan base-package="com.hbeu.controller"></context:component-scan>
 -->
<context:component-scan base-package="com.hbeu.controller"></context:component-scan>


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

说明一点,整个环境的搭建是基于注解的方式配置的,也是最简单,最简洁的配置。

<mvc:annotation-driven></mvc:annotation-driven>

这行配置,涵盖了映射器和适配器的配置,同时也默认加载了很多参数绑定方法

<context:component-scan base-package="com.hbeu.controller"></context:component-scan>

这行配置,加载注解驱动,启动注解

然后我们就需要去编写处理器了

编写处理器(Handler)

package com.hbeu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class controller1 {
	@RequestMapping("/test.action")
	public ModelAndView test() throws Exception {
		
		ModelAndView mav=new ModelAndView();
		mav.setViewName("/welcome");
		return mav;
	}
}

@Controller表示该类是一个控制器类

@RequestMapping("/test.action")表示该控制器处理的所有请求都被映射到test方法所制定的路径

接下来就是创建视图了

创建视图

一定要注意路径,根据springmvc.xml配置的视图解析器,前缀名与后缀名,所以新建的jsp一定要建在/WEB-INF/page路径下,否则会报404错误!

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
Hellow  springMVC!
</body>
</html>

启动服务器调试

在浏览器中输入路径:http://localhost:8080:springmvc/welcome.action

成功时会显示:Hello  springMVC!

至此,最简单的spring mvc环境就搭建好了,后期需要用到什么功能在进行配置!




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# demoWeb 一个基于SpringMVC的web框架 1.0.5 从web项目迁移成maven项目 1.0.6 增加菜单框架ext实现,类路径调整 1.0.7 增加http工具类,demo例子 1.0.8 socket工具类,权限组件,菜单组件,jdbc分页支持多种数据库,ant路径工具类,增加jquery easyUI 1.0.9 版本管理,服务根路径工具类,文件上传工具类 1.0.10 集成ueditor在线编辑器 1.0.11 地址联动 1.0.12 Excel工具类 Word工具类 Java NIO实现socket工具类 分布式session jdk升级到1.7 嵌入式redis服务(只支持linux) 1.0.13 修改默认的beanName生成策略,controller参数扩展 1.0.14 分布式session使用zookeeper 1.0.15 zookeeper工具类优化 增加工具类 1.0.16 页面html标志修改 httpclient中文支持 工具类增强(zip,reflect,thread) 1.0.17 ftp服务端和客户端工具类,配置文件maven和web项目路径统一 1.1.0 soapui工具类(web版本) properties等工具类 1.1.1 工具类数据校验 jsp自定义标签 Spring自定义注解 默认requestMapping 1.1.2 代码生成器 1.1.3 首页修改 dateformat.js 时间参数转换 SpringMVC配置文件集中 快递参数接口 1.1.4 des加解密字符串和文件 1.1.5 redis 加锁,redis升级成2.8.2 freemarker工具类 1.1.6 spring websocket 实现在线聊天 maven升级jdk1.8 jetty9.2.4 web升级jdk1.7 tomcat7 1.1.7(maven only) 包名修改 从此不再支持web版本,只支持maven版本 1.1.8 jquery 图片预览插件 图片滚动显示插件 1.1.9 jquery实现鼠标在按钮上显示窗口,离开窗口和按钮时消失 1.1.10 rabbitMQ集成 视频截图 图片缩略图旋转 集成Mybatis 使用数据库连接池druid dubbo使用 1.1.11 集成Spring Cache,FastJson Spring Cache增加redis缓存实现 Mybatis使用二级缓存,增加redis实现 增加reactJs 增加Mybatis插件pageHelper,Mapper 1.1.12 使用draft富文本编辑器 增加ant design 代码生成器功能增强
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值