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 ...
随机推荐
- 带参数的CLR存储过程
昨天有学习<简单创建与布署CLR存储过程>http://www.cnblogs.com/insus/p/4371762.html,知道怎样创建以及布署至SQL中去. 下面这个范例是实现CL ...
- 华为acl(traffic-filter)和dhcp管理
华为alc配置实例:-traffic-filter # 在VLAN100上配置基于ACL的报文过滤,允许源IP地址为192.168.0.2/32的报文通过,丢弃其他报文. <HUAWEI> ...
- iptables案例手册
Linux防火墙Iptable如何设置只允许某个ip访问80端口,只允许特定ip访问某端口 iptables常用实例备查(更新中) 9个常用iptables配置实例 案例: http://www.cn ...
- 数据库 SQL语法一
建立表语句 CREATE TABLE TABLENAME(COL_NAME1 TYPE,COL_NAME2 TYPE,......); 常用TYPE说明 INT 正数 CHAR(LENGTH) 定长字 ...
- web—第四章css&第五章
web—第四章css&第五章 终于迎接等待已久的CSS,在没学这个之前,我们只会用一点img,查一点小图片,或者是用style改一下颜色,而且比较麻烦.现在多了个css在文件夹在创建一个cs ...
- File类
存储在变量,数组和对象中的数据是暂时的,当程序终止时他们就会丢失.为了能够永久的保存程序中创建的数据,需要将他们存储到硬盘或光盘的文件中.这些文件可以移动,传送,亦可以被其他程序使用.由于数据存储在文 ...
- 数据库MySQL与Oracle的一些去O注意项
一.oracle递归查询语句start with ...connect by prior ① 给你一张表,表里面有主键id,以及该项的父节点parent_id,查询出该表中所有的父子关系节点树? Or ...
- GPS围栏两个多边形相交问题的奇葩解法
前言 GPS测量仪测量的产地面积,然后提交到系统中,系统需要校验这块产地和其他产地是否有重叠,重叠超过10%就要提出警告这块产地已经被XXX登记入库了.GPS测量仪测量出来的数据是连续的经纬度坐标数据 ...
- Java学习笔记(二二)——Java HashMap
[前面的话] 早上起来好瞌睡哈,最近要注意一样作息状态. HashMap好好学习一下. [定义] Hashmap:是一个散列表,它存储的内容是键值对(key——value)映射.允许nul ...
- PHP+mysql数据库开发搜索功能:中英文分词+全文检索(MySQL全文检索+中文分词(SCWS))
PHP+mysql数据库开发类似百度的搜索功能:中英文分词+全文检索 中文分词: a) robbe PHP中文分词扩展: http://www.boyunjian.com/v/softd/robb ...