写一个spring mvc后台传值到前台的一个小例子。

分为以下几个步骤:

1.创建web项目。

导入项目包。具体有如下:

  • spring-aop-4.0.4.RELEASE.jar
  • spring-beans-4.0.4.RELEASE.jar
  • spring-context-4.0.4.RELEASE.jar
  • spring-core-4.0.4.RELEASE.jar
  • spring-expression-4.0.4.RELEASE.jar
  • spring-web-4.0.4.RELEASE.jar
  • spring-webmvc-4.0.4.RELEASE.jar
  • commons-logging-1.1.1.jar

2.配置web.xml文件,配置servlet

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

<!--<init-param>这个参数是为了加载下一步配置的spring配置文件的 ,默认spring mvc的加载是将spring配置文件放在web-inf下而且名称是web.xml配置的该 servlet名字-servlet.xml  会在启动时自动去改路径寻找该配置文件,我们这里将它放在了src下去加载-->

<init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:helloSpring-servlet.xml</param-value>
  </init-param>

<!--在项目启动时加载配置文件-->
  <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>
  <servlet-name>helloSpring</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

3.添加spring配置文件并进行配置。

我的spring配置文件叫helloSpring-servlet.xml,放在src下。

在<beans></beans>元素里添加如下内容:

<!-- 配置自动扫描包路径,此处采用注解方式 -->
 <context:component-scan base-package="com.demo"></context:component-scan>
 <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 ,会在web-inf/views/下找到你返回的页面名字.jsp-->
 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/views/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>

4.编写逻辑代码:在src下创建com.demo.controller包

编写类MySpringController。具体代码如下:

package com.demo;

import org.omg.CORBA.Request;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller//注解,配置扫描到controller

public class MySpringController {

//方法
 @RequestMapping(value="/hello",method=RequestMethod.GET)//访问请求的路径,value值是hello

//该方法需要一个ModelMap,它会存储信息,这将hello springMVC字符串放到message里

public String helloWorld(ModelMap map){
  map.addAttribute("message","hello springMVC");

//返回一个string hello,会在配置文件中的前缀后缀进行结合,就相当于跳转到web-inf/views/hello.jsp

return "hello";
 }
 }

----------------------这个方法也可以这么写

@RequestMapping(value="/hello",method=RequestMethod.GET)

public ModelAndView  helloWorld()

{

ModelAndView modelAndView=new ModelAndView("hello");//ModelAndView跳转的页面

modelAndView.addObject("message","hello springMVC");//添加到request域存放数据

return modelAndView;

}

5.去web-inf创建views文件夹,在views文件夹下创建hello.jsp

在jsp页面用el表达式就可以取到数值

<body>
    ${message } <br>

<!--如果没有取到值可以用${requestScope.message}取值,因为它默认存放到request域中-->

</body>

6.访问http://localhost:8080/项目名/hello会跳转到hello.jsp显示数据。

至此一个简单的spring mvc就搭建完成。

个人搭建的,如果有问题欢迎指正。

spring MVC入门教程的更多相关文章

  1. Spring MVC 入门教程示例 (一)

    今天和大家分享下  Spring MVC  入门教程 首先还是从 HelloWorld  web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...

  2. spring mvc入门教程 转载自【http://elf8848.iteye.com/blog/875830】

    目录  一.前言二.spring mvc 核心类与接口三.spring mvc 核心流程图 四.spring mvc DispatcherServlet说明 五.spring mvc 父子上下文的说明 ...

  3. Java - Struts框架教程 Hibernate框架教程 Spring框架入门教程(新版) sping mvc spring boot spring cloud Mybatis

    https://www.zhihu.com/question/21142149 http://how2j.cn/k/hibernate/hibernate-tutorial/31.html?tid=6 ...

  4. Spring MVC新手教程(二)

    第一篇文章宏观讲了Spring MVC概念,以及分享了一个高速入门的样例. 这篇文章主要来谈谈Spring MVC的配置文件. 首先来谈谈web.xml: web项目启动时自己主动载入到内存中的信息, ...

  5. Spring Boot入门教程1、使用Spring Boot构建第一个Web应用程序

    一.前言 什么是Spring Boot?Spring Boot就是一个让你使用Spring构建应用时减少配置的一个框架.约定优于配置,一定程度上提高了开发效率.https://zhuanlan.zhi ...

  6. Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版

    一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...

  7. Spring Boot 入门教程

    Spring Boot 入门教程,包含且不仅限于使用Spring Boot构建API.使用Thymeleaf模板引擎以及Freemarker模板引擎渲染视图.使用MyBatis操作数据库等等.本教程示 ...

  8. Spring Cloud 入门教程 - 搭建配置中心服务

    简介 Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等.我们不必先知道每个 ...

  9. Spring Cloud 入门教程(七): 熔断机制 -- 断路器

    对断路器模式不太清楚的话,可以参看另一篇博文:断路器(Curcuit Breaker)模式,下面直接介绍Spring Cloud的断路器如何使用. SpringCloud Netflix实现了断路器库 ...

随机推荐

  1. 高性能 TCP & UDP 通信框架 HP-Socket v3.5.1

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  2. mui 手势事件配置

    在开发中监听双击屏幕事件时不起作用,需要在mui.init方法的gestureConfig参数中设置需要监听的手势事件 手势事件配置: 根据使用频率,mui默认会监听部分手势事件,如点击.滑动事件:为 ...

  3. MBTiles

    MBTiles Specification MBTiles is a specification for storing tiled map data in SQLite databases for ...

  4. 学习笔记 HTTP参数污染注入

    HTTP参数污染注入源于网站对于提交的相同的参数的不同处理方式导致. 例如: www.XX.com/a?key=ab&key=3 如果服务端返回输入key的值,可能会有 一: ab 二:3 三 ...

  5. VisualStudio 调试Linux

    微软自从换了CEO之后,拥抱开源的步伐真实越来越快了,这部,现在VS可以跟踪Linux程序了 http://blogs.msdn.com/b/vcblog/archive/2015/11/18/ann ...

  6. 【代码笔记】iOS-页面之间的跳转效果

    一,工程图. 二,代码. RootViewController.m -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { ...

  7. nginx+tomcat https实践

    1. 安装ssl'证书 使用Let's Encrypt 的免费证书: 下载源代码: git clone https://github.com/letsencrypt/letsencrypt 我时阿里云 ...

  8. AngularJS 过滤器

    过滤器可以使用一个管道字符(|)添加到表达式和指令中 AngularJS 过滤器可用于转换数据: currency 格式化数字为货币格式. filter 从数组项中选择一个子集. lowercase ...

  9. Hadoop技巧系列索引

    本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink Hadoop技巧(01):插件,终端权限 Had ...

  10. Arduino 1602液晶屏实验和程序

    在Arduino IDE中, 项目->加载库->管理库中搜索LiquidCrystal,然后安装即可 1.接线图 2.引脚图 3.最简单程序 #include <LiquidCrys ...