说明:在此只说明helloword的简单实现,通过helloword例子先了解springMvc是这样工作的,然后在一步步的研究原理

配置web.xml

  1.配置servlet

    servlet-class:吧请求接入到springmvc中

    url-pattern:拦截请求的路径

    param-value:加载的springMvc文件地址
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

  2.配置springmvc

<?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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.atguigu.springmvc"></context:component-scan> <!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

  

配置扫描的包:<context:component-scan base-package="com.csy.springmvc"></context:component-scan>
配置实体解析器:通过前缀+返回值+后缀的方式拼装成跳转地址,然后通过转发跳转到目标页面
以下是具体的实列

1.springmvc的helloWord

1.建立hellowordweb项目

2.在web.xml中配置前端的请求路劲,把请求接入到springMvc中。

     <servlet>

         <servlet-name>dispatcherServlet</servlet-name>

         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

         <!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
<!--
实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
-->
<!--
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup> </servlet>

2.配置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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自定扫描的包 -->
<context:component-scan base-package="com.atguigu.springmvc"></context:component-scan> <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

3.配置请求处理器(controller

package com.atguigu.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HelloWorld { /**
* 1. 使用 @RequestMapping 注解来映射请求的 URL
* 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析:
* 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
*
* /WEB-INF/views/success.jsp
*
* @return
*/
@RequestMapping("/helloworld")
public String hello(){
System.out.println("hello world");
return "success";
} }

最新文章

  1. Design7:数据删除设计
  2. ucenter用户登录过程
  3. svn的安装配置
  4. 在centos6.5中安装mysql5.7
  5. HackerRank &quot;Morgan and a String&quot;
  6. 下载安装和OpenCV匹配的Android开发环境
  7. [Everyday Mathematic]20150217
  8. js 连续赋值
  9. 使用Gradle构建Android应用内测版本
  10. 第十七章——配置SQLServer(4)——优化SQLServer实例的配置
  11. jquery的跳转.禁止全url跳转.只需控制器+方法
  12. Python图片爬虫
  13. Windows7系统基础操作
  14. 将docker镜像上传到docker hub
  15. Polynomial_0
  16. JDBC 中preparedStatement和Statement区别
  17. 如何用git将项目代码上传到github
  18. 前端 --- 4 js
  19. 【一】php 基础知识
  20. 8.1 服务器开发 API 函数封装,select 优化服务器和客户端

热门文章

  1. springboot(三)-使用JSP
  2. vue 在使用v-html绑定的时候,里面的元素不会继承外部的css,解决方案
  3. 【总结】kali(amd64)中安装nessus
  4. 阿里Tree-based Deep Match(TDM) 学习笔记
  5. html5中新增的语义化的标签
  6. ckeditor和ckfinder
  7. Nginx设置日志分割方法
  8. tabs(标签页的现成页面)原生js写法
  9. pat03-树2. List Leaves (25)
  10. java使用POI进行 Excel文件解析