springMVC配置步骤
所用的工具为eclipse for javaEE,tomcat 8.0
1.导入jar包

把以上的jar包全部复制到项目的WebContent/WEB-INF/lib目录中
2.在webContent/WEB-INF下新增或编辑web.xml

web.xml的完整内容为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>hadoopWeb</display-name> <servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
这里只拦截.do后缀的请求,spring-servlet.xml放在classpath(右键build path/configure bulid path/source里可以看到哪几个文件夹,也可以在libraries下新增)下就行
3.在java Resource/src下新增或编辑spring-servlet.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:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="action"></context:component-scan> <bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
这里springMVC在拦截到.do后缀的请求后会扫描 scr/action 下标记了@Controller注解的类,处理完后会请求 webContent/ 下的后缀为".jsp"的文件
3.进行测试,顺便记录SpringMVC怎么传值
在webContent下新建jsp文件index.jsp进行传值,完整内容为:
<%@ 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>
<form action="print.do">
<input type="text" name="s" ></input>
<input type="submit"></input>
</form>
</body>
</html>
在src/action下新建class文件print.java,完整内容为:
package action; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class PrintAction { @RequestMapping(value="print.do")
public String print(String s,Model model){ System.out.println(s);
s+=".badck";
model.addAttribute("s2",s);
return "print";
}
}
在webContent下新建jsp文件print.jsp,完整内容为:
<%@ 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">
<title>Insert title here</title>
</head>
<body>
<%
String s=(String)request.getAttribute("s2");
out.println("输入的内容为:"+s);
%>
</body>
</html>
index.jsp请求print.do,被pringMVC拦截到,扫描action文件夹,发现print.java被标注为Controller,并发现它的方法print(String s,Model model)被注释了@RequestMapping(value="print.do"),开始运行print方法.
参数s是要传入的值,在index.jsp和print的方法的参数两边的名称要一致,会自动将index.jsp 的请求中的名为"s"的值传到参数中的"s",
在print.java中处理完后,通过model.addAttribute("s2",s); 语句把s的值传给下一个文件,由于最后return "print";,所以springMVC会到webContent下找名为"print",后缀为".jsp"的文件
在print.jsp上接收的时候用String s=(String)request.getAttribute("s2");语句接受s的值.
效果:
在index.jsp输入3并提交

跳转到print.jsp,显示3.badck

并在后台输出了3,我运行了两次,所以有两个3

最后print.jsp输出带了".badck"并在后台有输出说明print.java确实运行到了
springMVC配置步骤的更多相关文章
- Spring-MVC开发步骤(入门配置)
Spring-MVC开发步骤(入门配置) Step1.导包 spring-webmvc Step2.添加spring配置文件 Step3.配置DispatcherServlet 在web.xml中: ...
- SpringMVC_01 SpringMVC五大组件、SpringMVC编程步骤(不使用注解进行配置)、SpringMVC编程步骤(利用注解进行配置)、参数获取、响应数据
1 什么是SpringMVC 是一个mvc框架,用来简化基于mvc架构的web应用程序的 开发. 2 SpringMVC五大组件 DispatcherServlet (前端控制器) HanlderMa ...
- SpringMVC配置实例
一.SpringMVC概述 MVCII模式实现的框架技术 Model--业务模型(Biz,Dao...) View--jsp及相关的jquery框架技术(easyui) Contraller--Dis ...
- 3.2.2 SpringMVC配置式开发
SpringMVC配置式开发 1. SpringMVC运行原理(执行过程) 2. 需求 用户提交一个请求, 服务端处理器接收到请求后, 给出一条信息,在相应页面中显示该条信息 3. 开发步骤 (1) ...
- Spring-MVC配置Gson做为Message Converter解析Json
Spring-MVC配置Gson做为Message Converter解析Json 在学习Spring的时候看到可以使用@RequestBody 和@ResponseBody注解来是的Spring自动 ...
- log4j.properties 详解与配置步骤
一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一般警告,比如session丢失IN ...
- log4j.properties 详解与配置步骤(转)
找的文章,供参考使用 转自 log4j.properties 详解与配置步骤 一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR ...
- MySQL数据库集群进行正确配置步骤
MySQL数据库集群进行正确配置步骤 2010-06-09 10:47 arrowcat 博客园 字号:T | T 我们今天是要和大家一起分享的是对MySQL数据库集群进行正确配置,我前两天在相关网站 ...
- Apache安装配置步骤
注释:这里以Linux 红帽商业版为例~~~~~~~纯手打啊 Apache安装配置步骤 准备:关闭其他虚拟设备 #/etc/init.d/libvirtd stop #/etc/init.d/xend ...
随机推荐
- ubuntu mysql 更改IP导致mysql无法启动
bind-address = 127.0.0.1 => bind-address= 136.129.20.168 IP要这么改 这么改远程连不上,那么需要把这行整行注释掉,重启MYSQL,tel ...
- logstash搭建日志追踪系统
前言 开始博客之前,首先说下10月份没写博客的原因 = =. 10月份赶上国庆,回了趟老家休息了下,回来后自己工作内容发生了点改变,开始搞一些小架构的东西以及研究一些新鲜东西,当时我听到这个消息真的是 ...
- Javascript的精华啊【如果以后我看到了或者想到了再继续补吧】
我不过略有一些讨人喜欢的地方而已,怎么会有什么迷人的魔力呢? 一.语法 JS只有一个数字类型,64位浮点数,所以1和1.0是相同的.为什么这么设计:防止短整型的溢出. 二.对象 1.通常将一个对象的值 ...
- .net破解一(反编译,反混淆-剥壳)
大家好,前段时间做数据分析,需要解析对方数据,而数据文件是对方公司内部的生成方式,完全不知道它是怎么生成的. 不过还好能拿到客户端(正好是C#开发)所以第一件事就是用Reflector编译,但是没有想 ...
- 教你写一个web远程控制小工具
惯例先上图 晚上躺床上了,发现忘关电脑了,又不想起来关,来用手机控制电脑多好,百度了下,果然一大把.哈,我自己为什么不自己也实现个呢,任意的自己diy.Just do it. 如果不想看如何实现,那么 ...
- Oracle中序列(SEQUENCE)的使用一例
曾经在触发器中使用序列(SEQUENCE): create or replace trigger TRI_SUPPLIER before insert on SUPPLIER for each row ...
- Matlab绘图详解
Matlab绘图 强大的绘图功能是Matlab的特点之一,Matlab提供了一系列的绘图函数,用户不需要过多的考虑绘图的细节,只需要给出一些基本参数就能得到所需图形,这类函数称为高层绘图函数.此外,M ...
- Apache CXF实现WebService发布和调用
第一种方法:不用导入cxf jars 服务端: 1. 新建Web工程 2.新建接口和实现类.测试类 目录结构图如下: 接口代码: package com.cxf.spring.service; imp ...
- IE对象最后一个属性后不要加逗号,否则在IE7及以下版本中会报错
某函数返回一个对象,如果在最后一个属性后加逗号,IE7及以下版本中会报错 正确代码: return{ top:rect.top-top, bottom:rect.bottom-top, left:re ...
- DLL丢失修复
DLL丢失修复,简答傻瓜式! DirectX修复工具(DirectX Repair)是一款系统级工具软件,简便易用.本程序为绿色版,无需安装,可直接运行. 本程序的主要功能是检测当前系统的Dir ...