新建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. Div自适应高度的方法

    http://www.yutheme.cn/website/index.php/content/view/39/63.html div高度自适应是个比较麻烦的问题,在朋友artery那里看到这个文章, ...

  2. ZooKeeper Recipes and Solutions 翻译

    ZooKeeper 秘诀 与解决方案 A Guide to Creating Higher-level Constructs with ZooKeeper Out of the Box Applica ...

  3. Android开发笔记:SQLite导入导出数据

    SQLite是Android中最方便使用的数据库了,现在看下如何快速的在SQLite中导入导出数据. 首先由于是.NET项目转Android,原有数据库使用的是SQLSERVER,由于项目相同部分结构 ...

  4. JS之toString方法

    1.JS中几乎每个值都有toString方法,null和undefined除外 2.对于字符串形式的值也可以使用toString()方法,返回该字符串的一个副本 3.toString(radix)方法 ...

  5. 关于action script与js相互调用的Security Error问题

    大家都知道,as和js相互调用可以通过ExternalInterface.call和ExternalInterface.addCallback来进行. 比较好的做法是使用之前通过ExternalInt ...

  6. LeetCode Meeting Rooms

    原题链接在这里:https://leetcode.com/problems/meeting-rooms/ Given an array of meeting time intervals consis ...

  7. 北大,awk 命令基础练习

    北大,awk 命令基础练习 Red Hat Enterprise Linux Version 5.3 Get the latest news about the world's Open Source ...

  8. python3学习问题汇总

    1.python2脚本转python3报类型错误 TypeError: ‘str’ does not support the buffer interface 原因:Python3x的string类型 ...

  9. 30天,App创业从0到1【7.12西安站】

    活动概况 时间:2015年07月12日13:30-16:30 地点:汇天使咖啡(高新路36号智空间二楼) 主办:APICloud.UPYUN.万紫网络 网址:www.apicloud.com 费用:免 ...

  10. ssh secure shell client

    登陆方式和putty一样