<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name> <!-- 配置 springmvc的核心控制器
这里的 <servlet-name>springmvc</servlet-name>
springmvc就是规定了 我们springmvc的配置文件的名称
<servlet-name>-servlet.xml
-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
<!-- 为什么不能配置/*
我们打开浏览器 第一次访问 项目名/hello ,会通过我们的handlerMaping找 有没有bean name="/hello"!
有就进入对应的controller进行操作,之后根据controller中返回的ModelAndView ===》hello01!
让试图解析器进行解析 :解析之后 变成了/SpringMVC001/WEB-INF/jsp/hello01.jsp 如果我们配置的是 /* 那么解析之后的路径也会被当成一个Bean name来处理,很显然我们没有这个bean,所以会报错! /: 只会匹配 /login /index 不能匹配 /*.jsp 这样的后缀url
/*:匹配所有的请求 所以我们hello01.jsp 也会被拦截
-->
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 采用默认的方式 BeanNameUrlHandlerMapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- name="/hello" 用户的请求 class就是我们的Controller处理器-->
<bean name="/hello01" class="cn.rick.controller.HelloController"/> <!-- 视图解析器 项目名/web-inf/jsp/springmvc.jsp 需要设置前缀 和后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/> </bean> </beans>

springmvc-servlet.xml

package cn.rick.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController; public class HelloController extends AbstractController { // 第一个springmvc小例子
/**
* handleRequestInternal():对应struts2的 execute()
* ModelAndView:返回值 需要核心配置文件中的试图解析器 解析
*/
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("第一个springmvc小例子....");
// hello01就是我们返回的视图名称 但是 现在只是一个字符串
return new ModelAndView("hello01");
} }

HelloController.java

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功
</body>
</html>

hello01.jsp

springmvc20170322的更多相关文章

随机推荐

  1. Swift mutating Equatable Hashable 待研究

    Swift mutating Equatable Hashable 待研究

  2. IIS发布403报错

    报错信息如下图 解决方案,inetmgr打开IIS,找到对应网站的目录浏览,双击 开启

  3. Flask框架 之abort、自定义错误、视图函数返回值与jsonify

    一.abort函数 使用abort函数可以立即终止视图函数的执行,并可以返回给前端特定的值. abort函数的作用: 1.传递状态码,必须是标准的http状态码 2.传递响应体信息 @app.rout ...

  4. php利用array_filter()过滤数组空值

    利用array_filter过滤数组空值 <?php $array = array( 0 => '霜天部落', 1 => false, 2 => 1, 3 => null ...

  5. 浅谈:nodejs在cmd提示不是内部或外部命令

    今天用cmd安装个库,结果发现node不是内部命令,检查后发现上次重装nodejs换了个安装位置,path环境变量忘改了. 找到变量值中node的安装地址,比如C:develop\nodejs,如果不 ...

  6. 并查集(Union Find)的基本实现

    概念 并查集是一种树形的数据结构,用来处理一些不交集的合并及查询问题.主要有两个操作: find:确定元素属于哪一个子集. union:将两个子集合并成同一个集合. 所以并查集能够解决网络中节点的连通 ...

  7. Python字符的转义

    参考原文 廖雪峰Python教程 字符的转义 字符串是以单引号' 或双引号" 括起来的任意文本,比如'abc',"xyz".''或""本身只是一种表示 ...

  8. python_ 学习笔记(基本数据类型)

    python3有6中标准数据类型:Number(数字).String(字符串).List(列表).Tuple(元组).Dictionary(字典).Set(集合)不可变数据:Number.String ...

  9. Nginx + Lets'encrypt 实现HTTPS访问七牛空间资源

    上一篇文章 为七牛云存储空间绑定自定义域名,并使用七牛云提供的免费SSL证书,将自定义加名升级为HTTPS 我们提到利用七牛的免费SSL证书,将自定义加名升级为HTTPS的方法. 不知道有没有小伙伴会 ...

  10. CCF201503-2 数字排序 java(100分)

    试题编号: 201503-2 试题名称: 数字排序 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出. 输 ...