新建springmvc01项目
1.创建项目,导入jar包
拷贝jar/spring/first下的五个spring的jar包,以及jar/spring/mvc下的两个mvcjar包放在lib下

2.创建Spring MVC配置文件
复制xml到src下,并将ApplicationContext.xml名字改为spring-mvc.xml

3.在web.xml中配置DispatcherServlet
在web.xml中配置DispatcherServlet,直接复制PPT上面的相关代码即可,注意,web.xml中通过DisPatcherservlet 的初始化方法启动了spring容器。之前我们在测试方法中总是要去写

启动容器代码,现在不需要了。这里的代码只要写一次。

4.创建Controller
在controller包下新建HelloController类,要求实现Controller接口,返回的是一个ModelAndView

5.配置Controller
a.声明Controller b.定义请求处理映射HandlerMapping c.定义视图解析器ViewResolver

6.创建页面
在WEB-INF下新建一个文件hello.jsp

8.部署-》启动Tomcat-》访问http://localhost:8088/springmvc/hello.do,则会输出JSP中的内容

注意:很容易出现500错误,一定是xml配置文件路径有问题,注意检查路径!

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

HelloController.java:

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

//二级控制器:处理业务逻辑
//要求实现Controller接口,只能处理一个请求。

public class HelloController implements Controller{

public ModelAndView handleRequest(
HttpServletRequest req,HttpServletResponse res)
throws Exception {
System.out.println("helloController处理请求");
//hello是视图名,ViewResolver会将视图名解析成真正的页面的名称,
//比如解析成hello.jsp
return new ModelAndView("hello");
}

}

spring-mvc.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<bean id="helloController" class="controller.HelloController"/>

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">helloController</prop>
</props>
</property>
</bean>

<!-- 这样,视图名hello通过以下配置可以映射到/WEB-INF/hello.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

WebRoot/WEB-INF/hello.jsp:

<h1>Hello,SpringMVC!</h1>

结果:输入http://localhost:8088/springmvc01/hello.do:

要求熟练掌握这个流程思想!

springday03-go2的更多相关文章

  1. Spring-day03

    Spring集成JDBC:提供了一些方便我们使用JDBC的工具类; query(String,ResultSetHandler handler,Object..parm){ Connection co ...

  2. go2基本类型

    /* Go基本类型 布尔型:bool - 长度:1字节 - 取值范围:true, false - 注意事项:不可以用数字代表true或false 整型:int/uint - 根据运行平台可能为32或6 ...

  3. 后端程序员之路 52、A Tour of Go-2

    # flowcontrol    - for        - for i := 0; i < 10; i++ {        - for ; sum < 1000; {        ...

  4. Tomcat服务器进击

    一.Tomcat服务器端口配置更改 Tomcat的所有配置都放在conf文件夹之中,server.xml文件就是配置的核心文件. 现在呢,我们需要将8080端口设置成8088端口. 启动端口默认: & ...

  5. jQuery动画与特效详解

    本文主要是讲解和学习jQuery的自动显隐,渐入渐出等. 1.显示和隐藏hide()和show() 对于动画来说,显示和隐藏是最基本的效果之一,本节简单介绍jQuery的显示和隐藏. 代码如下: &l ...

  6. struts2.5的配置及其注意事项

    坑爹的apache,官方的jar包提供了一个struts2的运行最小jar包

  7. Cordova+Asp.net Mvc+GIS跨平台移动应用开发实战1-系统初步搭建(附演示,apk,全部源码)

    1.前言 身处在移动互联网的今天,移动应用开发炙手可热,身为程序猿的我们怎么能错过开发一款我们自己的APP.本人算是一个基于.net的GIS开发入门者(马上就大四啦), 暑假在学校参加GIS比赛有大把 ...

  8. PHP+ajaxfileupload与jcrop插件结合 完成头像上传

    昨天花了点时间整合了一下头像插件 东拼西凑的成果 先来看下效果

  9. Moon.Orm性能报告

    以下为有网友公司的评估测试及使用规范 大家可以下载word看看 http://pan.baidu.com/s/1hquvRuc 一.和ADO.NET进行的压力测试 说明:2000并发用户,此图为一网友 ...

  10. java web学习总结(三) -------------------TOMCAT使用帮助(二)

    一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命令的用法如下:

随机推荐

  1. http协议(转)

    主要还是为了存放状态码··· 剖析 HTTP 协议   目录 HTTP 概述 HTTP 消息结构 HTTP 请求 HTTP 响应 HTTP 状态码 参考 回到顶部 HTTP 概述 HTTP 是什么? ...

  2. Windows 一键安装OpenSSL

    原理:OpenSSL在github上有开源项目,我们只需要把代码克隆到本地,在本地编译一下就好了 注意事项: 1->在github上获取源码,必须要安装git for windows,网址 ht ...

  3. php---初学者git使用

    1.git自学网站 廖雪峰的官方网站 2.安装一个简单的git 创建用户名.邮箱 git config --global user.name "your name" git con ...

  4. Mongoose中关联查询populate的使用

    MongoDB中没有join的特性,因此无法使用join进行表的连接和关联查询,在Mongoose中封装了populate方法,在定义一个 Schema 的时候可以指定了其中的字段(属性)是另一个Sc ...

  5. MVVM with ReactiveCocoa

    内容提要: 本文首先对比MVC简单介绍了MVVM的概念和优点,其次,简单介绍了Reactive Cocoa的使用,最后,通过一个例子介绍了使用Reactive Cocoa的MVVM框架. 正文: 首先 ...

  6. Aptana快捷键(方便查询)

    窗口类:Ctrl+ Shift +L 调出快捷键提示Ctrl+ W 关闭当前标签窗口Ctrl+ Shift + W 关闭当前标签窗口Ctrl+ F6 (或者是Aptana的Ctrl+Tab )下一个编 ...

  7. Selenium2学习-019-WebUI自动化实战实例-017-获取浏览器类型

    Web UI 自动化脚本分布执行过程中有时候需要获取浏览器的相关信息,此文给出了一个简略获取浏览器类型的方法,敬请各位小主们参阅.若有不足之处,敬请大神指正,不胜感激! 闲话少述,上码. /** * ...

  8. Selenium2学习-005-WebUI自动化实战实例-003-三种浏览器(Chrome、Firefox、IE)启动脚本源代码

    此文主要通过 三种浏览器(Chrome.Firefox.IE)启动脚本 功能,进行 Selenium2 三种浏览器启动方法的实战实例讲解.文中所附源代码于 2015-01-18 20:33 亲测通过, ...

  9. c#上传文件(二)使用文件流保存文件

    1.html代码: <asp:FileUpload runat="server" ID="UpLoadFile"/> <asp:Button ...

  10. Apple Developer Program Roles Overview

    Apple Developer Program Roles Overview There are three roles that can be assigned to Apple Developer ...