Spring MVC ——第一个工程之Hello World!

Spring MVC是一个非常优秀的MVC框架之一,Spring MVC是Spring框架的扩展,能很好的与Spring框架协同工作。今天开始学习Spring MVC框架。

首先是准备工作:

开发包:Spring MVC 必备的jar包:spring-framework-released-3.0.0 (3.0以上的版本,本人使用的是3.1的版本)

另外还需要准备一个包:comming-loging.jar  用于输出日志的类

开发工具:myeclipse + tomcat

开发环境:jdk5 以上

 

将以上软件安装好及开发环境配置好之后,就开始真正的Spring MVC项目的开发阶段。

下面是第一个Spring MVC的工程的搭建步骤:

1. 创建一个web project

2. 将所需jar包添加到WEB-INF/lib目录中

3. 配置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">
 
 <servlet>
 	<servlet-name>springMVC</servlet-name>
 	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 	<load-on-startup>1</load-on-startup>
 	
 	<init-param>
 		<param-name>contextConfigLocation</param-name>
 		<param-value>classpath*:springMVC-servlet.xml</param-value>
 	</init-param>
 </servlet>
 
 <servlet-mapping>
	<servlet-name>springMVC</servlet-name>
	<url-pattern>/</url-pattern>
 </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


4. 创建springMVC-servlet.xml文件,将文件存储在WEB-INF目录下,主要内容如下:

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<bean name="/test/helloworld" class="com.caott.web.controller.HelloWorldController"/>

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/" p:suffix=".jsp" />
</beans>


5. 下面创建HelloWorldController.java文件,文件内容如下:

/**
 * 第一个Spring MVC项目,hello world
 */
package com.caott.web.controller;

import java.util.ResourceBundle.Control;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloWorldController implements Controller{

	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {

		System.out.println("===========Hello World==============");
		return new ModelAndView("/welcome");
	}

}


6.新建视图文件:welcome.jsp  存储在webroot目录下。

 

7. 部署工程,启动tomcat服务器,访问视图。

在浏览器中输入访问地址:

http://localhost:8080/mvcTest/test/helloworld

 

以上关于配置文件的加载都是加载的安排好默认的方式进行加载。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值