springmvc20170322
<?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的更多相关文章
随机推荐
- codeforces_302D
D. Yaroslav and Time time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 06C语言运算符
C语言运算符 算术运算符 运算符 描述 + 把两个操作数相加 - 从第一个操作数中减去第二个操作数 * 把两个操作数相乘 / 分子除以分母 % 取模运算符,整除后的余数 ++ 自增运算符,整数值增加 ...
- 07Java Server Pages
Java Server Pages JSP是一种Java servlet,主要用于实现Java web应用程序的用户界面部分. JSP全称Java Server Pages,是一种动态网页开发技术.它 ...
- vuex与redux,我们都一样
vuex与redux的主要区别: redux:生成的全局数据流是通过每个组件的props逐层传递到各个子组件的,通过@connect装饰器绑定在this.props上面. vuex :生成的全局数据则 ...
- Linux常用命令——帮助命令
1.帮助命令:man man 命令 获取指定命令的帮助 [dmtsai@study ~]$ man date DATE (1) User Commands DATE(1) #注意这个(1),代表的是m ...
- java计算两地距离(公里)
//目标经度,目标纬度,自己经度,自己纬度 public static double getDistance(double lon1, double lat1, double lon2, double ...
- mysql导出数据到excel的两种方式
使用第一种方式如果数据中有换行符的话会自动换行,但使用第二种方式就不会出现这种效果了.两种方式自己选择哈 1:select * from into outfile 'c:/Users/a.xls' t ...
- kubeadmin 安装k8s集群
系统设置 CentOS Linux release 7.6.1810 (Core) 修改主机名 vim /etc/hostname k8s-master hostname -F /etc/hostna ...
- 关于ajax跨域解读
首先要了解何为跨域,(协议.域名.端口任意一个不同)的web资源.如何解决跨域:1,jsonp 它只支持GET请求而不支持POST等其它类型的HTTP请求:例如angular 中,如下使用,$http ...
- 小程序button默认样式透彻理解
微信小程序有一个默认样式,特别是有一个外边框,虽然看起来不别扭,但是自己每次设置border:0:都不生效,写成内联的样式也不生效,后来才知道里面的边框是伪元素的边框,这里的伪元素可以理解为用css动 ...