springMVC入门配置案例
1、spring的jar包下载
进入http://repo.springsource.org/libs-release-local/,然后依次点击org/-->springframework-->spring,即可根据需要下载spring框架的压缩包。
2、common-logging的jar包
spring核心容器依赖common-logging的jar包,下载地址http://commons.apache.org,依次点击Releases-->Logging,可选择commons-logging-1.2-bin.zip下载。
3、springMVC的DispatcherServlet
DispatcherServlet是一个Servlet,继承自HttpServlet。它是springMVC的前端控制器,用于拦截请求,与客户端交互。
4、springMVC入门案例
环境搭建:建立dynamic web project,导入springMVC需要的jar包,在web.xml中配置springMVC前端控制器,编写springMVC.xml核心配置文件,编写controller。
web.xml中配置springMVC前端控制器
<!-- 配置springMVC前段控制器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springMVC.xml</param-value>
</init-param>
<!-- Tomcat启动时加载该servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
核心配置文件springMVC.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置@Controller注解扫描 -->
<context:component-scan base-package="com.alphajuns.controller"></context:component-scan>
<!-- 配置处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
controller类
package com.alphajuns.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HelloController { @RequestMapping(value="/hello")
public ModelAndView hello() {
// 创建模型和视图对象
ModelAndView mv = new ModelAndView();
// 添加模型
mv.addObject("message", "Hello SpringMVC!");
// 设置视图
mv.setViewName("/WEB-INF/content/welcome.jsp");
// 返回模型和视图
return mv;
}
}
页面的话主要是取值,可用el表达式,如welcome.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${requestScope.message }
</body>
</html>
访问成功

如果不指定加载配置文件路径,默认情况下,加载springMVC配置文件会去WEB-INF文件夹下查找对应的[servlet-name]-servlet.xml。
解析springMVC配置文件会根据配置文件创建一个WebApplicationContext容器对象,WebApplicationContext继承自ApplicationContext容器。
springMVC建议把所有视图页面存放在WEB-INF文件夹下,这样可以保护视图,避免直接向视图页面发送请求。
springMVC入门配置案例的更多相关文章
- SpringMVC入门学习案例笔记
一.数据库环境用mysql,数据库建表语句如下: /* SQLyog v10.2 MySQL - 5.1.72-community : Database - mybatis ************* ...
- SpringMVC入门配置和简单实现
web.xml的配置 <!-- springmvc中央控制器 --> <servlet> <servlet-name>springmvc</servlet-n ...
- springMVC入门配置及helloworld实例
1. 新建web project 2. 往lib里copy必须的jar 3. 改写web.xml <?xml version="1.0" encoding="UTF ...
- Hibernate入门配置案例
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自 ...
- SpringMVC入门案例及请求流程图(关于处理器或视图解析器或处理器映射器等的初步配置)
SpringMVC简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 Spring结构图 Spr ...
- Java开发学习(二十三)----SpringMVC入门案例、工作流程解析及设置bean加载控制
一.SpringMVC概述 SpringMVC是隶属于Spring框架的一部分,主要是用来进行Web开发,是对Servlet进行了封装.SpringMVC是处于Web层的框架,所以其主要的作用就是用来 ...
- Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...
- Spring-MVC开发步骤(入门配置)
Spring-MVC开发步骤(入门配置) Step1.导包 spring-webmvc Step2.添加spring配置文件 Step3.配置DispatcherServlet 在web.xml中: ...
- springMVC的一些入门配置
1.springMVC的描述 1.1.SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架. 1.2.SpringMVC的作用是实现页面和后 ...
随机推荐
- Selenium(6)
一.定位页面元素 1.高级定位:层级定位 思路:先定位到祖先节点,在定位该祖先节点范围内的子节点 2.高级定位:Xpath定位(重点) (1)Xpath定位:Xpath就是一个表达式,表示元素的路径, ...
- 51Nod 1714 1位数SG异或打表
SG[i]表示一个数二进制下有i个1的SG值 SG[0]=0 打表: #include<bits/stdc++.h> using namespace std; ]; ]; , x; int ...
- ubuntu下mysql的用户添加、授权、取消授权
一.添加用户 新增用户会有两种方式的,一种是使用create命令,另一种是直接回使用grant 命令 create user 名字@登陆地址 identified by "密码"; ...
- 搭建私有CA
一.实验目的 搭建私有CA并使其可以实现公司内部的的签名服务. 二.实验环境: 系统架构:Centos7(服务器).Centos6(需要申请证书的服务器)需要的软件包:openssl.openssl- ...
- asp.net操纵Oracle存储过程
在bloginfo数据库中,利用用户名 system 密码 scy251147 选择超级管理员登陆 创建如下存储过程: 1 /*存储过程*/ 2 create or replace procedu ...
- 用python 遍历文件夹中所有.dcm文件
import dicomimport os def eachFile(filepath): for file in os.listdir(filepath): child = os.path.join ...
- (转载) Consul 使用手册(感觉比较全了)
使用consul 介绍 Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键特性: 服务发现 Consul的客户端可用提供一个服务,比如 api 或者 ...
- TCP数据段格式+UDP数据段格式详解
TCP 报文格式 TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议. TCP 报文段的报头有 10 个必需的字段和 ...
- vue3.0以上关于打包后出现空白页和路由不起作用
1.解决页面空白,找不到资源 在项目根目录中的vue.config.js中publicPath: '/'修改为publicPath: './',如果没有这个文件,新建一个,基础代码为: module. ...
- 学习shell(二)
条件分支: (条件表达式的中括号里面 空格不可以省略) = ] then echo '2 = 2'; else echo '2 != 2'; fi # 上面的代码不使用缩进, 并不会出错, 但不应该 ...