Spring MVC ——第一个工程之Hello World!
2013-08-26 16:50
351 查看
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文件
4. 创建springMVC-servlet.xml文件,将文件存储在WEB-INF目录下,主要内容如下:
5. 下面创建HelloWorldController.java文件,文件内容如下:
6.新建视图文件:welcome.jsp 存储在webroot目录下。
7. 部署工程,启动tomcat服务器,访问视图。
在浏览器中输入访问地址:
http://localhost:8080/mvcTest/test/helloworld
以上关于配置文件的加载都是加载的安排好默认的方式进行加载。
首先是准备工作:
开发包: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
以上关于配置文件的加载都是加载的安排好默认的方式进行加载。
相关文章推荐
- Spring Boot系列教程二:创建第一个web工程 hello world
- 安装并搭建第一个vue工程 -- hello world(入门级)
- Spring Boot系列教程二:创建第一个web工程 hello world
- android手机开发的第一个工程hello world
- Spring Boot系列教程二:创建第一个web工程 hello world
- UE4 Hello World 创建第一个UE4工程
- android <初级篇> 第一个工程Hello World ! ——菜鸟的学习之路
- UE4 Hello World 创建第一个UE4工程
- 【spring MVC】新手从零新建第一个hello world项目
- intellij第一个项目之hello world
- Node.js入门,第一个APP,Hello World
- Yii Framework 开发教程(1) 第一个应用Hello World
- Android adt bundle 开发环境配置及第一个“Hello world”程序运行
- OpenGL学习笔记(七):创建第一个Qt5.9.3 OpenGL工程模版(与平台无关)
- 调用Uefi第一个驱动--hello world
- playframework学习笔记1 -- 开发环境和第一个工程
- Spring MVC Hello World 实例
- 第一个java程序hello world
- android下第一个程序,Hello World
- spring mvc学习笔记一:hello world