新建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. Jquery小例子:全选按钮、加事件、挂事件;parent()语法;slideToggle()语法;animate()语法;元素的淡入淡出效果:fadeIn() 、fadeOut()、fadeToggle() 、fadeTo();function(e):e包括事件源和时间数据;append() 方法

    function(e): 事件包括事件源和事件数据,事件源是指是谁触发的这个事件,谁就是事件源(div,按钮,span都可以是事件源),时间数据是指比如点击鼠标的事件中,事件数据就是指点击鼠标的左建或 ...

  2. 国外it网站收集

    1.http://news.com.com/2.http://www.zdnet.com/3.http://www.salon.com/tech/index.html4.http://www.brin ...

  3. ASP.NET MVC3 Areas 分离项目 同名控制器(同名Controller) 演示demo

    为什么需要分离? 我们知道MVC项目各部分职责比较清晰,相比较ASP.NET Webform而言,MVC项目的业务逻辑和页面展现较好地分离开来,这样的做法有许多优点,比如可测试,易扩展等等.但是在实际 ...

  4. iOS GCD简单使用

    Grand Central Dispatch (GCD) 1)运行在主线程的Main queue,通过dispatch_get_main_queue获取. /*!* @function dispatc ...

  5. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

  6. How to read the HTML DTD

    Contents How to read the HTML DTD 1. DTD Comments 2. Parameter Entity definitions 3. Element declara ...

  7. UVA 10127题目的解答

    #include <iostream>#include <cstdio>#include <cmath> int main(){ int num; while (s ...

  8. Kafka使用入门教程 简单介绍

    介绍 Kafka是一个分布式的.可分区的.可复制的消息系统.它提供了普通消息系统的功能,但具有自己独特的设计.这个独特的设计是什么样的呢?   首先让我们看几个基本的消息系统术语: Kafka将消息以 ...

  9. 调试 rewrite

    等号后面是变量 访问http://m-test.jinlianchu.com/member/register.html?inviteCode=jlc24639  的跳转到https://wx-test ...

  10. Android --http请求之HttpURLConnection

    参考博客:Android HttpURLConnection 基础使用 参考博客:Android访问网络,使用HttpURLConnection还是HttpClient? String getUrl ...